os2threads.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2011-2017 KO Myung-Hun <komh@chollian.net>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * os2threads to pthreads wrapper
  23. */
  24. #ifndef COMPAT_OS2THREADS_H
  25. #define COMPAT_OS2THREADS_H
  26. #define INCL_DOS
  27. #define INCL_DOSERRORS
  28. #include <os2.h>
  29. #undef __STRICT_ANSI__ /* for _beginthread() */
  30. #include <stdlib.h>
  31. #include <time.h>
  32. #include <sys/builtin.h>
  33. #include <sys/fmutex.h>
  34. #include "libavutil/attributes.h"
  35. #include "libavutil/common.h"
  36. #include "libavutil/time.h"
  37. typedef struct {
  38. TID tid;
  39. void *(*start_routine)(void *);
  40. void *arg;
  41. void *result;
  42. } pthread_t;
  43. typedef void pthread_attr_t;
  44. typedef _fmutex pthread_mutex_t;
  45. typedef void pthread_mutexattr_t;
  46. #define PTHREAD_MUTEX_INITIALIZER _FMUTEX_INITIALIZER
  47. typedef struct {
  48. HEV event_sem;
  49. HEV ack_sem;
  50. volatile unsigned wait_count;
  51. } pthread_cond_t;
  52. typedef void pthread_condattr_t;
  53. typedef struct {
  54. volatile int done;
  55. _fmutex mtx;
  56. } pthread_once_t;
  57. #define PTHREAD_ONCE_INIT {0, _FMUTEX_INITIALIZER}
  58. static void thread_entry(void *arg)
  59. {
  60. pthread_t *thread = arg;
  61. thread->result = thread->start_routine(thread->arg);
  62. }
  63. static av_always_inline int pthread_create(pthread_t *thread,
  64. const pthread_attr_t *attr,
  65. void *(*start_routine)(void*),
  66. void *arg)
  67. {
  68. thread->start_routine = start_routine;
  69. thread->arg = arg;
  70. thread->result = NULL;
  71. thread->tid = _beginthread(thread_entry, NULL, 1024 * 1024, thread);
  72. return 0;
  73. }
  74. static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
  75. {
  76. DosWaitThread(&thread.tid, DCWW_WAIT);
  77. if (value_ptr)
  78. *value_ptr = thread.result;
  79. return 0;
  80. }
  81. static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex,
  82. const pthread_mutexattr_t *attr)
  83. {
  84. _fmutex_create(mutex, 0);
  85. return 0;
  86. }
  87. static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
  88. {
  89. _fmutex_close(mutex);
  90. return 0;
  91. }
  92. static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
  93. {
  94. _fmutex_request(mutex, 0);
  95. return 0;
  96. }
  97. static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
  98. {
  99. _fmutex_release(mutex);
  100. return 0;
  101. }
  102. static av_always_inline int pthread_cond_init(pthread_cond_t *cond,
  103. const pthread_condattr_t *attr)
  104. {
  105. DosCreateEventSem(NULL, &cond->event_sem, DCE_POSTONE, FALSE);
  106. DosCreateEventSem(NULL, &cond->ack_sem, DCE_POSTONE, FALSE);
  107. cond->wait_count = 0;
  108. return 0;
  109. }
  110. static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
  111. {
  112. DosCloseEventSem(cond->event_sem);
  113. DosCloseEventSem(cond->ack_sem);
  114. return 0;
  115. }
  116. static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
  117. {
  118. if (!__atomic_cmpxchg32(&cond->wait_count, 0, 0)) {
  119. DosPostEventSem(cond->event_sem);
  120. DosWaitEventSem(cond->ack_sem, SEM_INDEFINITE_WAIT);
  121. }
  122. return 0;
  123. }
  124. static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
  125. {
  126. while (!__atomic_cmpxchg32(&cond->wait_count, 0, 0))
  127. pthread_cond_signal(cond);
  128. return 0;
  129. }
  130. static av_always_inline int pthread_cond_timedwait(pthread_cond_t *cond,
  131. pthread_mutex_t *mutex,
  132. const struct timespec *abstime)
  133. {
  134. int64_t abs_milli = abstime->tv_sec * 1000LL + abstime->tv_nsec / 1000000;
  135. ULONG t = av_clip64(abs_milli - av_gettime() / 1000, 0, ULONG_MAX);
  136. __atomic_increment(&cond->wait_count);
  137. pthread_mutex_unlock(mutex);
  138. APIRET ret = DosWaitEventSem(cond->event_sem, t);
  139. __atomic_decrement(&cond->wait_count);
  140. DosPostEventSem(cond->ack_sem);
  141. pthread_mutex_lock(mutex);
  142. return (ret == ERROR_TIMEOUT) ? ETIMEDOUT : 0;
  143. }
  144. static av_always_inline int pthread_cond_wait(pthread_cond_t *cond,
  145. pthread_mutex_t *mutex)
  146. {
  147. __atomic_increment(&cond->wait_count);
  148. pthread_mutex_unlock(mutex);
  149. DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
  150. __atomic_decrement(&cond->wait_count);
  151. DosPostEventSem(cond->ack_sem);
  152. pthread_mutex_lock(mutex);
  153. return 0;
  154. }
  155. static av_always_inline int pthread_once(pthread_once_t *once_control,
  156. void (*init_routine)(void))
  157. {
  158. if (!once_control->done)
  159. {
  160. _fmutex_request(&once_control->mtx, 0);
  161. if (!once_control->done)
  162. {
  163. init_routine();
  164. once_control->done = 1;
  165. }
  166. _fmutex_release(&once_control->mtx);
  167. }
  168. return 0;
  169. }
  170. #endif /* COMPAT_OS2THREADS_H */