2 * Copyright (c) 2011 KO Myung-Hun <komh@chollian.net>
4 * This file is part of FFmpeg.
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.
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.
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
23 * os2threads to pthreads wrapper
26 #ifndef AVCODEC_OS2PTHREADS_H
27 #define AVCODEC_OS2PTHREADS_H
32 #undef __STRICT_ANSI__ /* for _beginthread() */
35 #include <sys/fmutex.h>
37 #include "libavutil/mem.h"
39 typedef TID pthread_t;
40 typedef void pthread_attr_t;
42 typedef HMTX pthread_mutex_t;
43 typedef void pthread_mutexattr_t;
50 typedef void pthread_condattr_t;
57 #define PTHREAD_ONCE_INIT {0, _FMUTEX_INITIALIZER}
60 void *(*start_routine)(void *);
64 static void thread_entry(void *arg)
66 struct thread_arg *thread_arg = arg;
68 thread_arg->start_routine(thread_arg->arg);
73 static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
75 struct thread_arg *thread_arg;
77 thread_arg = av_mallocz(sizeof(struct thread_arg));
81 thread_arg->start_routine = start_routine;
82 thread_arg->arg = arg;
84 *thread = _beginthread(thread_entry, NULL, 256 * 1024, thread_arg);
89 static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
91 DosWaitThread((PTID)&thread, DCWW_WAIT);
96 static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
98 DosCreateMutexSem(NULL, (PHMTX)mutex, 0, FALSE);
103 static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
105 DosCloseMutexSem(*(PHMTX)mutex);
110 static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
112 DosRequestMutexSem(*(PHMTX)mutex, SEM_INDEFINITE_WAIT);
117 static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
119 DosReleaseMutexSem(*(PHMTX)mutex);
124 static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
126 DosCreateEventSem(NULL, &cond->event_sem, DCE_POSTONE, FALSE);
128 cond->wait_count = 0;
133 static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
135 DosCloseEventSem(cond->event_sem);
140 static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
142 if (cond->wait_count > 0) {
143 DosPostEventSem(cond->event_sem);
151 static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
153 while (cond->wait_count > 0) {
154 DosPostEventSem(cond->event_sem);
162 static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
166 pthread_mutex_unlock(mutex);
168 DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
170 pthread_mutex_lock(mutex);
175 static av_always_inline int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
177 if (!once_control->done)
179 _fmutex_request(&once_control->mtx, 0);
181 if (!once_control->done)
185 once_control->done = 1;
188 _fmutex_release(&once_control->mtx);
193 #endif /* AVCODEC_OS2PTHREADS_H */