2 * Copyright (C) 2010-2011 x264 project
4 * Authors: Steven Walters <kemuri9@gmail.com>
5 * Pegasys Inc. <http://www.pegasys-inc.com>
7 * This file is part of FFmpeg.
9 * FFmpeg 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.
14 * FFmpeg 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.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * w32threads to pthreads wrapper
29 #ifndef FFMPEG_COMPAT_W32PTHREADS_H
30 #define FFMPEG_COMPAT_W32PTHREADS_H
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
35 * As most functions here are used without checking return values,
36 * only implement return values as necessary. */
38 #define WIN32_LEAN_AND_MEAN
42 /* MinGW requires the intrinsics header for the pthread_once fallback code */
43 #if _WIN32_WINNT < 0x0600 && defined(__MINGW32__)
47 #include "libavutil/attributes.h"
48 #include "libavutil/common.h"
49 #include "libavutil/internal.h"
50 #include "libavutil/mem.h"
52 typedef struct pthread_t {
54 void *(*func)(void* arg);
59 /* the conditional variable api for windows 6.0+ uses critical sections and
61 typedef CRITICAL_SECTION pthread_mutex_t;
63 /* This is the CONDITION_VARIABLE typedef for using Windows' native
64 * conditional variables on kernels 6.0+. */
65 #if HAVE_CONDITION_VARIABLE_PTR
66 typedef CONDITION_VARIABLE pthread_cond_t;
68 typedef struct pthread_cond_t {
73 #if _WIN32_WINNT >= 0x0600
74 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
75 #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
78 static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
81 h->ret = h->func(h->arg);
85 static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
86 void *(*start_routine)(void*), void *arg)
88 thread->func = start_routine;
91 thread->handle = (void*)CreateThread(NULL, 0, win32thread_worker, thread,
94 thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
97 return !thread->handle;
100 static av_unused int pthread_join(pthread_t thread, void **value_ptr)
102 DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
103 if (ret != WAIT_OBJECT_0) {
104 if (ret == WAIT_ABANDONED)
110 *value_ptr = thread.ret;
111 CloseHandle(thread.handle);
115 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
117 InitializeCriticalSection(m);
120 static inline int pthread_mutex_destroy(pthread_mutex_t *m)
122 DeleteCriticalSection(m);
125 static inline int pthread_mutex_lock(pthread_mutex_t *m)
127 EnterCriticalSection(m);
130 static inline int pthread_mutex_unlock(pthread_mutex_t *m)
132 LeaveCriticalSection(m);
136 #if _WIN32_WINNT >= 0x0600
137 typedef INIT_ONCE pthread_once_t;
138 #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
140 static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
142 BOOL pending = FALSE;
143 InitOnceBeginInitialize(once_control, 0, &pending, NULL);
146 InitOnceComplete(once_control, 0, NULL);
150 static inline int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
152 InitializeConditionVariable(cond);
156 /* native condition variables do not destroy */
157 static inline int pthread_cond_destroy(pthread_cond_t *cond)
162 static inline int pthread_cond_broadcast(pthread_cond_t *cond)
164 WakeAllConditionVariable(cond);
168 static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
170 SleepConditionVariableCS(cond, mutex, INFINITE);
174 static inline int pthread_cond_signal(pthread_cond_t *cond)
176 WakeConditionVariable(cond);
180 #else // _WIN32_WINNT < 0x0600
182 /* atomic init state of dynamically loaded functions */
183 static LONG w32thread_init_state = 0;
184 static av_unused void w32thread_init(void);
186 /* for pre-Windows 6.0 platforms, define INIT_ONCE struct,
187 * compatible to the one used in the native API */
189 typedef union pthread_once_t {
190 void * Ptr; ///< For the Windows 6.0+ native functions
191 LONG state; ///< For the pre-Windows 6.0 compat code
194 #define PTHREAD_ONCE_INIT {0}
196 /* function pointers to init once API on windows 6.0+ kernels */
197 static BOOL (WINAPI *initonce_begin)(pthread_once_t *lpInitOnce, DWORD dwFlags, BOOL *fPending, void **lpContext);
198 static BOOL (WINAPI *initonce_complete)(pthread_once_t *lpInitOnce, DWORD dwFlags, void *lpContext);
200 /* pre-Windows 6.0 compat using a spin-lock */
201 static inline void w32thread_once_fallback(LONG volatile *state, void (*init_routine)(void))
203 switch (InterlockedCompareExchange(state, 1, 0)) {
207 InterlockedExchange(state, 2);
209 /* Another thread is running init */
218 /* Initialization complete */
224 static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
226 w32thread_once_fallback(&w32thread_init_state, w32thread_init);
228 /* Use native functions on Windows 6.0+ */
229 if (initonce_begin && initonce_complete) {
230 BOOL pending = FALSE;
231 initonce_begin(once_control, 0, &pending, NULL);
234 initonce_complete(once_control, 0, NULL);
238 w32thread_once_fallback(&once_control->state, init_routine);
242 /* for pre-Windows 6.0 platforms we need to define and use our own condition
243 * variable and api */
245 typedef struct win32_cond_t {
246 pthread_mutex_t mtx_broadcast;
247 pthread_mutex_t mtx_waiter_count;
248 volatile int waiter_count;
251 volatile int is_broadcast;
254 /* function pointers to conditional variable API on windows 6.0+ kernels */
255 static void (WINAPI *cond_broadcast)(pthread_cond_t *cond);
256 static void (WINAPI *cond_init)(pthread_cond_t *cond);
257 static void (WINAPI *cond_signal)(pthread_cond_t *cond);
258 static BOOL (WINAPI *cond_wait)(pthread_cond_t *cond, pthread_mutex_t *mutex,
261 static av_unused int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
263 win32_cond_t *win32_cond = NULL;
265 w32thread_once_fallback(&w32thread_init_state, w32thread_init);
272 /* non native condition variables */
273 win32_cond = av_mallocz(sizeof(win32_cond_t));
276 cond->Ptr = win32_cond;
277 win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
278 if (!win32_cond->semaphore)
280 win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
281 if (!win32_cond->waiters_done)
284 pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
285 pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
289 static av_unused int pthread_cond_destroy(pthread_cond_t *cond)
291 win32_cond_t *win32_cond = cond->Ptr;
292 /* native condition variables do not destroy */
296 /* non native condition variables */
297 CloseHandle(win32_cond->semaphore);
298 CloseHandle(win32_cond->waiters_done);
299 pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
300 pthread_mutex_destroy(&win32_cond->mtx_broadcast);
301 av_freep(&win32_cond);
306 static av_unused int pthread_cond_broadcast(pthread_cond_t *cond)
308 win32_cond_t *win32_cond = cond->Ptr;
311 if (cond_broadcast) {
312 cond_broadcast(cond);
316 /* non native condition variables */
317 pthread_mutex_lock(&win32_cond->mtx_broadcast);
318 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
321 if (win32_cond->waiter_count) {
322 win32_cond->is_broadcast = 1;
327 ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
328 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
329 WaitForSingleObject(win32_cond->waiters_done, INFINITE);
330 ResetEvent(win32_cond->waiters_done);
331 win32_cond->is_broadcast = 0;
333 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
334 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
338 static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
340 win32_cond_t *win32_cond = cond->Ptr;
343 cond_wait(cond, mutex, INFINITE);
347 /* non native condition variables */
348 pthread_mutex_lock(&win32_cond->mtx_broadcast);
349 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
350 win32_cond->waiter_count++;
351 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
352 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
354 // unlock the external mutex
355 pthread_mutex_unlock(mutex);
356 WaitForSingleObject(win32_cond->semaphore, INFINITE);
358 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
359 win32_cond->waiter_count--;
360 last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
361 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
364 SetEvent(win32_cond->waiters_done);
366 // lock the external mutex
367 return pthread_mutex_lock(mutex);
370 static av_unused int pthread_cond_signal(pthread_cond_t *cond)
372 win32_cond_t *win32_cond = cond->Ptr;
379 pthread_mutex_lock(&win32_cond->mtx_broadcast);
381 /* non-native condition variables */
382 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
383 have_waiter = win32_cond->waiter_count;
384 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
387 ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
388 WaitForSingleObject(win32_cond->waiters_done, INFINITE);
389 ResetEvent(win32_cond->waiters_done);
392 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
397 static av_unused void w32thread_init(void)
399 #if _WIN32_WINNT < 0x0600
400 HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
401 /* if one is available, then they should all be available */
403 (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
405 (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
407 (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
409 (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
411 (void*)GetProcAddress(kernel_dll, "InitOnceBeginInitialize");
413 (void*)GetProcAddress(kernel_dll, "InitOnceComplete");
418 #endif /* FFMPEG_COMPAT_W32PTHREADS_H */