diff options
author | Hans de Goede <hdegoede@redhat.com> | 2013-05-16 16:43:24 +0200 |
---|---|---|
committer | Hans de Goede <hdegoede@redhat.com> | 2013-05-16 17:18:32 +0200 |
commit | 4ea8e4a1b99e6ff5578dd2c7e0d2f7bbfd1631b3 (patch) | |
tree | 18a3801e5a54168742d73fb684fd69ee2b8cfe60 | |
parent | fc8be23868ef1161278e978568ff630f27132cb3 (diff) | |
download | libusb-4ea8e4a1b99e6ff5578dd2c7e0d2f7bbfd1631b3.tar.gz |
core: Fixup default_context handling
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>
-rw-r--r-- | libusb/core.c | 17 | ||||
-rw-r--r-- | libusb/version_nano.h | 2 |
2 files changed, 9 insertions, 10 deletions
diff --git a/libusb/core.c b/libusb/core.c index 4e09a84..c8932c3 100644 --- a/libusb/core.c +++ b/libusb/core.c @@ -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); diff --git a/libusb/version_nano.h b/libusb/version_nano.h index e33caa6..9f9ffe6 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 10678 +#define LIBUSB_NANO 10679 |