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.

271 lines
8.0KB

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