threads_windows.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * libusbx synchronization on Microsoft Windows
  3. *
  4. * Copyright © 2010 Michael Plante <michael.plante@gmail.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef LIBUSB_THREADS_WINDOWS_H
  21. #define LIBUSB_THREADS_WINDOWS_H
  22. #define usbi_mutex_static_t volatile LONG
  23. #define USBI_MUTEX_INITIALIZER 0
  24. #define usbi_mutex_t HANDLE
  25. struct usbi_cond_perthread {
  26. struct list_head list;
  27. DWORD tid;
  28. HANDLE event;
  29. };
  30. struct usbi_cond_t_ {
  31. // Every time a thread touches the CV, it winds up in one of these lists.
  32. // It stays there until the CV is destroyed, even if the thread
  33. // terminates.
  34. struct list_head waiters;
  35. struct list_head not_waiting;
  36. };
  37. typedef struct usbi_cond_t_ usbi_cond_t;
  38. // We *were* getting timespec from pthread.h:
  39. #if (!defined(HAVE_STRUCT_TIMESPEC) && !defined(_TIMESPEC_DEFINED))
  40. #define HAVE_STRUCT_TIMESPEC 1
  41. #define _TIMESPEC_DEFINED 1
  42. struct timespec {
  43. long tv_sec;
  44. long tv_nsec;
  45. };
  46. #endif /* HAVE_STRUCT_TIMESPEC | _TIMESPEC_DEFINED */
  47. // We *were* getting ETIMEDOUT from pthread.h:
  48. #ifndef ETIMEDOUT
  49. # define ETIMEDOUT 10060 /* This is the value in winsock.h. */
  50. #endif
  51. #define usbi_mutexattr_t void
  52. #define usbi_condattr_t void
  53. // all Windows mutexes are recursive
  54. #define usbi_mutex_init_recursive(mutex, attr) usbi_mutex_init((mutex), (attr))
  55. int usbi_mutex_static_lock(usbi_mutex_static_t *mutex);
  56. int usbi_mutex_static_unlock(usbi_mutex_static_t *mutex);
  57. int usbi_mutex_init(usbi_mutex_t *mutex,
  58. const usbi_mutexattr_t *attr);
  59. int usbi_mutex_lock(usbi_mutex_t *mutex);
  60. int usbi_mutex_unlock(usbi_mutex_t *mutex);
  61. int usbi_mutex_trylock(usbi_mutex_t *mutex);
  62. int usbi_mutex_destroy(usbi_mutex_t *mutex);
  63. int usbi_cond_init(usbi_cond_t *cond,
  64. const usbi_condattr_t *attr);
  65. int usbi_cond_destroy(usbi_cond_t *cond);
  66. int usbi_cond_wait(usbi_cond_t *cond, usbi_mutex_t *mutex);
  67. int usbi_cond_timedwait(usbi_cond_t *cond,
  68. usbi_mutex_t *mutex,
  69. const struct timespec *abstime);
  70. int usbi_cond_broadcast(usbi_cond_t *cond);
  71. int usbi_cond_signal(usbi_cond_t *cond);
  72. int usbi_get_tid(void);
  73. #endif /* LIBUSB_THREADS_WINDOWS_H */