2 * Copyright (c) 2011-2017 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 _fmutex pthread_mutex_t;
50 typedef void pthread_mutexattr_t;
52 #define PTHREAD_MUTEX_INITIALIZER _FMUTEX_INITIALIZER
57 volatile unsigned wait_count;
60 typedef void pthread_condattr_t;
67 #define PTHREAD_ONCE_INIT {0, _FMUTEX_INITIALIZER}
69 static void thread_entry(void *arg)
71 pthread_t *thread = arg;
73 thread->result = thread->start_routine(thread->arg);
76 static av_always_inline int pthread_create(pthread_t *thread,
77 const pthread_attr_t *attr,
78 void *(*start_routine)(void*),
81 thread->start_routine = start_routine;
83 thread->result = NULL;
85 thread->tid = _beginthread(thread_entry, NULL, 1024 * 1024, thread);
90 static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
92 DosWaitThread(&thread.tid, DCWW_WAIT);
95 *value_ptr = thread.result;
100 static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex,
101 const pthread_mutexattr_t *attr)
103 _fmutex_create(mutex, 0);
108 static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
110 _fmutex_close(mutex);
115 static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
117 _fmutex_request(mutex, 0);
122 static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
124 _fmutex_release(mutex);
129 static av_always_inline int pthread_cond_init(pthread_cond_t *cond,
130 const pthread_condattr_t *attr)
132 DosCreateEventSem(NULL, &cond->event_sem, DCE_POSTONE, FALSE);
133 DosCreateEventSem(NULL, &cond->ack_sem, DCE_POSTONE, FALSE);
135 cond->wait_count = 0;
140 static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
142 DosCloseEventSem(cond->event_sem);
143 DosCloseEventSem(cond->ack_sem);
148 static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
150 if (!__atomic_cmpxchg32(&cond->wait_count, 0, 0)) {
151 DosPostEventSem(cond->event_sem);
152 DosWaitEventSem(cond->ack_sem, SEM_INDEFINITE_WAIT);
158 static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
160 while (!__atomic_cmpxchg32(&cond->wait_count, 0, 0))
161 pthread_cond_signal(cond);
166 static av_always_inline int pthread_cond_wait(pthread_cond_t *cond,
167 pthread_mutex_t *mutex)
169 __atomic_increment(&cond->wait_count);
171 pthread_mutex_unlock(mutex);
173 DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
175 __atomic_decrement(&cond->wait_count);
177 DosPostEventSem(cond->ack_sem);
179 pthread_mutex_lock(mutex);
184 static av_always_inline int pthread_once(pthread_once_t *once_control,
185 void (*init_routine)(void))
187 if (!once_control->done)
189 _fmutex_request(&once_control->mtx, 0);
191 if (!once_control->done)
195 once_control->done = 1;
198 _fmutex_release(&once_control->mtx);
203 #endif /* COMPAT_OS2THREADS_H */