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
30 #define INCL_DOSERRORS
33 #undef __STRICT_ANSI__ /* for _beginthread() */
37 #include <sys/builtin.h>
38 #include <sys/fmutex.h>
40 #include "libavutil/attributes.h"
41 #include "libavutil/common.h"
42 #include "libavutil/time.h"
46 void *(*start_routine)(void *);
51 typedef void pthread_attr_t;
53 typedef _fmutex pthread_mutex_t;
54 typedef void pthread_mutexattr_t;
56 #define PTHREAD_MUTEX_INITIALIZER _FMUTEX_INITIALIZER
61 volatile unsigned wait_count;
64 typedef void pthread_condattr_t;
71 #define PTHREAD_ONCE_INIT {0, _FMUTEX_INITIALIZER}
73 static void thread_entry(void *arg)
75 pthread_t *thread = arg;
77 thread->result = thread->start_routine(thread->arg);
80 static av_always_inline int pthread_create(pthread_t *thread,
81 const pthread_attr_t *attr,
82 void *(*start_routine)(void*),
85 thread->start_routine = start_routine;
87 thread->result = NULL;
89 thread->tid = _beginthread(thread_entry, NULL, 1024 * 1024, thread);
94 static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
96 DosWaitThread(&thread.tid, DCWW_WAIT);
99 *value_ptr = thread.result;
104 static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex,
105 const pthread_mutexattr_t *attr)
107 _fmutex_create(mutex, 0);
112 static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
114 _fmutex_close(mutex);
119 static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
121 _fmutex_request(mutex, 0);
126 static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
128 _fmutex_release(mutex);
133 static av_always_inline int pthread_cond_init(pthread_cond_t *cond,
134 const pthread_condattr_t *attr)
136 DosCreateEventSem(NULL, &cond->event_sem, DCE_POSTONE, FALSE);
137 DosCreateEventSem(NULL, &cond->ack_sem, DCE_POSTONE, FALSE);
139 cond->wait_count = 0;
144 static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
146 DosCloseEventSem(cond->event_sem);
147 DosCloseEventSem(cond->ack_sem);
152 static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
154 if (!__atomic_cmpxchg32(&cond->wait_count, 0, 0)) {
155 DosPostEventSem(cond->event_sem);
156 DosWaitEventSem(cond->ack_sem, SEM_INDEFINITE_WAIT);
162 static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
164 while (!__atomic_cmpxchg32(&cond->wait_count, 0, 0))
165 pthread_cond_signal(cond);
170 static av_always_inline int pthread_cond_timedwait(pthread_cond_t *cond,
171 pthread_mutex_t *mutex,
172 const struct timespec *abstime)
174 int64_t abs_milli = abstime->tv_sec * 1000LL + abstime->tv_nsec / 1000000;
175 ULONG t = av_clip64(abs_milli - av_gettime() / 1000, 0, ULONG_MAX);
177 __atomic_increment(&cond->wait_count);
179 pthread_mutex_unlock(mutex);
181 APIRET ret = DosWaitEventSem(cond->event_sem, t);
183 __atomic_decrement(&cond->wait_count);
185 DosPostEventSem(cond->ack_sem);
187 pthread_mutex_lock(mutex);
189 return (ret == ERROR_TIMEOUT) ? ETIMEDOUT : 0;
192 static av_always_inline int pthread_cond_wait(pthread_cond_t *cond,
193 pthread_mutex_t *mutex)
195 __atomic_increment(&cond->wait_count);
197 pthread_mutex_unlock(mutex);
199 DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
201 __atomic_decrement(&cond->wait_count);
203 DosPostEventSem(cond->ack_sem);
205 pthread_mutex_lock(mutex);
210 static av_always_inline int pthread_once(pthread_once_t *once_control,
211 void (*init_routine)(void))
213 if (!once_control->done)
215 _fmutex_request(&once_control->mtx, 0);
217 if (!once_control->done)
221 once_control->done = 1;
224 _fmutex_release(&once_control->mtx);
229 #endif /* COMPAT_OS2THREADS_H */