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.

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