w32pthreads.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "libavutil/attributes.h"
  38. #include "libavutil/common.h"
  39. #include "libavutil/internal.h"
  40. #include "libavutil/mem.h"
  41. typedef struct pthread_t {
  42. void *handle;
  43. void *(*func)(void* arg);
  44. void *arg;
  45. void *ret;
  46. } pthread_t;
  47. /* use light weight mutex/condition variable API for Windows Vista and later */
  48. typedef SRWLOCK pthread_mutex_t;
  49. typedef CONDITION_VARIABLE pthread_cond_t;
  50. #define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT
  51. #define PTHREAD_COND_INITIALIZER CONDITION_VARIABLE_INIT
  52. #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
  53. #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
  54. static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
  55. {
  56. pthread_t *h = (pthread_t*)arg;
  57. h->ret = h->func(h->arg);
  58. return 0;
  59. }
  60. static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
  61. void *(*start_routine)(void*), void *arg)
  62. {
  63. thread->func = start_routine;
  64. thread->arg = arg;
  65. #if HAVE_WINRT
  66. thread->handle = (void*)CreateThread(NULL, 0, win32thread_worker, thread,
  67. 0, NULL);
  68. #else
  69. thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
  70. 0, NULL);
  71. #endif
  72. return !thread->handle;
  73. }
  74. static av_unused int pthread_join(pthread_t thread, void **value_ptr)
  75. {
  76. DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
  77. if (ret != WAIT_OBJECT_0) {
  78. if (ret == WAIT_ABANDONED)
  79. return EINVAL;
  80. else
  81. return EDEADLK;
  82. }
  83. if (value_ptr)
  84. *value_ptr = thread.ret;
  85. CloseHandle(thread.handle);
  86. return 0;
  87. }
  88. static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
  89. {
  90. InitializeSRWLock(m);
  91. return 0;
  92. }
  93. static inline int pthread_mutex_destroy(pthread_mutex_t *m)
  94. {
  95. /* Unlocked SWR locks use no resources */
  96. return 0;
  97. }
  98. static inline int pthread_mutex_lock(pthread_mutex_t *m)
  99. {
  100. AcquireSRWLockExclusive(m);
  101. return 0;
  102. }
  103. static inline int pthread_mutex_unlock(pthread_mutex_t *m)
  104. {
  105. ReleaseSRWLockExclusive(m);
  106. return 0;
  107. }
  108. typedef INIT_ONCE pthread_once_t;
  109. #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
  110. static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
  111. {
  112. BOOL pending = FALSE;
  113. InitOnceBeginInitialize(once_control, 0, &pending, NULL);
  114. if (pending)
  115. init_routine();
  116. InitOnceComplete(once_control, 0, NULL);
  117. return 0;
  118. }
  119. static inline int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
  120. {
  121. InitializeConditionVariable(cond);
  122. return 0;
  123. }
  124. /* native condition variables do not destroy */
  125. static inline int pthread_cond_destroy(pthread_cond_t *cond)
  126. {
  127. return 0;
  128. }
  129. static inline int pthread_cond_broadcast(pthread_cond_t *cond)
  130. {
  131. WakeAllConditionVariable(cond);
  132. return 0;
  133. }
  134. static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  135. {
  136. SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
  137. return 0;
  138. }
  139. static inline int pthread_cond_signal(pthread_cond_t *cond)
  140. {
  141. WakeConditionVariable(cond);
  142. return 0;
  143. }
  144. #endif /* COMPAT_W32PTHREADS_H */