os2threads.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. thread_arg->start_routine = start_routine;
  55. thread_arg->arg = arg;
  56. *thread = _beginthread(thread_entry, NULL, 256 * 1024, thread_arg);
  57. return 0;
  58. }
  59. static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
  60. {
  61. DosWaitThread((PTID)&thread, DCWW_WAIT);
  62. return 0;
  63. }
  64. static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
  65. {
  66. DosCreateMutexSem(NULL, (PHMTX)mutex, 0, FALSE);
  67. return 0;
  68. }
  69. static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
  70. {
  71. DosCloseMutexSem(*(PHMTX)mutex);
  72. return 0;
  73. }
  74. static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
  75. {
  76. DosRequestMutexSem(*(PHMTX)mutex, SEM_INDEFINITE_WAIT);
  77. return 0;
  78. }
  79. static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
  80. {
  81. DosReleaseMutexSem(*(PHMTX)mutex);
  82. return 0;
  83. }
  84. static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
  85. {
  86. DosCreateEventSem(NULL, &cond->event_sem, DCE_POSTONE, FALSE);
  87. cond->wait_count = 0;
  88. return 0;
  89. }
  90. static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
  91. {
  92. DosCloseEventSem(cond->event_sem);
  93. return 0;
  94. }
  95. static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
  96. {
  97. if (cond->wait_count > 0) {
  98. DosPostEventSem(cond->event_sem);
  99. cond->wait_count--;
  100. }
  101. return 0;
  102. }
  103. static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
  104. {
  105. while (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_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  112. {
  113. cond->wait_count++;
  114. pthread_mutex_unlock(mutex);
  115. DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
  116. pthread_mutex_lock(mutex);
  117. return 0;
  118. }
  119. #endif /* AVCODEC_OS2PTHREADS_H */