w32pthreads.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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/attributes.h"
  38. #include "libavutil/internal.h"
  39. #include "libavutil/mem.h"
  40. typedef struct pthread_t {
  41. void *handle;
  42. void *(*func)(void* arg);
  43. void *arg;
  44. void *ret;
  45. } pthread_t;
  46. /* the conditional variable api for windows 6.0+ uses critical sections and
  47. * not mutexes */
  48. typedef CRITICAL_SECTION pthread_mutex_t;
  49. /* This is the CONDITION_VARIABLE typedef for using Windows' native
  50. * conditional variables on kernels 6.0+. */
  51. #if HAVE_CONDITION_VARIABLE_PTR
  52. typedef CONDITION_VARIABLE pthread_cond_t;
  53. #else
  54. typedef struct pthread_cond_t {
  55. void *Ptr;
  56. } pthread_cond_t;
  57. #endif
  58. #if _WIN32_WINNT >= 0x0600
  59. #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
  60. #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
  61. #endif
  62. static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
  63. {
  64. pthread_t *h = arg;
  65. h->ret = h->func(h->arg);
  66. return 0;
  67. }
  68. static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
  69. void *(*start_routine)(void*), void *arg)
  70. {
  71. thread->func = start_routine;
  72. thread->arg = arg;
  73. thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
  74. 0, NULL);
  75. return !thread->handle;
  76. }
  77. static av_unused void pthread_join(pthread_t thread, void **value_ptr)
  78. {
  79. DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
  80. if (ret != WAIT_OBJECT_0)
  81. return;
  82. if (value_ptr)
  83. *value_ptr = thread.ret;
  84. CloseHandle(thread.handle);
  85. }
  86. static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
  87. {
  88. InitializeCriticalSection(m);
  89. return 0;
  90. }
  91. static inline int pthread_mutex_destroy(pthread_mutex_t *m)
  92. {
  93. DeleteCriticalSection(m);
  94. return 0;
  95. }
  96. static inline int pthread_mutex_lock(pthread_mutex_t *m)
  97. {
  98. EnterCriticalSection(m);
  99. return 0;
  100. }
  101. static inline int pthread_mutex_unlock(pthread_mutex_t *m)
  102. {
  103. LeaveCriticalSection(m);
  104. return 0;
  105. }
  106. #if _WIN32_WINNT >= 0x0600
  107. typedef INIT_ONCE pthread_once_t;
  108. #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
  109. static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
  110. {
  111. BOOL pending = FALSE;
  112. InitOnceBeginInitialize(once_control, 0, &pending, NULL);
  113. if (pending)
  114. init_routine();
  115. InitOnceComplete(once_control, 0, NULL);
  116. return 0;
  117. }
  118. static inline void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
  119. {
  120. InitializeConditionVariable(cond);
  121. }
  122. /* native condition variables do not destroy */
  123. static inline void pthread_cond_destroy(pthread_cond_t *cond)
  124. {
  125. return;
  126. }
  127. static inline void pthread_cond_broadcast(pthread_cond_t *cond)
  128. {
  129. WakeAllConditionVariable(cond);
  130. }
  131. static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  132. {
  133. SleepConditionVariableCS(cond, mutex, INFINITE);
  134. return 0;
  135. }
  136. static inline void pthread_cond_signal(pthread_cond_t *cond)
  137. {
  138. WakeConditionVariable(cond);
  139. }
  140. #else // _WIN32_WINNT < 0x0600
  141. /* for pre-Windows 6.0 platforms, define INIT_ONCE struct,
  142. * compatible to the one used in the native API */
  143. typedef union pthread_once_t {
  144. void * Ptr; ///< For the Windows 6.0+ native functions
  145. LONG state; ///< For the pre-Windows 6.0 compat code
  146. } pthread_once_t;
  147. #define PTHREAD_ONCE_INIT {0}
  148. /* function pointers to init once API on windows 6.0+ kernels */
  149. static BOOL (WINAPI *initonce_begin)(pthread_once_t *lpInitOnce, DWORD dwFlags, BOOL *fPending, void **lpContext);
  150. static BOOL (WINAPI *initonce_complete)(pthread_once_t *lpInitOnce, DWORD dwFlags, void *lpContext);
  151. /* pre-Windows 6.0 compat using a spin-lock */
  152. static inline void w32thread_once_fallback(LONG volatile *state, void (*init_routine)(void))
  153. {
  154. switch (InterlockedCompareExchange(state, 1, 0)) {
  155. /* Initial run */
  156. case 0:
  157. init_routine();
  158. InterlockedExchange(state, 2);
  159. break;
  160. /* Another thread is running init */
  161. case 1:
  162. while (1) {
  163. MemoryBarrier();
  164. if (*state == 2)
  165. break;
  166. Sleep(0);
  167. }
  168. break;
  169. /* Initialization complete */
  170. case 2:
  171. break;
  172. }
  173. }
  174. static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
  175. {
  176. /* Use native functions on Windows 6.0+ */
  177. if (initonce_begin && initonce_complete) {
  178. BOOL pending = FALSE;
  179. initonce_begin(once_control, 0, &pending, NULL);
  180. if (pending)
  181. init_routine();
  182. initonce_complete(once_control, 0, NULL);
  183. return 0;
  184. }
  185. w32thread_once_fallback(&once_control->state, init_routine);
  186. return 0;
  187. }
  188. /* for pre-Windows 6.0 platforms we need to define and use our own condition
  189. * variable and api */
  190. typedef struct win32_cond_t {
  191. pthread_mutex_t mtx_broadcast;
  192. pthread_mutex_t mtx_waiter_count;
  193. volatile int waiter_count;
  194. HANDLE semaphore;
  195. HANDLE waiters_done;
  196. volatile int is_broadcast;
  197. } win32_cond_t;
  198. /* function pointers to conditional variable API on windows 6.0+ kernels */
  199. static void (WINAPI *cond_broadcast)(pthread_cond_t *cond);
  200. static void (WINAPI *cond_init)(pthread_cond_t *cond);
  201. static void (WINAPI *cond_signal)(pthread_cond_t *cond);
  202. static BOOL (WINAPI *cond_wait)(pthread_cond_t *cond, pthread_mutex_t *mutex,
  203. DWORD milliseconds);
  204. static av_unused void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
  205. {
  206. win32_cond_t *win32_cond = NULL;
  207. if (cond_init) {
  208. cond_init(cond);
  209. return;
  210. }
  211. /* non native condition variables */
  212. win32_cond = av_mallocz(sizeof(win32_cond_t));
  213. if (!win32_cond)
  214. return;
  215. cond->Ptr = win32_cond;
  216. win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
  217. if (!win32_cond->semaphore)
  218. return;
  219. win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
  220. if (!win32_cond->waiters_done)
  221. return;
  222. pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
  223. pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
  224. }
  225. static av_unused void pthread_cond_destroy(pthread_cond_t *cond)
  226. {
  227. win32_cond_t *win32_cond = cond->Ptr;
  228. /* native condition variables do not destroy */
  229. if (cond_init)
  230. return;
  231. /* non native condition variables */
  232. CloseHandle(win32_cond->semaphore);
  233. CloseHandle(win32_cond->waiters_done);
  234. pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
  235. pthread_mutex_destroy(&win32_cond->mtx_broadcast);
  236. av_freep(&win32_cond);
  237. cond->Ptr = NULL;
  238. }
  239. static av_unused void pthread_cond_broadcast(pthread_cond_t *cond)
  240. {
  241. win32_cond_t *win32_cond = cond->Ptr;
  242. int have_waiter;
  243. if (cond_broadcast) {
  244. cond_broadcast(cond);
  245. return;
  246. }
  247. /* non native condition variables */
  248. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  249. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  250. have_waiter = 0;
  251. if (win32_cond->waiter_count) {
  252. win32_cond->is_broadcast = 1;
  253. have_waiter = 1;
  254. }
  255. if (have_waiter) {
  256. ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
  257. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  258. WaitForSingleObject(win32_cond->waiters_done, INFINITE);
  259. ResetEvent(win32_cond->waiters_done);
  260. win32_cond->is_broadcast = 0;
  261. } else
  262. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  263. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  264. }
  265. static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  266. {
  267. win32_cond_t *win32_cond = cond->Ptr;
  268. int last_waiter;
  269. if (cond_wait) {
  270. cond_wait(cond, mutex, INFINITE);
  271. return 0;
  272. }
  273. /* non native condition variables */
  274. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  275. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  276. win32_cond->waiter_count++;
  277. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  278. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  279. // unlock the external mutex
  280. pthread_mutex_unlock(mutex);
  281. WaitForSingleObject(win32_cond->semaphore, INFINITE);
  282. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  283. win32_cond->waiter_count--;
  284. last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
  285. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  286. if (last_waiter)
  287. SetEvent(win32_cond->waiters_done);
  288. // lock the external mutex
  289. return pthread_mutex_lock(mutex);
  290. }
  291. static av_unused void pthread_cond_signal(pthread_cond_t *cond)
  292. {
  293. win32_cond_t *win32_cond = cond->Ptr;
  294. int have_waiter;
  295. if (cond_signal) {
  296. cond_signal(cond);
  297. return;
  298. }
  299. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  300. /* non-native condition variables */
  301. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  302. have_waiter = win32_cond->waiter_count;
  303. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  304. if (have_waiter) {
  305. ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
  306. WaitForSingleObject(win32_cond->waiters_done, INFINITE);
  307. ResetEvent(win32_cond->waiters_done);
  308. }
  309. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  310. }
  311. #endif
  312. static av_unused void w32thread_init(void)
  313. {
  314. #if _WIN32_WINNT < 0x0600
  315. HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
  316. /* if one is available, then they should all be available */
  317. cond_init =
  318. (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
  319. cond_broadcast =
  320. (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
  321. cond_signal =
  322. (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
  323. cond_wait =
  324. (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
  325. initonce_begin =
  326. (void*)GetProcAddress(kernel_dll, "InitOnceBeginInitialize");
  327. initonce_complete =
  328. (void*)GetProcAddress(kernel_dll, "InitOnceComplete");
  329. #endif
  330. }
  331. #endif /* LIBAV_COMPAT_W32PTHREADS_H */