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.

327 lines
9.3KB

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