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.

269 lines
7.9KB

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