core: Fixup default_context handling
authorHans de Goede <hdegoede@redhat.com>
Thu, 16 May 2013 14:43:24 +0000 (16:43 +0200)
committerHans de Goede <hdegoede@redhat.com>
Thu, 16 May 2013 15:18:32 +0000 (17:18 +0200)
Now that we set the default context earlier in libusb_init:
-There is no need to do it a second time at the end of libusb_init
-We need to set it to NULL if libusb_init fails later on
-Since it now can be set then reset by libusb_init, libusb_exit needs to
 take the lock while testing if the passed in ctx == default_context

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
libusb/core.c
libusb/version_nano.h

index 4e09a84..c8932c3 100644 (file)
@@ -1785,21 +1785,20 @@ int API_EXPORTED libusb_init(libusb_context **context)
        if (r < 0)
                goto err_backend_exit;
 
-       if (context) {
-               *context = ctx;
-       } else if (!usbi_default_context) {
-               usbi_dbg("created default context");
-               usbi_default_context = ctx;
-               default_context_refcnt++;
-       }
        usbi_mutex_static_unlock(&default_context_lock);
 
+       if (context)
+               *context = ctx;
+
        return 0;
 
 err_backend_exit:
        if (usbi_backend->exit)
                usbi_backend->exit();
 err_free_ctx:
+       if (ctx == usbi_default_context)
+               usbi_default_context = NULL;
+
        usbi_mutex_destroy(&ctx->open_devs_lock);
        usbi_mutex_destroy(&ctx->usb_devs_lock);
        usbi_mutex_destroy(&ctx->hotplug_cbs_lock);
@@ -1835,8 +1834,8 @@ void API_EXPORTED libusb_exit(struct libusb_context *ctx)
 
        /* if working with default context, only actually do the deinitialization
         * if we're the last user */
+       usbi_mutex_static_lock(&default_context_lock);
        if (ctx == usbi_default_context) {
-               usbi_mutex_static_lock(&default_context_lock);
                if (--default_context_refcnt > 0) {
                        usbi_dbg("not destroying default context");
                        usbi_mutex_static_unlock(&default_context_lock);
@@ -1844,8 +1843,8 @@ void API_EXPORTED libusb_exit(struct libusb_context *ctx)
                }
                usbi_dbg("destroying default context");
                usbi_default_context = NULL;
-               usbi_mutex_static_unlock(&default_context_lock);
        }
+       usbi_mutex_static_unlock(&default_context_lock);
 
        usbi_mutex_static_lock(&active_contexts_lock);
        list_del (&ctx->list);
index e33caa6..9f9ffe6 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 10678
+#define LIBUSB_NANO 10679