w32pthreads.h 9.1 KB

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