Beispiel #1
0
static gpointer usb_thread(gpointer data)
{
	struct sr_context *ctx = data;

	while (ctx->usb_thread_running) {
		g_mutex_lock(&ctx->usb_mutex);
		libusb_wait_for_event(ctx->libusb_ctx, NULL);
		SetEvent(ctx->usb_event);
		g_mutex_unlock(&ctx->usb_mutex);
		g_thread_yield();
	}

	return NULL;
}
Beispiel #2
0
	int Context::waitForEvent(State & state, Context_wrapper * wrapper){
		Stack * stack = state.stack;
		int result = 0;
		timeval tv;
		if (fillTimeval(state, tv, 1)){
			result = libusb_wait_for_event(wrapper->context, &tv);
			if (result == LIBUSB_SUCCESS){
				stack->push<bool>(true);
				return 1;
			}
			else{
				stack->push<bool>(false);
				stack->push<int>(result);
				return 2;
			}
		}
		else{
			return 0;
		}
	}
Beispiel #3
0
/* ----
 * device_handle_events()
 *
 *	Function called by device_read() and device_write() to perform
 *	the multi-thread correct event handling.
 * ----
 */
static int
device_handle_events(Open8055Card *card, int *completed)
{
	int				rc = 0;
	struct timeval	tv;

	Py_BEGIN_ALLOW_THREADS

	while (!*completed && rc == 0)
	{
		if (libusb_try_lock_events(libusbCxt) == 0)
		{
			/* ----
			 * We got the event lock, so we are the event handler.
			 * ----
			 */
			while(!*completed)
			{
				if (!libusb_event_handling_ok(libusbCxt))
				{
					libusb_unlock_events(libusbCxt);
					break;
				}

				tv.tv_sec = 1;
				tv.tv_usec = 0;
				rc = libusb_handle_events_locked(libusbCxt, &tv);
				if (rc != 0)
					break;
			}
			libusb_unlock_events(libusbCxt);
		}
		else
		{
			/* ----
			 * Somebody else is handling events ... wait for them.
			 * ----
			 */
			libusb_lock_event_waiters(libusbCxt);
			while (!completed)
			{
				/* ----
				 * Now that we have the waiters lock, recheck that
				 * somebody is really processing events.
				 * ----
				 */
				if (!libusb_event_handler_active(libusbCxt))
					break;
				
				/* ----
				 * OK to really wait now.
				 * ----
				 */
				libusb_wait_for_event(libusbCxt, NULL);
			}
			libusb_unlock_event_waiters(libusbCxt);
		}
	}

	Py_END_ALLOW_THREADS

	return rc;
}