os2threads.h 3.7 KB

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