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.

294 lines
8.9KB

  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. #define CreateEvent(a, reset, init, name) \
  67. CreateEventEx(a, name, \
  68. (reset ? CREATE_EVENT_MANUAL_RESET : 0) | \
  69. (init ? CREATE_EVENT_INITIAL_SET : 0), \
  70. EVENT_ALL_ACCESS)
  71. // CreateSemaphoreExA seems to be desktop-only, but as long as we don't
  72. // use named semaphores, it doesn't matter if we use the W version.
  73. #define CreateSemaphore(a, b, c, d) \
  74. CreateSemaphoreExW(a, b, c, d, 0, SEMAPHORE_ALL_ACCESS)
  75. #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
  76. #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
  77. #endif
  78. static unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
  79. {
  80. pthread_t *h = arg;
  81. h->ret = h->func(h->arg);
  82. return 0;
  83. }
  84. static int pthread_create(pthread_t *thread, const void *unused_attr,
  85. void *(*start_routine)(void*), void *arg)
  86. {
  87. thread->func = start_routine;
  88. thread->arg = arg;
  89. thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
  90. 0, NULL);
  91. return !thread->handle;
  92. }
  93. static void pthread_join(pthread_t thread, void **value_ptr)
  94. {
  95. DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
  96. if (ret != WAIT_OBJECT_0)
  97. return;
  98. if (value_ptr)
  99. *value_ptr = thread.ret;
  100. CloseHandle(thread.handle);
  101. }
  102. static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
  103. {
  104. InitializeCriticalSection(m);
  105. return 0;
  106. }
  107. static inline int pthread_mutex_destroy(pthread_mutex_t *m)
  108. {
  109. DeleteCriticalSection(m);
  110. return 0;
  111. }
  112. static inline int pthread_mutex_lock(pthread_mutex_t *m)
  113. {
  114. EnterCriticalSection(m);
  115. return 0;
  116. }
  117. static inline int pthread_mutex_unlock(pthread_mutex_t *m)
  118. {
  119. LeaveCriticalSection(m);
  120. return 0;
  121. }
  122. /* for pre-Windows 6.0 platforms we need to define and use our own condition
  123. * variable and api */
  124. typedef struct win32_cond_t {
  125. pthread_mutex_t mtx_broadcast;
  126. pthread_mutex_t mtx_waiter_count;
  127. volatile int waiter_count;
  128. HANDLE semaphore;
  129. HANDLE waiters_done;
  130. volatile int is_broadcast;
  131. } win32_cond_t;
  132. static void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
  133. {
  134. win32_cond_t *win32_cond = NULL;
  135. if (cond_init) {
  136. cond_init(cond);
  137. return;
  138. }
  139. /* non native condition variables */
  140. win32_cond = av_mallocz(sizeof(win32_cond_t));
  141. if (!win32_cond)
  142. return;
  143. cond->ptr = win32_cond;
  144. win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
  145. if (!win32_cond->semaphore)
  146. return;
  147. win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
  148. if (!win32_cond->waiters_done)
  149. return;
  150. pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
  151. pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
  152. }
  153. static void pthread_cond_destroy(pthread_cond_t *cond)
  154. {
  155. win32_cond_t *win32_cond = cond->ptr;
  156. /* native condition variables do not destroy */
  157. if (cond_init)
  158. return;
  159. /* non native condition variables */
  160. CloseHandle(win32_cond->semaphore);
  161. CloseHandle(win32_cond->waiters_done);
  162. pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
  163. pthread_mutex_destroy(&win32_cond->mtx_broadcast);
  164. av_freep(&win32_cond);
  165. cond->ptr = NULL;
  166. }
  167. static void pthread_cond_broadcast(pthread_cond_t *cond)
  168. {
  169. win32_cond_t *win32_cond = cond->ptr;
  170. int have_waiter;
  171. if (cond_broadcast) {
  172. cond_broadcast(cond);
  173. return;
  174. }
  175. /* non native condition variables */
  176. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  177. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  178. have_waiter = 0;
  179. if (win32_cond->waiter_count) {
  180. win32_cond->is_broadcast = 1;
  181. have_waiter = 1;
  182. }
  183. if (have_waiter) {
  184. ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
  185. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  186. WaitForSingleObject(win32_cond->waiters_done, INFINITE);
  187. ResetEvent(win32_cond->waiters_done);
  188. win32_cond->is_broadcast = 0;
  189. } else
  190. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  191. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  192. }
  193. static int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  194. {
  195. win32_cond_t *win32_cond = cond->ptr;
  196. int last_waiter;
  197. if (cond_wait) {
  198. cond_wait(cond, mutex, INFINITE);
  199. return 0;
  200. }
  201. /* non native condition variables */
  202. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  203. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  204. win32_cond->waiter_count++;
  205. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  206. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  207. // unlock the external mutex
  208. pthread_mutex_unlock(mutex);
  209. WaitForSingleObject(win32_cond->semaphore, INFINITE);
  210. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  211. win32_cond->waiter_count--;
  212. last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
  213. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  214. if (last_waiter)
  215. SetEvent(win32_cond->waiters_done);
  216. // lock the external mutex
  217. return pthread_mutex_lock(mutex);
  218. }
  219. static void pthread_cond_signal(pthread_cond_t *cond)
  220. {
  221. win32_cond_t *win32_cond = cond->ptr;
  222. int have_waiter;
  223. if (cond_signal) {
  224. cond_signal(cond);
  225. return;
  226. }
  227. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  228. /* non-native condition variables */
  229. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  230. have_waiter = win32_cond->waiter_count;
  231. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  232. if (have_waiter) {
  233. ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
  234. WaitForSingleObject(win32_cond->waiters_done, INFINITE);
  235. ResetEvent(win32_cond->waiters_done);
  236. }
  237. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  238. }
  239. static void w32thread_init(void)
  240. {
  241. #if _WIN32_WINNT < 0x0600
  242. HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
  243. /* if one is available, then they should all be available */
  244. cond_init =
  245. (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
  246. cond_broadcast =
  247. (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
  248. cond_signal =
  249. (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
  250. cond_wait =
  251. (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
  252. #endif
  253. }
  254. #endif /* LIBAV_COMPAT_W32PTHREADS_H */