core: Transition device close event to use event_data_lock
authorChris Dickens <christopher.a.dickens@gmail.com>
Tue, 18 Nov 2014 07:53:09 +0000 (23:53 -0800)
committerChris Dickens <chris.dickens@hp.com>
Fri, 19 Dec 2014 19:13:46 +0000 (11:13 -0800)
This change removes the device_close_lock and uses the shared event
data lock to protect the value of the device_close counter.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
libusb/core.c
libusb/io.c
libusb/libusbi.h
libusb/version_nano.h

index 5d1b48d..87cb776 100644 (file)
@@ -1400,18 +1400,18 @@ void API_EXPORTED libusb_close(libusb_device_handle *dev_handle)
         * descriptor from the polling loop. */
 
        /* record that we are closing a device */
-       usbi_mutex_lock(&ctx->device_close_lock);
+       usbi_mutex_lock(&ctx->event_data_lock);
        ctx->device_close++;
-       usbi_mutex_unlock(&ctx->device_close_lock);
+       usbi_mutex_unlock(&ctx->event_data_lock);
 
        /* write some data on control pipe to interrupt event handlers */
        r = usbi_write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
        if (r <= 0) {
                usbi_warn(ctx, "internal signalling write failed, closing anyway");
                do_close(ctx, dev_handle);
-               usbi_mutex_lock(&ctx->device_close_lock);
+               usbi_mutex_lock(&ctx->event_data_lock);
                ctx->device_close--;
-               usbi_mutex_unlock(&ctx->device_close_lock);
+               usbi_mutex_unlock(&ctx->event_data_lock);
                return;
        }
 
@@ -1427,9 +1427,9 @@ void API_EXPORTED libusb_close(libusb_device_handle *dev_handle)
        do_close(ctx, dev_handle);
 
        /* we're done with closing this device */
-       usbi_mutex_lock(&ctx->device_close_lock);
+       usbi_mutex_lock(&ctx->event_data_lock);
        ctx->device_close--;
-       usbi_mutex_unlock(&ctx->device_close_lock);
+       usbi_mutex_unlock(&ctx->event_data_lock);
 
        /* Release event handling lock and wake up event waiters */
        libusb_unlock_events(ctx);
index 2b3c755..029ed57 100644 (file)
@@ -1112,7 +1112,6 @@ int usbi_io_init(struct libusb_context *ctx)
 
        usbi_mutex_init(&ctx->flying_transfers_lock, NULL);
        usbi_mutex_init(&ctx->pollfds_lock, NULL);
-       usbi_mutex_init(&ctx->device_close_lock, NULL);
        usbi_mutex_init_recursive(&ctx->events_lock, NULL);
        usbi_mutex_init(&ctx->event_data_lock, NULL);
        usbi_mutex_init(&ctx->event_waiters_lock, NULL);
@@ -1174,7 +1173,6 @@ err_close_pipe:
 err:
        usbi_mutex_destroy(&ctx->flying_transfers_lock);
        usbi_mutex_destroy(&ctx->pollfds_lock);
-       usbi_mutex_destroy(&ctx->device_close_lock);
        usbi_mutex_destroy(&ctx->events_lock);
        usbi_mutex_destroy(&ctx->event_data_lock);
        usbi_mutex_destroy(&ctx->event_waiters_lock);
@@ -1198,7 +1196,6 @@ void usbi_io_exit(struct libusb_context *ctx)
 #endif
        usbi_mutex_destroy(&ctx->flying_transfers_lock);
        usbi_mutex_destroy(&ctx->pollfds_lock);
-       usbi_mutex_destroy(&ctx->device_close_lock);
        usbi_mutex_destroy(&ctx->events_lock);
        usbi_mutex_destroy(&ctx->event_data_lock);
        usbi_mutex_destroy(&ctx->event_waiters_lock);
@@ -1659,9 +1656,9 @@ int API_EXPORTED libusb_try_lock_events(libusb_context *ctx)
 
        /* is someone else waiting to close a device? if so, don't let this thread
         * start event handling */
-       usbi_mutex_lock(&ctx->device_close_lock);
+       usbi_mutex_lock(&ctx->event_data_lock);
        ru = ctx->device_close;
-       usbi_mutex_unlock(&ctx->device_close_lock);
+       usbi_mutex_unlock(&ctx->event_data_lock);
        if (ru) {
                usbi_dbg("someone else is closing a device");
                return 1;
@@ -1750,9 +1747,9 @@ int API_EXPORTED libusb_event_handling_ok(libusb_context *ctx)
 
        /* is someone else waiting to close a device? if so, don't let this thread
         * continue event handling */
-       usbi_mutex_lock(&ctx->device_close_lock);
+       usbi_mutex_lock(&ctx->event_data_lock);
        r = ctx->device_close;
-       usbi_mutex_unlock(&ctx->device_close_lock);
+       usbi_mutex_unlock(&ctx->event_data_lock);
        if (r) {
                usbi_dbg("someone else is closing a device");
                return 0;
@@ -1778,9 +1775,9 @@ int API_EXPORTED libusb_event_handler_active(libusb_context *ctx)
 
        /* is someone else waiting to close a device? if so, don't let this thread
         * start event handling -- indicate that event handling is happening */
-       usbi_mutex_lock(&ctx->device_close_lock);
+       usbi_mutex_lock(&ctx->event_data_lock);
        r = ctx->device_close;
-       usbi_mutex_unlock(&ctx->device_close_lock);
+       usbi_mutex_unlock(&ctx->event_data_lock);
        if (r) {
                usbi_dbg("someone else is closing a device");
                return 1;
@@ -2062,9 +2059,9 @@ redo_poll:
 
                /* read the dummy data from the control pipe unless someone is closing
                 * a device */
-               usbi_mutex_lock(&ctx->device_close_lock);
+               usbi_mutex_lock(&ctx->event_data_lock);
                ru = ctx->device_close;
-               usbi_mutex_unlock(&ctx->device_close_lock);
+               usbi_mutex_unlock(&ctx->event_data_lock);
                if (!ru) {
                        ret = usbi_read(ctx->ctrl_pipe[0], &dummy, sizeof(dummy));
                        if (ret != sizeof(dummy)) {
index b1c056e..5548b80 100644 (file)
@@ -245,7 +245,7 @@ struct libusb_context {
        int debug_fixed;
 
        /* internal control pipe, used for interrupting event handling when
-        * something needs to modify poll fds. */
+        * an internal event occurs. */
        int ctrl_pipe[2];
 
        struct list_head usb_devs;
@@ -277,11 +277,6 @@ struct libusb_context {
        unsigned int pollfds_modified;
        usbi_mutex_t pollfds_lock;
 
-       /* a counter that is set when we want to interrupt event handling, in order
-        * to safely close a device, and a lock to protect it. */
-       unsigned int device_close;
-       usbi_mutex_t device_close_lock;
-
        /* user callbacks for pollfd changes */
        libusb_pollfd_added_cb fd_added_cb;
        libusb_pollfd_removed_cb fd_removed_cb;
@@ -296,6 +291,10 @@ struct libusb_context {
        /* A lock to protect internal context event data. */
        usbi_mutex_t event_data_lock;
 
+       /* A counter that is set when we want to interrupt and prevent event handling,
+        * in order to safely close a device. Protected by event_data_lock. */
+       unsigned int device_close;
+
        /* used to wait for event completion in threads other than the one that is
         * event handling */
        usbi_mutex_t event_waiters_lock;
index 4313410..3ac4f9e 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 10933
+#define LIBUSB_NANO 10934