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 COMPAT_OS2THREADS_H
27 #define COMPAT_OS2THREADS_H
32 #undef __STRICT_ANSI__ /* for _beginthread() */
35 #include <sys/builtin.h>
36 #include <sys/fmutex.h>
38 #include "libavutil/attributes.h"
42 void *(*start_routine)(void *);
47 typedef void pthread_attr_t;
49 typedef HMTX pthread_mutex_t;
50 typedef void pthread_mutexattr_t;
55 volatile unsigned wait_count;
58 typedef void pthread_condattr_t;
65 #define PTHREAD_ONCE_INIT {0, _FMUTEX_INITIALIZER}
67 static void thread_entry(void *arg)
69 pthread_t *thread = arg;
71 thread->result = thread->start_routine(thread->arg);
74 static av_always_inline int pthread_create(pthread_t *thread,
75 const pthread_attr_t *attr,
76 void *(*start_routine)(void*),
79 thread->start_routine = start_routine;
81 thread->result = NULL;
83 thread->tid = _beginthread(thread_entry, NULL, 1024 * 1024, thread);
88 static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
90 DosWaitThread(&thread.tid, DCWW_WAIT);
93 *value_ptr = thread.result;
98 static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex,
99 const pthread_mutexattr_t *attr)
101 DosCreateMutexSem(NULL, (PHMTX)mutex, 0, FALSE);
106 static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
108 DosCloseMutexSem(*(PHMTX)mutex);
113 static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
115 DosRequestMutexSem(*(PHMTX)mutex, SEM_INDEFINITE_WAIT);
120 static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
122 DosReleaseMutexSem(*(PHMTX)mutex);
127 static av_always_inline int pthread_cond_init(pthread_cond_t *cond,
128 const pthread_condattr_t *attr)
130 DosCreateEventSem(NULL, &cond->event_sem, DCE_POSTONE, FALSE);
131 DosCreateEventSem(NULL, &cond->ack_sem, DCE_POSTONE, FALSE);
133 cond->wait_count = 0;
138 static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
140 DosCloseEventSem(cond->event_sem);
141 DosCloseEventSem(cond->ack_sem);
146 static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
148 if (!__atomic_cmpxchg32(&cond->wait_count, 0, 0)) {
149 DosPostEventSem(cond->event_sem);
150 DosWaitEventSem(cond->ack_sem, SEM_INDEFINITE_WAIT);
156 static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
158 while (!__atomic_cmpxchg32(&cond->wait_count, 0, 0))
159 pthread_cond_signal(cond);
164 static av_always_inline int pthread_cond_wait(pthread_cond_t *cond,
165 pthread_mutex_t *mutex)
167 __atomic_increment(&cond->wait_count);
169 pthread_mutex_unlock(mutex);
171 DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
173 __atomic_decrement(&cond->wait_count);
175 DosPostEventSem(cond->ack_sem);
177 pthread_mutex_lock(mutex);
182 static av_always_inline int pthread_once(pthread_once_t *once_control,
183 void (*init_routine)(void))
185 if (!once_control->done)
187 _fmutex_request(&once_control->mtx, 0);
189 if (!once_control->done)
193 once_control->done = 1;
196 _fmutex_release(&once_control->mtx);
201 #endif /* COMPAT_OS2THREADS_H */