summaryrefslogtreecommitdiff
path: root/libusb/core.c
diff options
context:
space:
mode:
authorChris Dickens <christopher.a.dickens@gmail.com>2020-08-12 16:06:38 -0700
committerChris Dickens <christopher.a.dickens@gmail.com>2020-08-12 16:06:38 -0700
commitd67eb5beaa44c17c09f089a522ff483f4978a978 (patch)
tree8a5dffe3e6779dc95fa1adb32a1dc6d7be69c7af /libusb/core.c
parentba6b8bcb7ea204e65a3deec3be81aacc9f4b6d5a (diff)
downloadlibusb-d67eb5beaa44c17c09f089a522ff483f4978a978.tar.gz
core: Introduce platform events abstraction
The way in which system handles or resources are represented differs greatly between Unix-like operating systems and Windows. Ever since Windows support was added to libusb, Windows been emulating principles of Unix-like operating systems such as file descriptors and poll(). This commit introduces an abstraction layer that completely removes the need to perform any emulation. Fundamentally there are three things that each platform provides to libusb: 1) A signallable event 2) A timer (not required, but useful) 3) A means to wait for event sources such as the above to be triggered The POSIX abstraction for Unix-like operating systems uses file descriptors as the "handles" to the underlying system resources. The signallable event is implemented using a pipe, the timer as a timerfd (where supported) and the poll() system call is used to wait for events. The Windows abstraction uses native HANDLEs as the "handles" to the underlying system resources. The signallable event is implemented using a manual-reset event, the timer as a manual-reset waitable timer, and the WaitForMultipleObjects() system call is used to wait for events. Closes #252 Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
Diffstat (limited to 'libusb/core.c')
-rw-r--r--libusb/core.c42
1 files changed, 2 insertions, 40 deletions
diff --git a/libusb/core.c b/libusb/core.c
index 89cfec3..692350a 100644
--- a/libusb/core.c
+++ b/libusb/core.c
@@ -1188,44 +1188,6 @@ void API_EXPORTED libusb_unref_device(libusb_device *dev)
}
}
-/*
- * Signal the event pipe so that the event handling thread will be
- * interrupted to process an internal event.
- */
-int usbi_signal_event(struct libusb_context *ctx)
-{
- unsigned char dummy = 1;
- ssize_t r;
-
- /* write some data on event pipe to interrupt event handlers */
- r = usbi_write(ctx->event_pipe[1], &dummy, sizeof(dummy));
- if (r != sizeof(dummy)) {
- usbi_warn(ctx, "internal signalling write failed");
- return LIBUSB_ERROR_IO;
- }
-
- return 0;
-}
-
-/*
- * Clear the event pipe so that the event handling will no longer be
- * interrupted.
- */
-int usbi_clear_event(struct libusb_context *ctx)
-{
- unsigned char dummy;
- ssize_t r;
-
- /* read some data on event pipe to clear it */
- r = usbi_read(ctx->event_pipe[0], &dummy, sizeof(dummy));
- if (r != sizeof(dummy)) {
- usbi_warn(ctx, "internal signalling read failed");
- return LIBUSB_ERROR_IO;
- }
-
- return 0;
-}
-
/** \ingroup libusb_dev
* Wrap a platform-specific system device handle and obtain a libusb device
* handle for the underlying device. The handle allows you to use libusb to
@@ -1500,7 +1462,7 @@ void API_EXPORTED libusb_close(libusb_device_handle *dev_handle)
pending_events = usbi_pending_events(ctx);
ctx->device_close++;
if (!pending_events)
- usbi_signal_event(ctx);
+ usbi_signal_event(&ctx->event);
usbi_mutex_unlock(&ctx->event_data_lock);
/* take event handling lock */
@@ -1517,7 +1479,7 @@ void API_EXPORTED libusb_close(libusb_device_handle *dev_handle)
ctx->device_close--;
pending_events = usbi_pending_events(ctx);
if (!pending_events)
- usbi_clear_event(ctx);
+ usbi_clear_event(&ctx->event);
usbi_mutex_unlock(&ctx->event_data_lock);
/* Release event handling lock and wake up event waiters */