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.

314 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. #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. static inline void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
  108. {
  109. InitializeConditionVariable(cond);
  110. }
  111. /* native condition variables do not destroy */
  112. static inline void pthread_cond_destroy(pthread_cond_t *cond)
  113. {
  114. return;
  115. }
  116. static inline void pthread_cond_broadcast(pthread_cond_t *cond)
  117. {
  118. WakeAllConditionVariable(cond);
  119. }
  120. static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  121. {
  122. SleepConditionVariableCS(cond, mutex, INFINITE);
  123. return 0;
  124. }
  125. static inline void pthread_cond_signal(pthread_cond_t *cond)
  126. {
  127. WakeConditionVariable(cond);
  128. }
  129. #else // _WIN32_WINNT < 0x0600
  130. /* for pre-Windows 6.0 platforms we need to define and use our own condition
  131. * variable and api */
  132. typedef struct win32_cond_t {
  133. pthread_mutex_t mtx_broadcast;
  134. pthread_mutex_t mtx_waiter_count;
  135. volatile int waiter_count;
  136. HANDLE semaphore;
  137. HANDLE waiters_done;
  138. volatile int is_broadcast;
  139. } win32_cond_t;
  140. /* function pointers to conditional variable API on windows 6.0+ kernels */
  141. static void (WINAPI *cond_broadcast)(pthread_cond_t *cond);
  142. static void (WINAPI *cond_init)(pthread_cond_t *cond);
  143. static void (WINAPI *cond_signal)(pthread_cond_t *cond);
  144. static BOOL (WINAPI *cond_wait)(pthread_cond_t *cond, pthread_mutex_t *mutex,
  145. DWORD milliseconds);
  146. static av_unused void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
  147. {
  148. win32_cond_t *win32_cond = NULL;
  149. if (cond_init) {
  150. cond_init(cond);
  151. return;
  152. }
  153. /* non native condition variables */
  154. win32_cond = av_mallocz(sizeof(win32_cond_t));
  155. if (!win32_cond)
  156. return;
  157. cond->Ptr = win32_cond;
  158. win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
  159. if (!win32_cond->semaphore)
  160. return;
  161. win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
  162. if (!win32_cond->waiters_done)
  163. return;
  164. pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
  165. pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
  166. }
  167. static av_unused void pthread_cond_destroy(pthread_cond_t *cond)
  168. {
  169. win32_cond_t *win32_cond = cond->Ptr;
  170. /* native condition variables do not destroy */
  171. if (cond_init)
  172. return;
  173. /* non native condition variables */
  174. CloseHandle(win32_cond->semaphore);
  175. CloseHandle(win32_cond->waiters_done);
  176. pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
  177. pthread_mutex_destroy(&win32_cond->mtx_broadcast);
  178. av_freep(&win32_cond);
  179. cond->Ptr = NULL;
  180. }
  181. static av_unused void pthread_cond_broadcast(pthread_cond_t *cond)
  182. {
  183. win32_cond_t *win32_cond = cond->Ptr;
  184. int have_waiter;
  185. if (cond_broadcast) {
  186. cond_broadcast(cond);
  187. return;
  188. }
  189. /* non native condition variables */
  190. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  191. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  192. have_waiter = 0;
  193. if (win32_cond->waiter_count) {
  194. win32_cond->is_broadcast = 1;
  195. have_waiter = 1;
  196. }
  197. if (have_waiter) {
  198. ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
  199. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  200. WaitForSingleObject(win32_cond->waiters_done, INFINITE);
  201. ResetEvent(win32_cond->waiters_done);
  202. win32_cond->is_broadcast = 0;
  203. } else
  204. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  205. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  206. }
  207. static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  208. {
  209. win32_cond_t *win32_cond = cond->Ptr;
  210. int last_waiter;
  211. if (cond_wait) {
  212. cond_wait(cond, mutex, INFINITE);
  213. return 0;
  214. }
  215. /* non native condition variables */
  216. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  217. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  218. win32_cond->waiter_count++;
  219. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  220. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  221. // unlock the external mutex
  222. pthread_mutex_unlock(mutex);
  223. WaitForSingleObject(win32_cond->semaphore, INFINITE);
  224. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  225. win32_cond->waiter_count--;
  226. last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
  227. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  228. if (last_waiter)
  229. SetEvent(win32_cond->waiters_done);
  230. // lock the external mutex
  231. return pthread_mutex_lock(mutex);
  232. }
  233. static av_unused void pthread_cond_signal(pthread_cond_t *cond)
  234. {
  235. win32_cond_t *win32_cond = cond->Ptr;
  236. int have_waiter;
  237. if (cond_signal) {
  238. cond_signal(cond);
  239. return;
  240. }
  241. pthread_mutex_lock(&win32_cond->mtx_broadcast);
  242. /* non-native condition variables */
  243. pthread_mutex_lock(&win32_cond->mtx_waiter_count);
  244. have_waiter = win32_cond->waiter_count;
  245. pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
  246. if (have_waiter) {
  247. ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
  248. WaitForSingleObject(win32_cond->waiters_done, INFINITE);
  249. ResetEvent(win32_cond->waiters_done);
  250. }
  251. pthread_mutex_unlock(&win32_cond->mtx_broadcast);
  252. }
  253. #endif
  254. static av_unused void w32thread_init(void)
  255. {
  256. #if _WIN32_WINNT < 0x0600
  257. HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
  258. /* if one is available, then they should all be available */
  259. cond_init =
  260. (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
  261. cond_broadcast =
  262. (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
  263. cond_signal =
  264. (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
  265. cond_wait =
  266. (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
  267. #endif
  268. }
  269. #endif /* LIBAV_COMPAT_W32PTHREADS_H */