os2threads.h 4.7 KB

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