darwin: use destructor function instead of atexit to cleanup state
authorNathan Hjelm <hjelmn@me.com>
Wed, 26 Feb 2014 15:02:40 +0000 (08:02 -0700)
committerNathan Hjelm <hjelmn@me.com>
Wed, 26 Feb 2014 15:02:40 +0000 (08:02 -0700)
Using an atexit function to cleanup the state could cause weird interactions
with other atexit functions. Ensure the darwin cleanup function is called
last by making it a destructor function instead.

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
libusb/os/darwin_usb.c
libusb/version_nano.h

index d1092ec..beba158 100644 (file)
@@ -401,7 +401,8 @@ static void *darwin_event_thread_main (void *arg0) {
   pthread_exit (NULL);
 }
 
-static void _darwin_finalize(void) {
+/* cleanup function to destroy cached devices */
+static void __attribute__((destructor)) _darwin_finalize(void) {
   struct darwin_cached_device *dev, *next;
 
   usbi_mutex_lock(&darwin_cached_devices_lock);
@@ -413,7 +414,6 @@ static void _darwin_finalize(void) {
 
 static int darwin_init(struct libusb_context *ctx) {
   host_name_port_t host_self;
-  static int initted = 0;
   int rc;
 
   rc = darwin_scan_devices (ctx);
@@ -424,11 +424,6 @@ static int darwin_init(struct libusb_context *ctx) {
   if (OSAtomicIncrement32Barrier(&initCount) == 1) {
     /* create the clocks that will be used */
 
-    if (!initted) {
-      initted = 1;
-      atexit(_darwin_finalize);
-    }
-
     host_self = mach_host_self();
     host_get_clock_service(host_self, CALENDAR_CLOCK, &clock_realtime);
     host_get_clock_service(host_self, SYSTEM_CLOCK, &clock_monotonic);
index 227e258..fb49a5f 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 10869
+#define LIBUSB_NANO 10870