w32pthreads.h 9.2 KB

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