w32pthreads.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 COMPAT_W32PTHREADS_H
  28. #define 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. #include <windows.h>
  35. #include <process.h>
  36. #include <time.h>
  37. #include "libavutil/attributes.h"
  38. #include "libavutil/common.h"
  39. #include "libavutil/internal.h"
  40. #include "libavutil/mem.h"
  41. #include "libavutil/time.h"
  42. typedef struct pthread_t {
  43. void *handle;
  44. void *(*func)(void* arg);
  45. void *arg;
  46. void *ret;
  47. } *pthread_t;
  48. /* use light weight mutex/condition variable API for Windows Vista and later */
  49. typedef SRWLOCK pthread_mutex_t;
  50. typedef CONDITION_VARIABLE pthread_cond_t;
  51. #define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT
  52. #define PTHREAD_COND_INITIALIZER CONDITION_VARIABLE_INIT
  53. #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
  54. #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
  55. #define PTHREAD_CANCEL_ENABLE 1
  56. #define PTHREAD_CANCEL_DISABLE 0
  57. #if HAVE_WINRT
  58. #define THREADFUNC_RETTYPE DWORD
  59. #else
  60. #define THREADFUNC_RETTYPE unsigned
  61. #endif
  62. static av_unused THREADFUNC_RETTYPE
  63. __stdcall attribute_align_arg win32thread_worker(void *arg)
  64. {
  65. pthread_t h = (pthread_t)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. pthread_t ret;
  73. ret = av_mallocz(sizeof(*ret));
  74. if (!ret)
  75. return EAGAIN;
  76. ret->func = start_routine;
  77. ret->arg = arg;
  78. #if HAVE_WINRT
  79. ret->handle = (void*)CreateThread(NULL, 0, win32thread_worker, ret,
  80. 0, NULL);
  81. #else
  82. ret->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, ret,
  83. 0, NULL);
  84. #endif
  85. if (!ret->handle) {
  86. av_free(ret);
  87. return EAGAIN;
  88. }
  89. *thread = ret;
  90. return 0;
  91. }
  92. static av_unused int pthread_join(pthread_t thread, void **value_ptr)
  93. {
  94. DWORD ret = WaitForSingleObject(thread->handle, INFINITE);
  95. if (ret != WAIT_OBJECT_0) {
  96. if (ret == WAIT_ABANDONED)
  97. return EINVAL;
  98. else
  99. return EDEADLK;
  100. }
  101. if (value_ptr)
  102. *value_ptr = thread->ret;
  103. CloseHandle(thread->handle);
  104. av_free(thread);
  105. return 0;
  106. }
  107. static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
  108. {
  109. InitializeSRWLock(m);
  110. return 0;
  111. }
  112. static inline int pthread_mutex_destroy(pthread_mutex_t *m)
  113. {
  114. /* Unlocked SWR locks use no resources */
  115. return 0;
  116. }
  117. static inline int pthread_mutex_lock(pthread_mutex_t *m)
  118. {
  119. AcquireSRWLockExclusive(m);
  120. return 0;
  121. }
  122. static inline int pthread_mutex_unlock(pthread_mutex_t *m)
  123. {
  124. ReleaseSRWLockExclusive(m);
  125. return 0;
  126. }
  127. typedef INIT_ONCE pthread_once_t;
  128. #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
  129. static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
  130. {
  131. BOOL pending = FALSE;
  132. InitOnceBeginInitialize(once_control, 0, &pending, NULL);
  133. if (pending)
  134. init_routine();
  135. InitOnceComplete(once_control, 0, NULL);
  136. return 0;
  137. }
  138. static inline int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
  139. {
  140. InitializeConditionVariable(cond);
  141. return 0;
  142. }
  143. /* native condition variables do not destroy */
  144. static inline int pthread_cond_destroy(pthread_cond_t *cond)
  145. {
  146. return 0;
  147. }
  148. static inline int pthread_cond_broadcast(pthread_cond_t *cond)
  149. {
  150. WakeAllConditionVariable(cond);
  151. return 0;
  152. }
  153. static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  154. {
  155. SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
  156. return 0;
  157. }
  158. static inline int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  159. const struct timespec *abstime)
  160. {
  161. int64_t abs_milli = abstime->tv_sec * 1000LL + abstime->tv_nsec / 1000000;
  162. DWORD t = av_clip64(abs_milli - av_gettime() / 1000, 0, UINT32_MAX);
  163. if (!SleepConditionVariableSRW(cond, mutex, t, 0)) {
  164. DWORD err = GetLastError();
  165. if (err == ERROR_TIMEOUT)
  166. return ETIMEDOUT;
  167. else
  168. return EINVAL;
  169. }
  170. return 0;
  171. }
  172. static inline int pthread_cond_signal(pthread_cond_t *cond)
  173. {
  174. WakeConditionVariable(cond);
  175. return 0;
  176. }
  177. static inline int pthread_setcancelstate(int state, int *oldstate)
  178. {
  179. return 0;
  180. }
  181. #endif /* COMPAT_W32PTHREADS_H */