w32pthreads.h 5.2 KB

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