summaryrefslogtreecommitdiff
path: root/libusb/os/poll_windows.h
Commit message (Collapse)AuthorAgeFilesLines
* core: Introduce platform events abstractionChris Dickens2020-08-121-49/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | The way in which system handles or resources are represented differs greatly between Unix-like operating systems and Windows. Ever since Windows support was added to libusb, Windows been emulating principles of Unix-like operating systems such as file descriptors and poll(). This commit introduces an abstraction layer that completely removes the need to perform any emulation. Fundamentally there are three things that each platform provides to libusb: 1) A signallable event 2) A timer (not required, but useful) 3) A means to wait for event sources such as the above to be triggered The POSIX abstraction for Unix-like operating systems uses file descriptors as the "handles" to the underlying system resources. The signallable event is implemented using a pipe, the timer as a timerfd (where supported) and the poll() system call is used to wait for events. The Windows abstraction uses native HANDLEs as the "handles" to the underlying system resources. The signallable event is implemented using a manual-reset event, the timer as a manual-reset waitable timer, and the WaitForMultipleObjects() system call is used to wait for events. Closes #252 Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Use I/O completion ports for transfersChris Dickens2020-08-101-11/+0
| | | | | | | | | | | As a first step in removing the Windows poll() emulation, switch the transfers to use an I/O completion port. A dedicated per-context thread will wait on the I/O completion port and report transfer completions using usbi_signal_transfer_completion(). This enables the complete removal of the handle_events() function for the Windows backend and removes the notion of one "file descriptor" per transfer. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Switch usbi_transfer to store timeout as timespecChris Dickens2020-03-051-15/+0
| | | | | | | | | | | | | | | | | The transfer timeout is structured around time values provided by the clock_gettime() function. This function uses a timespec structure, but the usbi_transfer structure was storing its calculated timeout in a timeval structure. This mismatch introduces extra work when checking for transfer timeouts as there must be a conversion between these two structures. Eliminate this by storing the calculated timeout as a timespec, thus allowing direct comparison. Note that a conversion to a timeval is still necessary in the libusb_get_next_timeout() function because the public API uses a timeval structure, but this is now the only place where such a conversion is done. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Refactoring to consolidate and simplify common codeChris Dickens2020-02-071-7/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Both the UsbDk and WinUSB backends perform common steps when handling transfers in order to interact with the poll abstraction, both during submission and when processing transfer completion. With some rearranging of shared structures, this can be yanked from the individual backends and placed in the common area. This allows for several functions to be removed outright from each backend. The cancellation logic can also be simplified by attempting CancelIoEx() at the highest level and delegating to the backend if there are alternatives to try should CancelIoEx() fail. After some analysis of how Windows processes asychronous (OVERLAPPED) requests that the underlying driver completes synchronously, it is now evident that such requests need not be handled in any special fashion. Each function that called a driver function that was expected to complete asynchronously had logic to handle the case of a synchronous completion, so this has all been killed off. This significantly cleans up these call sites as now they must only check for an error condition. Finally, the initialization code for the WinUSB backend has been reworked to load the WinUSB DLL independent of the libusbK DLL. Previously when the libusbK DLL was present, all requests to devices using WinUSB would first be sent through the libusbK DLL where they would then be forwarded to the WinUSB DLL. This is slightly inefficient but is also limiting when using Windows 8.1 or later because support for isochronous transfers through WinUSB will be lost. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Convert internal macros to static inline functionsChris Dickens2020-01-251-16/+9
| | | | | | | | | | | | Older versions of the Visual Studio compiler are picky about macros constructed with the 'do { ... } while (0)' construct. Convert these internal ones to static inline functions. The result is functionally equivalent but gets us type checking and a bit more readability. Also address some compiler warnings due to some header files that are being included in a different order than before. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* configure.ac: Cleanup and refactoringChris Dickens2020-01-241-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Make the formatting consistent across the entire file. In particular: - Always quote strings whose values are derived - Use tabs consistently - Wrap all arguments with square brackets Replace the use of '-a' with '&&' to be more portable. Rearrange some of the feature checks to be conditional upon the platform or backend. For example, there is no need to check for nfds_t on Windows because poll() doesn't exist there. Similarly we now only check for timerfd on Linux and Solaris. This translates into slightly faster configure times. Explicitly define tokens for both the poll and thread implementations. This makes the preprocessor conditionals much nicer since it is not necessary to enumerate all possible OS_* tokens. Also replace POLL_NFDS_TYPE with a proper typedef that is based on the availability of the nfds_t type. Migrate to config definition names that are more consistent with autoconf. The check for timerfd actually verifies the presence of the library function instead of just the header definitions, and the token USBI_TIMERFD_AVAILABLE is now HAVE_TIMERFD. Similarly the check for syslog results in a definition of HAVE_SYSLOG. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Misc: Trim and consolidate header file usageChris Dickens2020-01-241-27/+23
| | | | | | | | | | | | | Refactor libusbi.h to include the set of common header files needed by every main source file in the library and change these source files to include libusbi.h first, followed by any non-common headers. Including libusbi.h first ensures that the config definitions are pulled in and will eliminate redundant includes in the individual sources files. Also clean up some whitespace errors and remove unnecessary definitions in the manually generated config.h files. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Improve poll abstractionChris Dickens2020-01-221-3/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Commit 395e5a8a6f ("windows: remove total fds (256) limitations") and commit c730a8410c ("windows: workaround WaitForMultipleObjects max 64 events limitation.") lifted some hard-coded limits in the number of HANDLEs that can be used within the library. This change improves on these changes to make them more efficient. A bitmap has been added to provide an efficient lookup mechanism for located unused file descriptor indices. This avoids the O(n) lookup time for traversing the entire fd_table. This bitmap is dynamically resized along with the fd_table. The incremental size of the fd_table has been reduced from 256 to 64. The vast majority of applications won't need to use 256 HANDLEs, so we can optimize memory usage a bit. Commit fb864b7cde ("fix windows crash when multi-thread do sync transfer") added usbi_inc_fds_ref() and usbi_dec_fds_ref() functions to work around a reference count issue. Remove these functions and change the implementation of usbi_poll() to take a reference to each file descriptor upon entry and drop the references when returning. If the application experiences any kind of crash, there is a problem elsewhere. Finally, make the thread executing usbi_poll() take part in the waiting. The original implementation had this thread simply waiting on a single event while separate threads waited on the HANDLEs. Now this thread will wait on MAXIMUM_WAIT_OBJECTS - 1 HANDLEs, thereby reducing the number of threads that are created. Additionally there is now only a single event object that is shared amongst all waiting threads. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Remove support for WinCE and Visual Studio older than 2013Chris Dickens2020-01-201-4/+0
| | | | | | | | | | | There appears to be no need for the WinCE backend anymore, and it is increasingly difficult to keep healthy as the rest of the library changes. Require at least Visual Studio 2013 to compile. This simplifies matters as there is some semblance of C99 support there. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* windows: remove total fds (256) limitationsFrank Li2019-08-091-2/+0
| | | | | | | | | | | | | | | When queue more usb requests, and more devices working at the same time 256's limitation is easy to reach. This patch remove such limitation of windows version. dymatic allocate map fd_table. each time increase 256 entry if request more fds. Closes #592 Signed-off-by: Frank Li <Frank.Li@nxp.com> Signed-off-by: Nathan Hjelm <hjelmn@google.com>
* windows: fix return type of usbi_{inc,dec}_fds_ref.Josh Gao2019-07-071-2/+2
| | | | | | | | | These functions were declared as returning int, but failed to actually return anything, and no one was using their return values anyway. Closes #562 Signed-off-by: Nathan Hjelm <hjelmn@me.com>
* fix windows crash when multi-thread do sync transferFrank Li2019-04-041-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | fun() { libusb_open() ... sync transfer libusb_close() } two thread call fun infininately. to speed up crash, enable application verifier below 20 cycle, assert(fd!=NULL) happen at check_pollfds below 100 cycle, crash at pollable_fd->overlappend in winusb_get_overlapped result with this fix, success fun over 1000 cycles in handle_events usbi_mutex_lock() fds = ctx->pollfds nfds = ctx->pollfds_cnt; usbi_mutex_unclock() usbi_poll() callback. usbi poll is not in mutex. pollfds may be change by usbi_add_pollfd and usbi_remove_pollfd. Although usbi_add_pollfd and usbi_remove_pollfd hold mutex, but usbi_poll and callback is not in protext of mutex. windows use fd as index of fb_table. fb_table may changed by usbi_remove_pollfd. the map between fd and internal file_descriptor may be wrong. this patch added ref count for file_descriptor, only free file_desciptor and remove it from fb_table when ref count is 0. ref count will be increase when fds copy with mutex lock. so fd always index validate file_descriptor. ref count will be descress before return from handle_events. the file_descriptor can be free safely at this time. Closes #521 Signed-off-by: Frank Li <Frank.Li@nxp.com> Signed-off-by: Nathan Hjelm <hjelmn@me.com>
* Windows: Enable dynamic selection between WinUSB and UsbDk backendsChris Dickens2018-01-081-15/+0
| | | | | | | | | | | | | This commit unifies the two Windows backends into a single project and enables the user to switch to the UsbDk backend, if available, using the libusb_set_option() function. All contexts will use the WinUSB backend by default for backwards compatibility. With this change, the UsbDk-specific projects are no longer required. Closes #309 Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Rework poll() emulation to a much simpler and effective designChris Dickens2018-01-061-28/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The previous poll() implementation worked okay but had some issues. It was inefficient, had a large footprint, and there were simply some use cases that didn't work (e.g. a synchronous transfer that completes when no other event or transfer is pending would not be processed until the next poll() timeout). This commit introduces a new, simpler design that simply associates an OVERLAPPED structure to an integer that acts as a file descriptor. The poll() emulation now solely cares about the OVERLAPPED structure, not transfers or HANDLEs or cancelation functions. These details have been moved up into the higher OS-specific layers. For Windows NT environments, several deficiencies have been addressed: 1) It was previously possible to successfully submit a transfer but fail to add the "file descriptor" to the pollfd set. This was silently ignored and would result in the user never seeing the transfer being completed. 2) Synchronously completed transfers would previously not be processed unless another event (such as a timeout or other transfer completion) was processed. 3) Canceling any one transfer on an endpoint would previously result in *all* transfers on that endpoint being canceled, due to the use of the AbortPipe() function. This commit addresses all of these issues. In particular, run-time detection of the CancelIoEx() function will allow the user to cancel a single outstanding transfer without affecting any others still in process. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* windows: fix USB 3.0 speed detection on Windows 8 or laterPete Batard2014-05-181-6/+12
| | | | | | * ...since Microsoft broke IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX between Windows 7 and Windows 8 * Also improve Windows version detection and reporting * Closes #10
* Windows: Correctly clear backend transfer private informationToby Gray2013-04-111-1/+1
| | | | | | | | | | | | | | | | | | | | | * Without this fix if a transfer is reused then there is a period of time between it being adding to the flying transfer list and submitted where the fd value will be the old fd value. * If this occurs at the same time as all of the following conditions then the incorrect transfer will be handled as having completed: * The old fd value in the reused transfer has been recycled for a currently pending transfer. * This other pending transfer has a later timeout than the reused transfer (so therefore comes later in the flying transfer list). * The other pending transfer completes, therefore signalling the fd. As the flying transfer list is examined in order when handling events, the resubmitted transfer with the old fd value will be considered as completed. This will generally cause a NULL pointer dereference as the OVERLAPPED structure was already freed. Also see: http://libusbx.1081486.n5.nabble.com/Libusbx-devel-PATCH-Fix-NULL-pointer-dereference-in-Windows-and-WinCE-backends-when-reusing-transfers-tt1041.html
* WinCE: Post integration cleanupPete Batard2013-03-031-1/+2
| | | | | | * Update copyrights and switch to UTF-8 everywhere * Add SleepEx() to missing.h, and move include to libusbi.h * Remove ifdef for GetSystemTimeAsFileTime()
* Windows: Simplify poll_windows and add provisions for WinCEToby Gray2013-01-231-1/+11
| | | | | * Because poll_windows now requires struct usbi_transfer to be defined, it's inclusion in libusbi.h had to be moved down.
* Misc: Ensure all sources are UTF-8Pete Batard2012-05-231-1/+1
| | | | * Also remove extra lines at the end of samples
* Windows: Remove #define options and use same set of defaultsPete Batard2012-04-111-5/+0
| | | | | | * The DYNAMIC_FDS, AUTO_CLAIM and FORCE_INSTANT_TIMEOUTS options were introduced for development/testing and don't appear to be used by the Windows backend users => remove them.
* Windows: misc improvementsPete Batard2012-04-101-1/+1
| | | | | | * prefer calloc over malloc * silence VS2010 intellisense warnings on mem allocation * other minor fixes and formatting improvements to align with -pbatard
* Misc: Rebrand to libusbxPete Batard2012-04-031-2/+1
| | | | | * Mentions of 'libusb' in doxygen are changed to 'libusbx' * Also update copyright notices and remove unneeded EOF LFs
* Windows: Fix undefined datatype 'LONG_PTR' in MSVC6Pete Batard2012-02-081-1/+1
| | | | | | * issue reported by Elmi Signed-off-by: Michael Plante <michael.plante@gmail.com>
* Windows: Don't leak pipe fdsPete Batard2011-06-131-4/+6
| | | | | | | | | | | use _open() and _close() rather than _open_osfhandle() and CloseHandle() * use of CloseHandle() prevented the pipe fds from being relinquished on libusb_exit() * leaked fds could lead to the OS running out of new fds and LIBUSB_ERROR_NO_MEM being returned as a result * using _open() avoids _open_osfhandle() redef for cygwin * issue reported by Stephano Antonelli
* configure.ac: Check for poll.h, and for nfds_t on DarwinPeter Stuge2011-06-131-2/+0
| | | | | | | | | | | On Linux, assume nfds_t is always available. On Darwin, fall back to unsigned int when poll() exists but there is no nfds_t, such as on Mac OS X before 10.4. On Windows (both MinGW and Cygwin), always use unsigned int instead of nfds_t, and don't check for poll.h because we use our own poll() implementation.
* Add Windows supportPete Batard2010-07-271-0/+120
Via Cygwin/MinGW, libusb now has windows support. Thanks to contributors: Michael Plante, Orin Eman, Peter Stuge, Stephan Meyer, Xiaofan Chen.