w32pthreads.h 12 KB

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