w32pthreads: Add pthread_once emulation
[libav.git] / compat / w32pthreads.h
1 /*
2 * Copyright (C) 2010-2011 x264 project
3 *
4 * Authors: Steven Walters <kemuri9@gmail.com>
5 * Pegasys Inc. <http://www.pegasys-inc.com>
6 *
7 * This file is part of Libav.
8 *
9 * Libav is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * Libav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * w32threads to pthreads wrapper
27 */
28
29 #ifndef LIBAV_COMPAT_W32PTHREADS_H
30 #define LIBAV_COMPAT_W32PTHREADS_H
31
32 /* Build up a pthread-like API using underlying Windows API. Have only static
33 * methods so as to not conflict with a potentially linked in pthread-win32
34 * library.
35 * As most functions here are used without checking return values,
36 * only implement return values as necessary. */
37
38 #define WIN32_LEAN_AND_MEAN
39 #include <windows.h>
40 #include <process.h>
41
42 #include "libavutil/attributes.h"
43 #include "libavutil/internal.h"
44 #include "libavutil/mem.h"
45
46 typedef struct pthread_t {
47 void *handle;
48 void *(*func)(void* arg);
49 void *arg;
50 void *ret;
51 } pthread_t;
52
53 /* the conditional variable api for windows 6.0+ uses critical sections and
54 * not mutexes */
55 typedef CRITICAL_SECTION pthread_mutex_t;
56
57 /* This is the CONDITION_VARIABLE typedef for using Windows' native
58 * conditional variables on kernels 6.0+. */
59 #if HAVE_CONDITION_VARIABLE_PTR
60 typedef CONDITION_VARIABLE pthread_cond_t;
61 #else
62 typedef struct pthread_cond_t {
63 void *Ptr;
64 } pthread_cond_t;
65 #endif
66
67 #if _WIN32_WINNT >= 0x0600
68 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
69 #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
70 #endif
71
72 static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
73 {
74 pthread_t *h = arg;
75 h->ret = h->func(h->arg);
76 return 0;
77 }
78
79 static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
80 void *(*start_routine)(void*), void *arg)
81 {
82 thread->func = start_routine;
83 thread->arg = arg;
84 thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
85 0, NULL);
86 return !thread->handle;
87 }
88
89 static av_unused void pthread_join(pthread_t thread, void **value_ptr)
90 {
91 DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
92 if (ret != WAIT_OBJECT_0)
93 return;
94 if (value_ptr)
95 *value_ptr = thread.ret;
96 CloseHandle(thread.handle);
97 }
98
99 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
100 {
101 InitializeCriticalSection(m);
102 return 0;
103 }
104 static inline int pthread_mutex_destroy(pthread_mutex_t *m)
105 {
106 DeleteCriticalSection(m);
107 return 0;
108 }
109 static inline int pthread_mutex_lock(pthread_mutex_t *m)
110 {
111 EnterCriticalSection(m);
112 return 0;
113 }
114 static inline int pthread_mutex_unlock(pthread_mutex_t *m)
115 {
116 LeaveCriticalSection(m);
117 return 0;
118 }
119
120 #if _WIN32_WINNT >= 0x0600
121
122 typedef INIT_ONCE pthread_once_t;
123 #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
124
125 static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
126 {
127 BOOL pending = FALSE;
128 InitOnceBeginInitialize(once_control, 0, &pending, NULL);
129 if (pending)
130 init_routine();
131 InitOnceComplete(once_control, 0, NULL);
132 return 0;
133 }
134
135 static inline void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
136 {
137 InitializeConditionVariable(cond);
138 }
139
140 /* native condition variables do not destroy */
141 static inline void pthread_cond_destroy(pthread_cond_t *cond)
142 {
143 return;
144 }
145
146 static inline void pthread_cond_broadcast(pthread_cond_t *cond)
147 {
148 WakeAllConditionVariable(cond);
149 }
150
151 static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
152 {
153 SleepConditionVariableCS(cond, mutex, INFINITE);
154 return 0;
155 }
156
157 static inline void pthread_cond_signal(pthread_cond_t *cond)
158 {
159 WakeConditionVariable(cond);
160 }
161
162 #else // _WIN32_WINNT < 0x0600
163 /* for pre-Windows 6.0 platforms, define INIT_ONCE struct,
164 * compatible to the one used in the native API */
165
166 typedef union pthread_once_t {
167 void * Ptr; ///< For the Windows 6.0+ native functions
168 LONG state; ///< For the pre-Windows 6.0 compat code
169 } pthread_once_t;
170
171 #define PTHREAD_ONCE_INIT {0}
172
173 /* function pointers to init once API on windows 6.0+ kernels */
174 static BOOL (WINAPI *initonce_begin)(pthread_once_t *lpInitOnce, DWORD dwFlags, BOOL *fPending, void **lpContext);
175 static BOOL (WINAPI *initonce_complete)(pthread_once_t *lpInitOnce, DWORD dwFlags, void *lpContext);
176
177 /* pre-Windows 6.0 compat using a spin-lock */
178 static inline void w32thread_once_fallback(LONG volatile *state, void (*init_routine)(void))
179 {
180 switch (InterlockedCompareExchange(state, 1, 0)) {
181 /* Initial run */
182 case 0:
183 init_routine();
184 InterlockedExchange(state, 2);
185 break;
186 /* Another thread is running init */
187 case 1:
188 while (1) {
189 MemoryBarrier();
190 if (*state == 2)
191 break;
192 Sleep(0);
193 }
194 break;
195 /* Initialization complete */
196 case 2:
197 break;
198 }
199 }
200
201 static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
202 {
203 /* Use native functions on Windows 6.0+ */
204 if (initonce_begin && initonce_complete) {
205 BOOL pending = FALSE;
206 initonce_begin(once_control, 0, &pending, NULL);
207 if (pending)
208 init_routine();
209 initonce_complete(once_control, 0, NULL);
210 return 0;
211 }
212
213 w32thread_once_fallback(&once_control->state, init_routine);
214 return 0;
215 }
216
217 /* for pre-Windows 6.0 platforms we need to define and use our own condition
218 * variable and api */
219
220 typedef struct win32_cond_t {
221 pthread_mutex_t mtx_broadcast;
222 pthread_mutex_t mtx_waiter_count;
223 volatile int waiter_count;
224 HANDLE semaphore;
225 HANDLE waiters_done;
226 volatile int is_broadcast;
227 } win32_cond_t;
228
229 /* function pointers to conditional variable API on windows 6.0+ kernels */
230 static void (WINAPI *cond_broadcast)(pthread_cond_t *cond);
231 static void (WINAPI *cond_init)(pthread_cond_t *cond);
232 static void (WINAPI *cond_signal)(pthread_cond_t *cond);
233 static BOOL (WINAPI *cond_wait)(pthread_cond_t *cond, pthread_mutex_t *mutex,
234 DWORD milliseconds);
235
236 static av_unused void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
237 {
238 win32_cond_t *win32_cond = NULL;
239 if (cond_init) {
240 cond_init(cond);
241 return;
242 }
243
244 /* non native condition variables */
245 win32_cond = av_mallocz(sizeof(win32_cond_t));
246 if (!win32_cond)
247 return;
248 cond->Ptr = win32_cond;
249 win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
250 if (!win32_cond->semaphore)
251 return;
252 win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
253 if (!win32_cond->waiters_done)
254 return;
255
256 pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
257 pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
258 }
259
260 static av_unused void pthread_cond_destroy(pthread_cond_t *cond)
261 {
262 win32_cond_t *win32_cond = cond->Ptr;
263 /* native condition variables do not destroy */
264 if (cond_init)
265 return;
266
267 /* non native condition variables */
268 CloseHandle(win32_cond->semaphore);
269 CloseHandle(win32_cond->waiters_done);
270 pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
271 pthread_mutex_destroy(&win32_cond->mtx_broadcast);
272 av_freep(&win32_cond);
273 cond->Ptr = NULL;
274 }
275
276 static av_unused void pthread_cond_broadcast(pthread_cond_t *cond)
277 {
278 win32_cond_t *win32_cond = cond->Ptr;
279 int have_waiter;
280
281 if (cond_broadcast) {
282 cond_broadcast(cond);
283 return;
284 }
285
286 /* non native condition variables */
287 pthread_mutex_lock(&win32_cond->mtx_broadcast);
288 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
289 have_waiter = 0;
290
291 if (win32_cond->waiter_count) {
292 win32_cond->is_broadcast = 1;
293 have_waiter = 1;
294 }
295
296 if (have_waiter) {
297 ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
298 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
299 WaitForSingleObject(win32_cond->waiters_done, INFINITE);
300 ResetEvent(win32_cond->waiters_done);
301 win32_cond->is_broadcast = 0;
302 } else
303 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
304 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
305 }
306
307 static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
308 {
309 win32_cond_t *win32_cond = cond->Ptr;
310 int last_waiter;
311 if (cond_wait) {
312 cond_wait(cond, mutex, INFINITE);
313 return 0;
314 }
315
316 /* non native condition variables */
317 pthread_mutex_lock(&win32_cond->mtx_broadcast);
318 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
319 win32_cond->waiter_count++;
320 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
321 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
322
323 // unlock the external mutex
324 pthread_mutex_unlock(mutex);
325 WaitForSingleObject(win32_cond->semaphore, INFINITE);
326
327 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
328 win32_cond->waiter_count--;
329 last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
330 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
331
332 if (last_waiter)
333 SetEvent(win32_cond->waiters_done);
334
335 // lock the external mutex
336 return pthread_mutex_lock(mutex);
337 }
338
339 static av_unused void pthread_cond_signal(pthread_cond_t *cond)
340 {
341 win32_cond_t *win32_cond = cond->Ptr;
342 int have_waiter;
343 if (cond_signal) {
344 cond_signal(cond);
345 return;
346 }
347
348 pthread_mutex_lock(&win32_cond->mtx_broadcast);
349
350 /* non-native condition variables */
351 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
352 have_waiter = win32_cond->waiter_count;
353 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
354
355 if (have_waiter) {
356 ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
357 WaitForSingleObject(win32_cond->waiters_done, INFINITE);
358 ResetEvent(win32_cond->waiters_done);
359 }
360
361 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
362 }
363 #endif
364
365 static av_unused void w32thread_init(void)
366 {
367 #if _WIN32_WINNT < 0x0600
368 HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
369 /* if one is available, then they should all be available */
370 cond_init =
371 (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
372 cond_broadcast =
373 (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
374 cond_signal =
375 (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
376 cond_wait =
377 (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
378 initonce_begin =
379 (void*)GetProcAddress(kernel_dll, "InitOnceBeginInitialize");
380 initonce_complete =
381 (void*)GetProcAddress(kernel_dll, "InitOnceComplete");
382 #endif
383
384 }
385
386 #endif /* LIBAV_COMPAT_W32PTHREADS_H */