poll_windows.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Windows compat: POSIX compatibility wrapper
  3. * Copyright (C) 2009-2010 Pete Batard <pbatard@gmail.com>
  4. * With contributions from Michael Plante, Orin Eman et al.
  5. * Parts of poll implementation from libusb-win32, by Stephan Meyer et al.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #pragma once
  23. #include <windows.h>
  24. #if defined(_MSC_VER)
  25. // disable /W4 MSVC warnings that are benign
  26. #pragma warning(disable:4127) // conditional expression is constant
  27. #endif
  28. // Handle synchronous completion through the overlapped structure
  29. #if !defined(STATUS_REPARSE) // reuse the REPARSE status code
  30. #define STATUS_REPARSE ((LONG)0x00000104L)
  31. #endif
  32. #define STATUS_COMPLETED_SYNCHRONOUSLY STATUS_REPARSE
  33. #define HasOverlappedIoCompletedSync(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) == STATUS_COMPLETED_SYNCHRONOUSLY)
  34. #define DUMMY_HANDLE ((HANDLE)(LONG_PTR)-2)
  35. enum windows_version {
  36. WINDOWS_UNSUPPORTED,
  37. WINDOWS_XP,
  38. WINDOWS_2003, // also includes XP 64
  39. WINDOWS_VISTA_AND_LATER,
  40. };
  41. extern enum windows_version windows_version;
  42. #define MAX_FDS 256
  43. #define POLLIN 0x0001 /* There is data to read */
  44. #define POLLPRI 0x0002 /* There is urgent data to read */
  45. #define POLLOUT 0x0004 /* Writing now will not block */
  46. #define POLLERR 0x0008 /* Error condition */
  47. #define POLLHUP 0x0010 /* Hung up */
  48. #define POLLNVAL 0x0020 /* Invalid request: fd not open */
  49. struct pollfd {
  50. int fd; /* file descriptor */
  51. short events; /* requested events */
  52. short revents; /* returned events */
  53. };
  54. // access modes
  55. enum rw_type {
  56. RW_NONE,
  57. RW_READ,
  58. RW_WRITE,
  59. };
  60. // fd struct that can be used for polling on Windows
  61. struct winfd {
  62. int fd; // what's exposed to libusb core
  63. HANDLE handle; // what we need to attach overlapped to the I/O op, so we can poll it
  64. OVERLAPPED* overlapped; // what will report our I/O status
  65. enum rw_type rw; // I/O transfer direction: read *XOR* write (NOT BOTH)
  66. };
  67. extern const struct winfd INVALID_WINFD;
  68. int usbi_pipe(int pipefd[2]);
  69. int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout);
  70. ssize_t usbi_write(int fd, const void *buf, size_t count);
  71. ssize_t usbi_read(int fd, void *buf, size_t count);
  72. int usbi_close(int fd);
  73. void init_polling(void);
  74. void exit_polling(void);
  75. struct winfd usbi_create_fd(HANDLE handle, int access_mode);
  76. void usbi_free_fd(int fd);
  77. struct winfd fd_to_winfd(int fd);
  78. struct winfd handle_to_winfd(HANDLE handle);
  79. struct winfd overlapped_to_winfd(OVERLAPPED* overlapped);
  80. /*
  81. * Timeval operations
  82. */
  83. #if defined(DDKBUILD)
  84. #include <winsock.h> // defines timeval functions on DDK
  85. #endif
  86. #if !defined(TIMESPEC_TO_TIMEVAL)
  87. #define TIMESPEC_TO_TIMEVAL(tv, ts) { \
  88. (tv)->tv_sec = (long)(ts)->tv_sec; \
  89. (tv)->tv_usec = (long)(ts)->tv_nsec / 1000; \
  90. }
  91. #endif
  92. #if !defined(timersub)
  93. #define timersub(a, b, result) \
  94. do { \
  95. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  96. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  97. if ((result)->tv_usec < 0) { \
  98. --(result)->tv_sec; \
  99. (result)->tv_usec += 1000000; \
  100. } \
  101. } while (0)
  102. #endif