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.

298 lines
9.1KB

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