darwin: Fix race condition that results in segmentation fault
authorChris Dickens <christopher.a.dickens@gmail.com>
Tue, 3 Mar 2020 00:23:45 +0000 (16:23 -0800)
committerChris Dickens <christopher.a.dickens@gmail.com>
Fri, 6 Mar 2020 06:47:52 +0000 (22:47 -0800)
commitec01c28e1e892b410bdd1581a2bd4e46cbd03eac
tree49091b5b2893cfe5c40abd5593daad31343c789c
parent281ca64c8ac9b9ee6ec72f2b776016aa98e964ba
darwin: Fix race condition that results in segmentation fault

Commit 763668cc92 ("darwin: fix occasional dead-lock on libusb_exit")
resolved the deadlock situation observed in #112, but unfortunately
there is a very rare race condition that can occur when the asynchronous
event thread exits the run loop before CFRunLoopWakeUp() is called. This
can occur when the shutdown source signal is processed as part of other
events in the run loop, in which case the thread was already "awake".

Prior to this change I was able to consistently trigger a segmentation
fault within 10,000 iterations of a libusb_init()/libusb_exit() loop.
With this change I reached over 4 million iterations without issue.

Closes #701

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