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.

184 lines
5.1KB

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