os2threads.h 4.2 KB

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