threads_windows.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * libusb 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 WINAPI_CHECK(expression) ASSERT_NE(expression, 0)
  23. #define USBI_MUTEX_INITIALIZER 0L
  24. typedef LONG usbi_mutex_static_t;
  25. static inline void usbi_mutex_static_lock(usbi_mutex_static_t *mutex)
  26. {
  27. while (InterlockedExchange(mutex, 1L) == 1L)
  28. SleepEx(0, TRUE);
  29. }
  30. static inline void usbi_mutex_static_unlock(usbi_mutex_static_t *mutex)
  31. {
  32. InterlockedExchange(mutex, 0L);
  33. }
  34. typedef CRITICAL_SECTION usbi_mutex_t;
  35. static inline void usbi_mutex_init(usbi_mutex_t *mutex)
  36. {
  37. InitializeCriticalSection(mutex);
  38. }
  39. static inline void usbi_mutex_lock(usbi_mutex_t *mutex)
  40. {
  41. EnterCriticalSection(mutex);
  42. }
  43. static inline void usbi_mutex_unlock(usbi_mutex_t *mutex)
  44. {
  45. LeaveCriticalSection(mutex);
  46. }
  47. static inline int usbi_mutex_trylock(usbi_mutex_t *mutex)
  48. {
  49. return TryEnterCriticalSection(mutex) != 0;
  50. }
  51. static inline void usbi_mutex_destroy(usbi_mutex_t *mutex)
  52. {
  53. DeleteCriticalSection(mutex);
  54. }
  55. #if !defined(HAVE_STRUCT_TIMESPEC) && !defined(_TIMESPEC_DEFINED)
  56. #define HAVE_STRUCT_TIMESPEC 1
  57. #define _TIMESPEC_DEFINED 1
  58. struct timespec {
  59. long tv_sec;
  60. long tv_nsec;
  61. };
  62. #endif /* HAVE_STRUCT_TIMESPEC || _TIMESPEC_DEFINED */
  63. typedef CONDITION_VARIABLE usbi_cond_t;
  64. static inline void usbi_cond_init(usbi_cond_t *cond)
  65. {
  66. InitializeConditionVariable(cond);
  67. }
  68. static inline void usbi_cond_wait(usbi_cond_t *cond, usbi_mutex_t *mutex)
  69. {
  70. WINAPI_CHECK(SleepConditionVariableCS(cond, mutex, INFINITE));
  71. }
  72. int usbi_cond_timedwait(usbi_cond_t *cond,
  73. usbi_mutex_t *mutex, const struct timeval *tv);
  74. static inline void usbi_cond_broadcast(usbi_cond_t *cond)
  75. {
  76. WakeAllConditionVariable(cond);
  77. }
  78. static inline void usbi_cond_destroy(usbi_cond_t *cond)
  79. {
  80. UNUSED(cond);
  81. }
  82. typedef DWORD usbi_tls_key_t;
  83. static inline void usbi_tls_key_create(usbi_tls_key_t *key)
  84. {
  85. *key = TlsAlloc();
  86. assert(*key != TLS_OUT_OF_INDEXES);
  87. }
  88. static inline void *usbi_tls_key_get(usbi_tls_key_t key)
  89. {
  90. return TlsGetValue(key);
  91. }
  92. static inline void usbi_tls_key_set(usbi_tls_key_t key, void *ptr)
  93. {
  94. WINAPI_CHECK(TlsSetValue(key, ptr));
  95. }
  96. static inline void usbi_tls_key_delete(usbi_tls_key_t key)
  97. {
  98. WINAPI_CHECK(TlsFree(key));
  99. }
  100. static inline unsigned int usbi_get_tid(void)
  101. {
  102. return (unsigned int)GetCurrentThreadId();
  103. }
  104. #endif /* LIBUSB_THREADS_WINDOWS_H */