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