Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  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. /**
  22.  * @file
  23.  * os2threads to pthreads wrapper
  24.  */
  25.  
  26. #ifndef AVCODEC_OS2PTHREADS_H
  27. #define AVCODEC_OS2PTHREADS_H
  28.  
  29. #define INCL_DOS
  30. #include <os2.h>
  31.  
  32. #undef __STRICT_ANSI__          /* for _beginthread() */
  33. #include <stdlib.h>
  34.  
  35. typedef TID  pthread_t;
  36. typedef void pthread_attr_t;
  37.  
  38. typedef HMTX pthread_mutex_t;
  39. typedef void pthread_mutexattr_t;
  40.  
  41. typedef struct {
  42.     HEV  event_sem;
  43.     int  wait_count;
  44. } pthread_cond_t;
  45.  
  46. typedef void pthread_condattr_t;
  47.  
  48. struct thread_arg {
  49.     void *(*start_routine)(void *);
  50.     void *arg;
  51. };
  52.  
  53. static void thread_entry(void *arg)
  54. {
  55.     struct thread_arg *thread_arg = arg;
  56.  
  57.     thread_arg->start_routine(thread_arg->arg);
  58.  
  59.     av_free(thread_arg);
  60. }
  61.  
  62. static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
  63. {
  64.     struct thread_arg *thread_arg;
  65.  
  66.     thread_arg = av_mallocz(sizeof(struct thread_arg));
  67.  
  68.     thread_arg->start_routine = start_routine;
  69.     thread_arg->arg = arg;
  70.  
  71.     *thread = _beginthread(thread_entry, NULL, 256 * 1024, thread_arg);
  72.  
  73.     return 0;
  74. }
  75.  
  76. static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
  77. {
  78.     DosWaitThread((PTID)&thread, DCWW_WAIT);
  79.  
  80.     return 0;
  81. }
  82.  
  83. static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
  84. {
  85.     DosCreateMutexSem(NULL, (PHMTX)mutex, 0, FALSE);
  86.  
  87.     return 0;
  88. }
  89.  
  90. static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
  91. {
  92.     DosCloseMutexSem(*(PHMTX)mutex);
  93.  
  94.     return 0;
  95. }
  96.  
  97. static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
  98. {
  99.     DosRequestMutexSem(*(PHMTX)mutex, SEM_INDEFINITE_WAIT);
  100.  
  101.     return 0;
  102. }
  103.  
  104. static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
  105. {
  106.     DosReleaseMutexSem(*(PHMTX)mutex);
  107.  
  108.     return 0;
  109. }
  110.  
  111. static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
  112. {
  113.     DosCreateEventSem(NULL, &cond->event_sem, DCE_POSTONE, FALSE);
  114.  
  115.     cond->wait_count = 0;
  116.  
  117.     return 0;
  118. }
  119.  
  120. static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
  121. {
  122.     DosCloseEventSem(cond->event_sem);
  123.  
  124.     return 0;
  125. }
  126.  
  127. static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
  128. {
  129.     if (cond->wait_count > 0) {
  130.         DosPostEventSem(cond->event_sem);
  131.  
  132.         cond->wait_count--;
  133.     }
  134.  
  135.     return 0;
  136. }
  137.  
  138. static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
  139. {
  140.     while (cond->wait_count > 0) {
  141.         DosPostEventSem(cond->event_sem);
  142.  
  143.         cond->wait_count--;
  144.     }
  145.  
  146.     return 0;
  147. }
  148.  
  149. static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  150. {
  151.     cond->wait_count++;
  152.  
  153.     pthread_mutex_unlock(mutex);
  154.  
  155.     DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
  156.  
  157.     pthread_mutex_lock(mutex);
  158.  
  159.     return 0;
  160. }
  161.  
  162. #endif /* AVCODEC_OS2PTHREADS_H */
  163.