w32pthreads.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. thread->func = start_routine;
  73. thread->arg = arg;
  74. #if HAVE_WINRT
  75. thread->handle = (void*)CreateThread(NULL, 0, win32thread_worker, thread,
  76. 0, NULL);
  77. #else
  78. thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
  79. 0, NULL);
  80. #endif
  81. return !thread->handle;
  82. }
  83. static av_unused int pthread_join(pthread_t thread, void **value_ptr)
  84. {
  85. DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
  86. if (ret != WAIT_OBJECT_0) {
  87. if (ret == WAIT_ABANDONED)
  88. return EINVAL;
  89. else
  90. return EDEADLK;
  91. }
  92. if (value_ptr)
  93. *value_ptr = thread.ret;
  94. CloseHandle(thread.handle);
  95. return 0;
  96. }
  97. static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
  98. {
  99. InitializeSRWLock(m);
  100. return 0;
  101. }
  102. static inline int pthread_mutex_destroy(pthread_mutex_t *m)
  103. {
  104. /* Unlocked SWR locks use no resources */
  105. return 0;
  106. }
  107. static inline int pthread_mutex_lock(pthread_mutex_t *m)
  108. {
  109. AcquireSRWLockExclusive(m);
  110. return 0;
  111. }
  112. static inline int pthread_mutex_unlock(pthread_mutex_t *m)
  113. {
  114. ReleaseSRWLockExclusive(m);
  115. return 0;
  116. }
  117. typedef INIT_ONCE pthread_once_t;
  118. #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
  119. static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
  120. {
  121. BOOL pending = FALSE;
  122. InitOnceBeginInitialize(once_control, 0, &pending, NULL);
  123. if (pending)
  124. init_routine();
  125. InitOnceComplete(once_control, 0, NULL);
  126. return 0;
  127. }
  128. static inline int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
  129. {
  130. InitializeConditionVariable(cond);
  131. return 0;
  132. }
  133. /* native condition variables do not destroy */
  134. static inline int pthread_cond_destroy(pthread_cond_t *cond)
  135. {
  136. return 0;
  137. }
  138. static inline int pthread_cond_broadcast(pthread_cond_t *cond)
  139. {
  140. WakeAllConditionVariable(cond);
  141. return 0;
  142. }
  143. static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  144. {
  145. SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
  146. return 0;
  147. }
  148. static inline int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  149. const struct timespec *abstime)
  150. {
  151. int64_t abs_milli = abstime->tv_sec * 1000LL + abstime->tv_nsec / 1000000;
  152. DWORD t = av_clip64(abs_milli - av_gettime() / 1000, 0, UINT32_MAX);
  153. if (!SleepConditionVariableSRW(cond, mutex, t, 0)) {
  154. DWORD err = GetLastError();
  155. if (err == ERROR_TIMEOUT)
  156. return ETIMEDOUT;
  157. else
  158. return EINVAL;
  159. }
  160. return 0;
  161. }
  162. static inline int pthread_cond_signal(pthread_cond_t *cond)
  163. {
  164. WakeConditionVariable(cond);
  165. return 0;
  166. }
  167. static inline int pthread_setcancelstate(int state, int *oldstate)
  168. {
  169. return 0;
  170. }
  171. #endif /* COMPAT_W32PTHREADS_H */