w32pthreads.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (C) 2010-2011 x264 project
  3. *
  4. * Authors: Steven Walters <kemuri9@gmail.com>
  5. * Pegasys Inc. <http://www.pegasys-inc.com>
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * Libav is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * w32threads to pthreads wrapper
  26. */
  27. #ifndef LIBAV_COMPAT_W32PTHREADS_H
  28. #define LIBAV_COMPAT_W32PTHREADS_H
  29. /* Build up a pthread-like API using underlying Windows API. Have only static
  30. * methods so as to not conflict with a potentially linked in pthread-win32
  31. * library.
  32. * As most functions here are used without checking return values,
  33. * only implement return values as necessary. */
  34. #define WIN32_LEAN_AND_MEAN
  35. #include <windows.h>
  36. #include <process.h>
  37. #include "libavutil/internal.h"
  38. #include "libavutil/mem.h"
  39. typedef struct pthread_t {
  40. void *handle;
  41. void *(*func)(void* arg);
  42. void *arg;
  43. void *ret;
  44. } pthread_t;
  45. /* the conditional variable api for windows 6.0+ uses critical sections and
  46. * not mutexes */
  47. typedef CRITICAL_SECTION pthread_mutex_t;
  48. /* This is the CONDITIONAL_VARIABLE typedef for using Window's native
  49. * conditional variables on kernels 6.0+.
  50. * MinGW does not currently have this typedef. */
  51. typedef struct pthread_cond_t {
  52. void *ptr;
  53. } pthread_cond_t;
  54. /* function pointers to conditional variable API on windows 6.0+ kernels */
  55. #if _WIN32_WINNT < 0x0600
  56. static void (WINAPI *cond_broadcast)(pthread_cond_t *cond);
  57. static void (WINAPI *cond_init)(pthread_cond_t *cond);
  58. static void (WINAPI *cond_signal)(pthread_cond_t *cond);
  59. static BOOL (WINAPI *cond_wait)(pthread_cond_t *cond, pthread_mutex_t *mutex,
  60. DWORD milliseconds);
  61. #else
  62. #define cond_init InitializeConditionVariable
  63. #define cond_broadcast WakeAllConditionVariable
  64. #define cond_signal WakeConditionVariable
  65. #define cond_wait SleepConditionVariableCS
  66. #endif
  67. static unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
  68. {
  69. pthread_t *h = arg;
  70. h->ret = h->func(h->arg);
  71. return 0;
  72. }
  73. static int pthread_create(pthread_t *thread, const void *unused_attr,
  74. void *(*start_routine)(void*), void *arg)
  75. {
  76. thread->func = start_routine;
  77. thread->arg = arg;
  78. thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
  79. 0, NULL);
  80. return !thread->handle;
  81. }
  82. static void pthread_join(pthread_t thread, void **value_ptr)
  83. {
  84. DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
  85. if (ret != WAIT_OBJECT_0)
  86. return;
  87. if (value_ptr)
  88. *value_ptr = thread.ret;
  89. CloseHandle(thread.handle);
  90. }
  91. static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
  92. {
  93. InitializeCriticalSection(m);
  94. return 0;
  95. }
  96. static inline int pthread_mutex_destroy(pthread_mutex_t *m)
  97. {
  98. DeleteCriticalSection(m);
  99. return 0;
  100. }
  101. static inline int pthread_mutex_lock(pthread_mutex_t *m)
  102. {
  103. EnterCriticalSection(m);
  104. return 0;
  105. }
  106. static inline int pthread_mutex_unlock(pthread_mutex_t *m)
  107. {
  108. LeaveCriticalSection(m);
  109. return 0;
  110. }
  111. /* for pre-Windows 6.0 platforms we need to define and use our own condition
  112. * variable and api */
  113. typedef struct win32_cond_t {
  114. pthread_mutex_t mtx_broadcast;
  115. pthread_mutex_t mtx_waiter_count;
  116. volatile int waiter_count;
  117. HANDLE semaphore;
  118. HANDLE waiters_done;
  119. volatile int is_broadcast;
  120. } win32_cond_t;
  121. static void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
  122. {
  123. win32_cond_t *win32_cond = NULL;
  124. if (_WIN32_WINNT >= 0x0600 || cond_init) {
  125. cond_init(cond);
  126. return;
  127. }
  128. /* non native condition variables */
  129. win32_cond = av_mallocz(sizeof(win32_cond_t));
  130. if (!win32_cond)
  131. return;
  132. cond->ptr = win32_cond;
  133. win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
  134. if (!win32_cond->semaphore)
  135. return;
  136. win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
  137. if (!win32_cond->waiters_done)
  138. return;
  139. pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
  140. pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
  141. }
  142. static void pthread_cond_destroy(pthread_cond_t *cond)
  143. {
  144. win32_cond_t *win32_cond = cond->ptr;
  145. /* native condition variables do not destroy */
  146. if (_WIN32_WINNT >= 0x0600 || cond_init)
  147. return;
  148. /* non native condition variables */
  149. CloseHandle(win32_cond->semaphore);
  150. CloseHandle(win32_cond->waiters_done);
  151. pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
  152. pthread_mutex_destroy(&win32_cond->mtx_broadcast);
  153. av_freep(&win32_cond);
  154. cond->ptr = NULL;
  155. }
  156. static void pthread_cond_broadcast(pthread_cond_t *cond)
  157. {
  158. win32_cond_t *win32_cond = cond->ptr;
  159. int have_waiter;
  160. if (_WIN32_WINNT >= 0x0600 || cond_broadcast) {
  161. cond_broadcast(cond);
  162. return;
  163. }
  164. /* non native condition variables */
  165. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  166. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  167. have_waiter = 0;
  168. if (win32_cond->waiter_count) {
  169. win32_cond->is_broadcast = 1;
  170. have_waiter = 1;
  171. }
  172. if (have_waiter) {
  173. ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
  174. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  175. WaitForSingleObject(win32_cond->waiters_done, INFINITE);
  176. ResetEvent(win32_cond->waiters_done);
  177. win32_cond->is_broadcast = 0;
  178. } else
  179. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  180. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  181. }
  182. static int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  183. {
  184. win32_cond_t *win32_cond = cond->ptr;
  185. int last_waiter;
  186. if (_WIN32_WINNT >= 0x0600 || cond_wait) {
  187. cond_wait(cond, mutex, INFINITE);
  188. return 0;
  189. }
  190. /* non native condition variables */
  191. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  192. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  193. win32_cond->waiter_count++;
  194. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  195. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  196. // unlock the external mutex
  197. pthread_mutex_unlock(mutex);
  198. WaitForSingleObject(win32_cond->semaphore, INFINITE);
  199. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  200. win32_cond->waiter_count--;
  201. last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
  202. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  203. if (last_waiter)
  204. SetEvent(win32_cond->waiters_done);
  205. // lock the external mutex
  206. return pthread_mutex_lock(mutex);
  207. }
  208. static void pthread_cond_signal(pthread_cond_t *cond)
  209. {
  210. win32_cond_t *win32_cond = cond->ptr;
  211. int have_waiter;
  212. if (_WIN32_WINNT >= 0x0600 || cond_signal) {
  213. cond_signal(cond);
  214. return;
  215. }
  216. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  217. /* non-native condition variables */
  218. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  219. have_waiter = win32_cond->waiter_count;
  220. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  221. if (have_waiter) {
  222. ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
  223. WaitForSingleObject(win32_cond->waiters_done, INFINITE);
  224. ResetEvent(win32_cond->waiters_done);
  225. }
  226. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  227. }
  228. static void w32thread_init(void)
  229. {
  230. #if _WIN32_WINNT < 0x0600
  231. HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
  232. /* if one is available, then they should all be available */
  233. cond_init =
  234. (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
  235. cond_broadcast =
  236. (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
  237. cond_signal =
  238. (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
  239. cond_wait =
  240. (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
  241. #endif
  242. }
  243. #endif /* LIBAV_COMPAT_W32PTHREADS_H */