w32pthreads: Fix function pointer casts
[libav.git] / compat / w32pthreads.h
CommitLineData
27237d52
SW
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
439902e0
DB
29#ifndef LIBAV_COMPAT_W32PTHREADS_H
30#define LIBAV_COMPAT_W32PTHREADS_H
27237d52
SW
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
68e00ad6 42#if _WIN32_WINNT < 0x0600 && defined(__MINGW32__)
407ac223
MS
43#undef MemoryBarrier
44#define MemoryBarrier __sync_synchronize
68e00ad6
HL
45#endif
46
6baeadd1 47#include "libavutil/attributes.h"
11928d24
DB
48#include "libavutil/internal.h"
49#include "libavutil/mem.h"
50
e4cbf752 51typedef struct pthread_t {
27237d52
SW
52 void *handle;
53 void *(*func)(void* arg);
54 void *arg;
55 void *ret;
56} pthread_t;
57
58/* the conditional variable api for windows 6.0+ uses critical sections and
59 * not mutexes */
60typedef CRITICAL_SECTION pthread_mutex_t;
61
73ea3ffc
JA
62/* This is the CONDITION_VARIABLE typedef for using Windows' native
63 * conditional variables on kernels 6.0+. */
64#if HAVE_CONDITION_VARIABLE_PTR
65typedef CONDITION_VARIABLE pthread_cond_t;
66#else
e4cbf752 67typedef struct pthread_cond_t {
73ea3ffc 68 void *Ptr;
27237d52 69} pthread_cond_t;
73ea3ffc 70#endif
27237d52 71
b7c3bfd5 72#if _WIN32_WINNT >= 0x0600
428b0578
MS
73#define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
74#define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
4332bf98 75#endif
27237d52 76
6baeadd1 77static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
27237d52
SW
78{
79 pthread_t *h = arg;
80 h->ret = h->func(h->arg);
81 return 0;
82}
83
6baeadd1
DB
84static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
85 void *(*start_routine)(void*), void *arg)
27237d52
SW
86{
87 thread->func = start_routine;
88 thread->arg = arg;
89 thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
90 0, NULL);
91 return !thread->handle;
92}
93
6baeadd1 94static av_unused void pthread_join(pthread_t thread, void **value_ptr)
27237d52
SW
95{
96 DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
97 if (ret != WAIT_OBJECT_0)
98 return;
99 if (value_ptr)
100 *value_ptr = thread.ret;
101 CloseHandle(thread.handle);
102}
103
81486312
MS
104static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
105{
106 InitializeCriticalSection(m);
107 return 0;
108}
109static inline int pthread_mutex_destroy(pthread_mutex_t *m)
110{
111 DeleteCriticalSection(m);
112 return 0;
113}
114static inline int pthread_mutex_lock(pthread_mutex_t *m)
115{
116 EnterCriticalSection(m);
117 return 0;
118}
119static inline int pthread_mutex_unlock(pthread_mutex_t *m)
120{
121 LeaveCriticalSection(m);
122 return 0;
123}
27237d52 124
b7c3bfd5 125#if _WIN32_WINNT >= 0x0600
b22693b0
HL
126
127typedef INIT_ONCE pthread_once_t;
128#define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
129
130static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
131{
132 BOOL pending = FALSE;
133 InitOnceBeginInitialize(once_control, 0, &pending, NULL);
134 if (pending)
135 init_routine();
136 InitOnceComplete(once_control, 0, NULL);
137 return 0;
138}
139
b7c3bfd5
JA
140static inline void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
141{
142 InitializeConditionVariable(cond);
143}
144
145/* native condition variables do not destroy */
146static inline void pthread_cond_destroy(pthread_cond_t *cond)
147{
148 return;
149}
150
151static inline void pthread_cond_broadcast(pthread_cond_t *cond)
152{
153 WakeAllConditionVariable(cond);
154}
155
156static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
157{
158 SleepConditionVariableCS(cond, mutex, INFINITE);
159 return 0;
160}
161
162static inline void pthread_cond_signal(pthread_cond_t *cond)
163{
164 WakeConditionVariable(cond);
165}
166
167#else // _WIN32_WINNT < 0x0600
2830bce4
HL
168
169/* atomic init state of dynamically loaded functions */
170static LONG w32thread_init_state = 0;
171static av_unused void w32thread_init(void);
172
b22693b0
HL
173/* for pre-Windows 6.0 platforms, define INIT_ONCE struct,
174 * compatible to the one used in the native API */
175
176typedef union pthread_once_t {
177 void * Ptr; ///< For the Windows 6.0+ native functions
178 LONG state; ///< For the pre-Windows 6.0 compat code
179} pthread_once_t;
180
181#define PTHREAD_ONCE_INIT {0}
182
183/* function pointers to init once API on windows 6.0+ kernels */
184static BOOL (WINAPI *initonce_begin)(pthread_once_t *lpInitOnce, DWORD dwFlags, BOOL *fPending, void **lpContext);
185static BOOL (WINAPI *initonce_complete)(pthread_once_t *lpInitOnce, DWORD dwFlags, void *lpContext);
186
187/* pre-Windows 6.0 compat using a spin-lock */
188static inline void w32thread_once_fallback(LONG volatile *state, void (*init_routine)(void))
189{
190 switch (InterlockedCompareExchange(state, 1, 0)) {
191 /* Initial run */
192 case 0:
193 init_routine();
194 InterlockedExchange(state, 2);
195 break;
196 /* Another thread is running init */
197 case 1:
198 while (1) {
199 MemoryBarrier();
200 if (*state == 2)
201 break;
202 Sleep(0);
203 }
204 break;
205 /* Initialization complete */
206 case 2:
207 break;
208 }
209}
210
211static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
212{
2830bce4
HL
213 w32thread_once_fallback(&w32thread_init_state, w32thread_init);
214
b22693b0
HL
215 /* Use native functions on Windows 6.0+ */
216 if (initonce_begin && initonce_complete) {
217 BOOL pending = FALSE;
218 initonce_begin(once_control, 0, &pending, NULL);
219 if (pending)
220 init_routine();
221 initonce_complete(once_control, 0, NULL);
222 return 0;
223 }
224
225 w32thread_once_fallback(&once_control->state, init_routine);
226 return 0;
227}
228
27237d52
SW
229/* for pre-Windows 6.0 platforms we need to define and use our own condition
230 * variable and api */
b22693b0 231
e4cbf752 232typedef struct win32_cond_t {
8b03d7fe 233 pthread_mutex_t mtx_broadcast;
27237d52
SW
234 pthread_mutex_t mtx_waiter_count;
235 volatile int waiter_count;
236 HANDLE semaphore;
8b03d7fe 237 HANDLE waiters_done;
01eb9805 238 volatile int is_broadcast;
27237d52
SW
239} win32_cond_t;
240
b7c3bfd5
JA
241/* function pointers to conditional variable API on windows 6.0+ kernels */
242static void (WINAPI *cond_broadcast)(pthread_cond_t *cond);
243static void (WINAPI *cond_init)(pthread_cond_t *cond);
244static void (WINAPI *cond_signal)(pthread_cond_t *cond);
245static BOOL (WINAPI *cond_wait)(pthread_cond_t *cond, pthread_mutex_t *mutex,
246 DWORD milliseconds);
247
6baeadd1 248static av_unused void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
27237d52
SW
249{
250 win32_cond_t *win32_cond = NULL;
2830bce4
HL
251
252 w32thread_once_fallback(&w32thread_init_state, w32thread_init);
253
ef51692a 254 if (cond_init) {
27237d52
SW
255 cond_init(cond);
256 return;
257 }
258
259 /* non native condition variables */
260 win32_cond = av_mallocz(sizeof(win32_cond_t));
261 if (!win32_cond)
262 return;
73ea3ffc 263 cond->Ptr = win32_cond;
27237d52
SW
264 win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
265 if (!win32_cond->semaphore)
266 return;
2003a292 267 win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
8b03d7fe
RB
268 if (!win32_cond->waiters_done)
269 return;
27237d52
SW
270
271 pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
8b03d7fe 272 pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
27237d52
SW
273}
274
6baeadd1 275static av_unused void pthread_cond_destroy(pthread_cond_t *cond)
27237d52 276{
73ea3ffc 277 win32_cond_t *win32_cond = cond->Ptr;
27237d52 278 /* native condition variables do not destroy */
ef51692a 279 if (cond_init)
27237d52
SW
280 return;
281
282 /* non native condition variables */
283 CloseHandle(win32_cond->semaphore);
8b03d7fe 284 CloseHandle(win32_cond->waiters_done);
27237d52 285 pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
8b03d7fe 286 pthread_mutex_destroy(&win32_cond->mtx_broadcast);
27237d52 287 av_freep(&win32_cond);
73ea3ffc 288 cond->Ptr = NULL;
27237d52
SW
289}
290
6baeadd1 291static av_unused void pthread_cond_broadcast(pthread_cond_t *cond)
27237d52 292{
73ea3ffc 293 win32_cond_t *win32_cond = cond->Ptr;
8b03d7fe
RB
294 int have_waiter;
295
ef51692a 296 if (cond_broadcast) {
27237d52
SW
297 cond_broadcast(cond);
298 return;
299 }
300
301 /* non native condition variables */
8b03d7fe 302 pthread_mutex_lock(&win32_cond->mtx_broadcast);
27237d52 303 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
8b03d7fe
RB
304 have_waiter = 0;
305
27237d52 306 if (win32_cond->waiter_count) {
8b03d7fe
RB
307 win32_cond->is_broadcast = 1;
308 have_waiter = 1;
27237d52 309 }
8b03d7fe
RB
310
311 if (have_waiter) {
312 ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
313 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
314 WaitForSingleObject(win32_cond->waiters_done, INFINITE);
01eb9805 315 ResetEvent(win32_cond->waiters_done);
8b03d7fe
RB
316 win32_cond->is_broadcast = 0;
317 } else
318 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
319 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
27237d52
SW
320}
321
6baeadd1 322static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
27237d52 323{
73ea3ffc 324 win32_cond_t *win32_cond = cond->Ptr;
8b03d7fe 325 int last_waiter;
ef51692a 326 if (cond_wait) {
27237d52 327 cond_wait(cond, mutex, INFINITE);
91dffb4c 328 return 0;
27237d52
SW
329 }
330
331 /* non native condition variables */
8b03d7fe 332 pthread_mutex_lock(&win32_cond->mtx_broadcast);
27237d52
SW
333 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
334 win32_cond->waiter_count++;
335 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
2003a292 336 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
27237d52 337
8b03d7fe 338 // unlock the external mutex
27237d52
SW
339 pthread_mutex_unlock(mutex);
340 WaitForSingleObject(win32_cond->semaphore, INFINITE);
8b03d7fe
RB
341
342 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
343 win32_cond->waiter_count--;
2003a292 344 last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
8b03d7fe
RB
345 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
346
347 if (last_waiter)
348 SetEvent(win32_cond->waiters_done);
349
350 // lock the external mutex
351 return pthread_mutex_lock(mutex);
27237d52
SW
352}
353
6baeadd1 354static av_unused void pthread_cond_signal(pthread_cond_t *cond)
27237d52 355{
73ea3ffc 356 win32_cond_t *win32_cond = cond->Ptr;
8b03d7fe 357 int have_waiter;
ef51692a 358 if (cond_signal) {
27237d52
SW
359 cond_signal(cond);
360 return;
361 }
362
2003a292
SR
363 pthread_mutex_lock(&win32_cond->mtx_broadcast);
364
27237d52
SW
365 /* non-native condition variables */
366 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
8b03d7fe 367 have_waiter = win32_cond->waiter_count;
27237d52 368 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
8b03d7fe 369
2003a292 370 if (have_waiter) {
8b03d7fe 371 ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
2003a292
SR
372 WaitForSingleObject(win32_cond->waiters_done, INFINITE);
373 ResetEvent(win32_cond->waiters_done);
374 }
375
376 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
27237d52 377}
b7c3bfd5 378#endif
27237d52 379
6baeadd1 380static av_unused void w32thread_init(void)
27237d52 381{
c360fb17 382#if _WIN32_WINNT < 0x0600
27237d52
SW
383 HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
384 /* if one is available, then they should all be available */
bd9cd046
DB
385 cond_init = (void (WINAPI*)(pthread_cond_t *))
386 GetProcAddress(kernel_dll, "InitializeConditionVariable");
387 cond_broadcast = (void (WINAPI*)(pthread_cond_t *))
388 GetProcAddress(kernel_dll, "WakeAllConditionVariable");
389 cond_signal = (void (WINAPI*)(pthread_cond_t *))
390 GetProcAddress(kernel_dll, "WakeConditionVariable");
391 cond_wait = (BOOL (WINAPI*)(pthread_cond_t *, pthread_mutex_t *, DWORD))
392 GetProcAddress(kernel_dll, "SleepConditionVariableCS");
393 initonce_begin = (BOOL (WINAPI*)(pthread_once_t *, DWORD, BOOL *, void **))
394 GetProcAddress(kernel_dll, "InitOnceBeginInitialize");
395 initonce_complete = (BOOL (WINAPI*)(pthread_once_t *, DWORD, void *))
396 GetProcAddress(kernel_dll, "InitOnceComplete");
c360fb17
JBK
397#endif
398
27237d52
SW
399}
400
439902e0 401#endif /* LIBAV_COMPAT_W32PTHREADS_H */