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.

153 lines
4.2KB

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