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() */
36 #include <sys/builtin.h>
37 #include <sys/fmutex.h>
39 #include "libavutil/attributes.h"
40 #include "libavutil/common.h"
41 #include "libavutil/time.h"
45 void *(*start_routine)(void *);
50 typedef void pthread_attr_t;
52 typedef _fmutex pthread_mutex_t;
53 typedef void pthread_mutexattr_t;
55 #define PTHREAD_MUTEX_INITIALIZER _FMUTEX_INITIALIZER
60 volatile unsigned wait_count;
63 typedef void pthread_condattr_t;
70 #define PTHREAD_ONCE_INIT {0, _FMUTEX_INITIALIZER}
72 static void thread_entry(void *arg)
74 pthread_t *thread = arg;
76 thread->result = thread->start_routine(thread->arg);
79 static av_always_inline int pthread_create(pthread_t *thread,
80 const pthread_attr_t *attr,
81 void *(*start_routine)(void*),
84 thread->start_routine = start_routine;
86 thread->result = NULL;
88 thread->tid = _beginthread(thread_entry, NULL, 1024 * 1024, thread);
93 static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
95 DosWaitThread(&thread.tid, DCWW_WAIT);
98 *value_ptr = thread.result;
103 static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex,
104 const pthread_mutexattr_t *attr)
106 _fmutex_create(mutex, 0);
111 static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
113 _fmutex_close(mutex);
118 static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
120 _fmutex_request(mutex, 0);
125 static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
127 _fmutex_release(mutex);
132 static av_always_inline int pthread_cond_init(pthread_cond_t *cond,
133 const pthread_condattr_t *attr)
135 DosCreateEventSem(NULL, &cond->event_sem, DCE_POSTONE, FALSE);
136 DosCreateEventSem(NULL, &cond->ack_sem, DCE_POSTONE, FALSE);
138 cond->wait_count = 0;
143 static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
145 DosCloseEventSem(cond->event_sem);
146 DosCloseEventSem(cond->ack_sem);
151 static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
153 if (!__atomic_cmpxchg32(&cond->wait_count, 0, 0)) {
154 DosPostEventSem(cond->event_sem);
155 DosWaitEventSem(cond->ack_sem, SEM_INDEFINITE_WAIT);
161 static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
163 while (!__atomic_cmpxchg32(&cond->wait_count, 0, 0))
164 pthread_cond_signal(cond);
169 static av_always_inline int pthread_cond_timedwait(pthread_cond_t *cond,
170 pthread_mutex_t *mutex,
171 const struct timespec *abstime)
173 int64_t abs_milli = abstime->tv_sec * 1000LL + abstime->tv_nsec / 1000000;
174 ULONG t = av_clip64(abs_milli - av_gettime() / 1000, 0, ULONG_MAX);
176 __atomic_increment(&cond->wait_count);
178 pthread_mutex_unlock(mutex);
180 APIRET ret = DosWaitEventSem(cond->event_sem, t);
182 __atomic_decrement(&cond->wait_count);
184 DosPostEventSem(cond->ack_sem);
186 pthread_mutex_lock(mutex);
188 return (ret == ERROR_TIMEOUT) ? ETIMEDOUT : 0;
191 static av_always_inline int pthread_cond_wait(pthread_cond_t *cond,
192 pthread_mutex_t *mutex)
194 __atomic_increment(&cond->wait_count);
196 pthread_mutex_unlock(mutex);
198 DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
200 __atomic_decrement(&cond->wait_count);
202 DosPostEventSem(cond->ack_sem);
204 pthread_mutex_lock(mutex);
209 static av_always_inline int pthread_once(pthread_once_t *once_control,
210 void (*init_routine)(void))
212 if (!once_control->done)
214 _fmutex_request(&once_control->mtx, 0);
216 if (!once_control->done)
220 once_control->done = 1;
223 _fmutex_release(&once_control->mtx);
228 #endif /* COMPAT_OS2THREADS_H */