You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

397 lines
12KB

  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. /* atomic init state of dynamically loaded functions */
  142. static LONG w32thread_init_state = 0;
  143. static av_unused void w32thread_init(void);
  144. /* for pre-Windows 6.0 platforms, define INIT_ONCE struct,
  145. * compatible to the one used in the native API */
  146. typedef union pthread_once_t {
  147. void * Ptr; ///< For the Windows 6.0+ native functions
  148. LONG state; ///< For the pre-Windows 6.0 compat code
  149. } pthread_once_t;
  150. #define PTHREAD_ONCE_INIT {0}
  151. /* function pointers to init once API on windows 6.0+ kernels */
  152. static BOOL (WINAPI *initonce_begin)(pthread_once_t *lpInitOnce, DWORD dwFlags, BOOL *fPending, void **lpContext);
  153. static BOOL (WINAPI *initonce_complete)(pthread_once_t *lpInitOnce, DWORD dwFlags, void *lpContext);
  154. /* pre-Windows 6.0 compat using a spin-lock */
  155. static inline void w32thread_once_fallback(LONG volatile *state, void (*init_routine)(void))
  156. {
  157. switch (InterlockedCompareExchange(state, 1, 0)) {
  158. /* Initial run */
  159. case 0:
  160. init_routine();
  161. InterlockedExchange(state, 2);
  162. break;
  163. /* Another thread is running init */
  164. case 1:
  165. while (1) {
  166. MemoryBarrier();
  167. if (*state == 2)
  168. break;
  169. Sleep(0);
  170. }
  171. break;
  172. /* Initialization complete */
  173. case 2:
  174. break;
  175. }
  176. }
  177. static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
  178. {
  179. w32thread_once_fallback(&w32thread_init_state, w32thread_init);
  180. /* Use native functions on Windows 6.0+ */
  181. if (initonce_begin && initonce_complete) {
  182. BOOL pending = FALSE;
  183. initonce_begin(once_control, 0, &pending, NULL);
  184. if (pending)
  185. init_routine();
  186. initonce_complete(once_control, 0, NULL);
  187. return 0;
  188. }
  189. w32thread_once_fallback(&once_control->state, init_routine);
  190. return 0;
  191. }
  192. /* for pre-Windows 6.0 platforms we need to define and use our own condition
  193. * variable and api */
  194. typedef struct win32_cond_t {
  195. pthread_mutex_t mtx_broadcast;
  196. pthread_mutex_t mtx_waiter_count;
  197. volatile int waiter_count;
  198. HANDLE semaphore;
  199. HANDLE waiters_done;
  200. volatile int is_broadcast;
  201. } win32_cond_t;
  202. /* function pointers to conditional variable API on windows 6.0+ kernels */
  203. static void (WINAPI *cond_broadcast)(pthread_cond_t *cond);
  204. static void (WINAPI *cond_init)(pthread_cond_t *cond);
  205. static void (WINAPI *cond_signal)(pthread_cond_t *cond);
  206. static BOOL (WINAPI *cond_wait)(pthread_cond_t *cond, pthread_mutex_t *mutex,
  207. DWORD milliseconds);
  208. static av_unused void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
  209. {
  210. win32_cond_t *win32_cond = NULL;
  211. w32thread_once_fallback(&w32thread_init_state, w32thread_init);
  212. if (cond_init) {
  213. cond_init(cond);
  214. return;
  215. }
  216. /* non native condition variables */
  217. win32_cond = av_mallocz(sizeof(win32_cond_t));
  218. if (!win32_cond)
  219. return;
  220. cond->Ptr = win32_cond;
  221. win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
  222. if (!win32_cond->semaphore)
  223. return;
  224. win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
  225. if (!win32_cond->waiters_done)
  226. return;
  227. pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
  228. pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
  229. }
  230. static av_unused void pthread_cond_destroy(pthread_cond_t *cond)
  231. {
  232. win32_cond_t *win32_cond = cond->Ptr;
  233. /* native condition variables do not destroy */
  234. if (cond_init)
  235. return;
  236. /* non native condition variables */
  237. CloseHandle(win32_cond->semaphore);
  238. CloseHandle(win32_cond->waiters_done);
  239. pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
  240. pthread_mutex_destroy(&win32_cond->mtx_broadcast);
  241. av_freep(&win32_cond);
  242. cond->Ptr = NULL;
  243. }
  244. static av_unused void pthread_cond_broadcast(pthread_cond_t *cond)
  245. {
  246. win32_cond_t *win32_cond = cond->Ptr;
  247. int have_waiter;
  248. if (cond_broadcast) {
  249. cond_broadcast(cond);
  250. return;
  251. }
  252. /* non native condition variables */
  253. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  254. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  255. have_waiter = 0;
  256. if (win32_cond->waiter_count) {
  257. win32_cond->is_broadcast = 1;
  258. have_waiter = 1;
  259. }
  260. if (have_waiter) {
  261. ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
  262. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  263. WaitForSingleObject(win32_cond->waiters_done, INFINITE);
  264. ResetEvent(win32_cond->waiters_done);
  265. win32_cond->is_broadcast = 0;
  266. } else
  267. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  268. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  269. }
  270. static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  271. {
  272. win32_cond_t *win32_cond = cond->Ptr;
  273. int last_waiter;
  274. if (cond_wait) {
  275. cond_wait(cond, mutex, INFINITE);
  276. return 0;
  277. }
  278. /* non native condition variables */
  279. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  280. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  281. win32_cond->waiter_count++;
  282. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  283. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  284. // unlock the external mutex
  285. pthread_mutex_unlock(mutex);
  286. WaitForSingleObject(win32_cond->semaphore, INFINITE);
  287. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  288. win32_cond->waiter_count--;
  289. last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
  290. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  291. if (last_waiter)
  292. SetEvent(win32_cond->waiters_done);
  293. // lock the external mutex
  294. return pthread_mutex_lock(mutex);
  295. }
  296. static av_unused void pthread_cond_signal(pthread_cond_t *cond)
  297. {
  298. win32_cond_t *win32_cond = cond->Ptr;
  299. int have_waiter;
  300. if (cond_signal) {
  301. cond_signal(cond);
  302. return;
  303. }
  304. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  305. /* non-native condition variables */
  306. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  307. have_waiter = win32_cond->waiter_count;
  308. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  309. if (have_waiter) {
  310. ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
  311. WaitForSingleObject(win32_cond->waiters_done, INFINITE);
  312. ResetEvent(win32_cond->waiters_done);
  313. }
  314. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  315. }
  316. #endif
  317. static av_unused void w32thread_init(void)
  318. {
  319. #if _WIN32_WINNT < 0x0600
  320. HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
  321. /* if one is available, then they should all be available */
  322. cond_init =
  323. (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
  324. cond_broadcast =
  325. (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
  326. cond_signal =
  327. (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
  328. cond_wait =
  329. (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
  330. initonce_begin =
  331. (void*)GetProcAddress(kernel_dll, "InitOnceBeginInitialize");
  332. initonce_complete =
  333. (void*)GetProcAddress(kernel_dll, "InitOnceComplete");
  334. #endif
  335. }
  336. #endif /* LIBAV_COMPAT_W32PTHREADS_H */