Fix #56 race condition causing delayed completion of sync transfers
authorGraeme Gill <graeme2@argyllcms.com>
Sat, 10 Sep 2011 13:47:05 +0000 (15:47 +0200)
committerPeter Stuge <peter@stuge.se>
Thu, 22 Sep 2011 09:25:29 +0000 (11:25 +0200)
commit6696512aade99bb15d6792af90ae329af270eba6
tree9137a1fdd3b78ace9d6b49902054598b57e1b3e7
parentcfee5446cdb2974c1e90c7c379a365c495975aef
Fix #56 race condition causing delayed completion of sync transfers

The sync API had a race where it would check a condition to know if it
needed to call a libusb_handle_events() function. However, the check
was done outside of the lock that is held while the condition is set,
so another thread could completely serve whatever was needed to make
the condition true between it being checked and the event handler being
called. This situation would be detected after a libusb-internal timeout
of 60 seconds, after which the transfer would be completed without
error, but with significant delay.

Original patch at http://marc.info/?l=libusb-devel&m=127252114815709

Changes by Hans de Goede:
- Renamed the "race-proof" functions from libusb_handle_events*_check()
  to libusb_handle_events*_completed()
- Drop r = 0 setting in libusb_handle_events_timeout_completed()
  (to make both completed checking cases identical flow wise)

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
[stuge: Simplify libusb_handle_events_timeout() change with a goto]
[pbatard: Fix _handle_events_timeout() and _completed() definitions]
libusb/io.c
libusb/libusb-1.0.def
libusb/libusb.h
libusb/sync.c