os2threads.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #include <os2.h>
  28. #undef __STRICT_ANSI__ /* for _beginthread() */
  29. #include <stdlib.h>
  30. #include <sys/builtin.h>
  31. #include <sys/fmutex.h>
  32. #include "libavutil/attributes.h"
  33. typedef struct {
  34. TID tid;
  35. void *(*start_routine)(void *);
  36. void *arg;
  37. void *result;
  38. } pthread_t;
  39. typedef void pthread_attr_t;
  40. typedef _fmutex pthread_mutex_t;
  41. typedef void pthread_mutexattr_t;
  42. #define PTHREAD_MUTEX_INITIALIZER _FMUTEX_INITIALIZER
  43. typedef struct {
  44. HEV event_sem;
  45. HEV ack_sem;
  46. volatile unsigned wait_count;
  47. } pthread_cond_t;
  48. typedef void pthread_condattr_t;
  49. typedef struct {
  50. volatile int done;
  51. _fmutex mtx;
  52. } pthread_once_t;
  53. #define PTHREAD_ONCE_INIT {0, _FMUTEX_INITIALIZER}
  54. static void thread_entry(void *arg)
  55. {
  56. pthread_t *thread = arg;
  57. thread->result = thread->start_routine(thread->arg);
  58. }
  59. static av_always_inline int pthread_create(pthread_t *thread,
  60. const pthread_attr_t *attr,
  61. void *(*start_routine)(void*),
  62. void *arg)
  63. {
  64. thread->start_routine = start_routine;
  65. thread->arg = arg;
  66. thread->result = NULL;
  67. thread->tid = _beginthread(thread_entry, NULL, 1024 * 1024, thread);
  68. return 0;
  69. }
  70. static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
  71. {
  72. DosWaitThread(&thread.tid, DCWW_WAIT);
  73. if (value_ptr)
  74. *value_ptr = thread.result;
  75. return 0;
  76. }
  77. static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex,
  78. const pthread_mutexattr_t *attr)
  79. {
  80. _fmutex_create(mutex, 0);
  81. return 0;
  82. }
  83. static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
  84. {
  85. _fmutex_close(mutex);
  86. return 0;
  87. }
  88. static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
  89. {
  90. _fmutex_request(mutex, 0);
  91. return 0;
  92. }
  93. static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
  94. {
  95. _fmutex_release(mutex);
  96. return 0;
  97. }
  98. static av_always_inline int pthread_cond_init(pthread_cond_t *cond,
  99. const pthread_condattr_t *attr)
  100. {
  101. DosCreateEventSem(NULL, &cond->event_sem, DCE_POSTONE, FALSE);
  102. DosCreateEventSem(NULL, &cond->ack_sem, DCE_POSTONE, FALSE);
  103. cond->wait_count = 0;
  104. return 0;
  105. }
  106. static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
  107. {
  108. DosCloseEventSem(cond->event_sem);
  109. DosCloseEventSem(cond->ack_sem);
  110. return 0;
  111. }
  112. static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
  113. {
  114. if (!__atomic_cmpxchg32(&cond->wait_count, 0, 0)) {
  115. DosPostEventSem(cond->event_sem);
  116. DosWaitEventSem(cond->ack_sem, SEM_INDEFINITE_WAIT);
  117. }
  118. return 0;
  119. }
  120. static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
  121. {
  122. while (!__atomic_cmpxchg32(&cond->wait_count, 0, 0))
  123. pthread_cond_signal(cond);
  124. return 0;
  125. }
  126. static av_always_inline int pthread_cond_wait(pthread_cond_t *cond,
  127. pthread_mutex_t *mutex)
  128. {
  129. __atomic_increment(&cond->wait_count);
  130. pthread_mutex_unlock(mutex);
  131. DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
  132. __atomic_decrement(&cond->wait_count);
  133. DosPostEventSem(cond->ack_sem);
  134. pthread_mutex_lock(mutex);
  135. return 0;
  136. }
  137. static av_always_inline int pthread_once(pthread_once_t *once_control,
  138. void (*init_routine)(void))
  139. {
  140. if (!once_control->done)
  141. {
  142. _fmutex_request(&once_control->mtx, 0);
  143. if (!once_control->done)
  144. {
  145. init_routine();
  146. once_control->done = 1;
  147. }
  148. _fmutex_release(&once_control->mtx);
  149. }
  150. return 0;
  151. }
  152. #endif /* COMPAT_OS2THREADS_H */