compat/os2threads: support the return value of joined thread
[ffmpeg.git] / compat / os2threads.h
1 /*
2  * Copyright (c) 2011 KO Myung-Hun <komh@chollian.net>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * os2threads to pthreads wrapper
24  */
25
26 #ifndef COMPAT_OS2THREADS_H
27 #define COMPAT_OS2THREADS_H
28
29 #define INCL_DOS
30 #include <os2.h>
31
32 #undef __STRICT_ANSI__          /* for _beginthread() */
33 #include <stdlib.h>
34
35 #include <sys/builtin.h>
36 #include <sys/fmutex.h>
37
38 #include "libavutil/attributes.h"
39
40 typedef struct {
41     TID tid;
42     void *(*start_routine)(void *);
43     void *arg;
44     void *result;
45 } pthread_t;
46
47 typedef void pthread_attr_t;
48
49 typedef HMTX pthread_mutex_t;
50 typedef void pthread_mutexattr_t;
51
52 typedef struct {
53     HEV event_sem;
54     HEV ack_sem;
55     volatile unsigned  wait_count;
56 } pthread_cond_t;
57
58 typedef void pthread_condattr_t;
59
60 typedef struct {
61     volatile int done;
62     _fmutex mtx;
63 } pthread_once_t;
64
65 #define PTHREAD_ONCE_INIT {0, _FMUTEX_INITIALIZER}
66
67 static void thread_entry(void *arg)
68 {
69     pthread_t *thread = arg;
70
71     thread->result = thread->start_routine(thread->arg);
72 }
73
74 static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
75 {
76     thread->start_routine = start_routine;
77     thread->arg = arg;
78     thread->result = NULL;
79
80     thread->tid = _beginthread(thread_entry, NULL, 1024 * 1024, thread);
81
82     return 0;
83 }
84
85 static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
86 {
87     DosWaitThread(&thread.tid, DCWW_WAIT);
88
89     if (value_ptr)
90         *value_ptr = thread.result;
91
92     return 0;
93 }
94
95 static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
96 {
97     DosCreateMutexSem(NULL, (PHMTX)mutex, 0, FALSE);
98
99     return 0;
100 }
101
102 static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
103 {
104     DosCloseMutexSem(*(PHMTX)mutex);
105
106     return 0;
107 }
108
109 static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
110 {
111     DosRequestMutexSem(*(PHMTX)mutex, SEM_INDEFINITE_WAIT);
112
113     return 0;
114 }
115
116 static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
117 {
118     DosReleaseMutexSem(*(PHMTX)mutex);
119
120     return 0;
121 }
122
123 static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
124 {
125     DosCreateEventSem(NULL, &cond->event_sem, DCE_POSTONE, FALSE);
126     DosCreateEventSem(NULL, &cond->ack_sem, DCE_POSTONE, FALSE);
127
128     cond->wait_count = 0;
129
130     return 0;
131 }
132
133 static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
134 {
135     DosCloseEventSem(cond->event_sem);
136     DosCloseEventSem(cond->ack_sem);
137
138     return 0;
139 }
140
141 static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
142 {
143     if (!__atomic_cmpxchg32(&cond->wait_count, 0, 0)) {
144         DosPostEventSem(cond->event_sem);
145         DosWaitEventSem(cond->ack_sem, SEM_INDEFINITE_WAIT);
146     }
147
148     return 0;
149 }
150
151 static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
152 {
153     while (!__atomic_cmpxchg32(&cond->wait_count, 0, 0))
154         pthread_cond_signal(cond);
155
156     return 0;
157 }
158
159 static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
160 {
161     __atomic_increment(&cond->wait_count);
162
163     pthread_mutex_unlock(mutex);
164
165     DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
166
167     __atomic_decrement(&cond->wait_count);
168
169     DosPostEventSem(cond->ack_sem);
170
171     pthread_mutex_lock(mutex);
172
173     return 0;
174 }
175
176 static av_always_inline int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
177 {
178     if (!once_control->done)
179     {
180         _fmutex_request(&once_control->mtx, 0);
181
182         if (!once_control->done)
183         {
184             init_routine();
185
186             once_control->done = 1;
187         }
188
189         _fmutex_release(&once_control->mtx);
190     }
191
192     return 0;
193 }
194 #endif /* COMPAT_OS2THREADS_H */