os2threads.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 AVCODEC_OS2PTHREADS_H
  25. #define AVCODEC_OS2PTHREADS_H
  26. #define INCL_DOS
  27. #include <os2.h>
  28. #undef __STRICT_ANSI__ /* for _beginthread() */
  29. #include <stdlib.h>
  30. typedef TID pthread_t;
  31. typedef void pthread_attr_t;
  32. typedef HMTX pthread_mutex_t;
  33. typedef void pthread_mutexattr_t;
  34. typedef struct {
  35. HEV event_sem;
  36. int wait_count;
  37. } pthread_cond_t;
  38. typedef void pthread_condattr_t;
  39. struct thread_arg {
  40. void *(*start_routine)(void *);
  41. void *arg;
  42. };
  43. static void thread_entry(void *arg)
  44. {
  45. struct thread_arg *thread_arg = arg;
  46. thread_arg->start_routine(thread_arg->arg);
  47. av_free(thread_arg);
  48. }
  49. static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
  50. {
  51. struct thread_arg *thread_arg;
  52. thread_arg = av_mallocz(sizeof(struct thread_arg));
  53. thread_arg->start_routine = start_routine;
  54. thread_arg->arg = arg;
  55. *thread = _beginthread(thread_entry, NULL, 256 * 1024, thread_arg);
  56. return 0;
  57. }
  58. static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
  59. {
  60. DosWaitThread((PTID)&thread, DCWW_WAIT);
  61. return 0;
  62. }
  63. static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
  64. {
  65. DosCreateMutexSem(NULL, (PHMTX)mutex, 0, FALSE);
  66. return 0;
  67. }
  68. static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
  69. {
  70. DosCloseMutexSem(*(PHMTX)mutex);
  71. return 0;
  72. }
  73. static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
  74. {
  75. DosRequestMutexSem(*(PHMTX)mutex, SEM_INDEFINITE_WAIT);
  76. return 0;
  77. }
  78. static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
  79. {
  80. DosReleaseMutexSem(*(PHMTX)mutex);
  81. return 0;
  82. }
  83. static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
  84. {
  85. DosCreateEventSem(NULL, &cond->event_sem, DCE_POSTONE, FALSE);
  86. cond->wait_count = 0;
  87. return 0;
  88. }
  89. static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
  90. {
  91. DosCloseEventSem(cond->event_sem);
  92. return 0;
  93. }
  94. static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
  95. {
  96. if (cond->wait_count > 0) {
  97. DosPostEventSem(cond->event_sem);
  98. cond->wait_count--;
  99. }
  100. return 0;
  101. }
  102. static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
  103. {
  104. while (cond->wait_count > 0) {
  105. DosPostEventSem(cond->event_sem);
  106. cond->wait_count--;
  107. }
  108. return 0;
  109. }
  110. static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  111. {
  112. cond->wait_count++;
  113. pthread_mutex_unlock(mutex);
  114. DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
  115. pthread_mutex_lock(mutex);
  116. return 0;
  117. }
  118. #endif /* AVCODEC_OS2PTHREADS_H */