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 #include "libavutil/attributes.h"
43 #include "libavutil/common.h"
44 #include "libavutil/internal.h"
45 #include "libavutil/mem.h"
47 typedef struct pthread_t {
49 void *(*func)(void* arg);
54 /* the conditional variable api for windows 6.0+ uses critical sections and
56 typedef CRITICAL_SECTION pthread_mutex_t;
58 /* This is the CONDITION_VARIABLE typedef for using Windows' native
59 * conditional variables on kernels 6.0+. */
60 #if HAVE_CONDITION_VARIABLE_PTR
61 typedef CONDITION_VARIABLE pthread_cond_t;
63 typedef struct pthread_cond_t {
68 #if _WIN32_WINNT >= 0x0600
69 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
70 #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
73 static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
76 h->ret = h->func(h->arg);
80 static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
81 void *(*start_routine)(void*), void *arg)
83 thread->func = start_routine;
85 thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
87 return !thread->handle;
90 static av_unused int pthread_join(pthread_t thread, void **value_ptr)
92 DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
93 if (ret != WAIT_OBJECT_0) {
94 if (ret == WAIT_ABANDONED)
100 *value_ptr = thread.ret;
101 CloseHandle(thread.handle);
105 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
107 InitializeCriticalSection(m);
110 static inline int pthread_mutex_destroy(pthread_mutex_t *m)
112 DeleteCriticalSection(m);
115 static inline int pthread_mutex_lock(pthread_mutex_t *m)
117 EnterCriticalSection(m);
120 static inline int pthread_mutex_unlock(pthread_mutex_t *m)
122 LeaveCriticalSection(m);
126 #if _WIN32_WINNT >= 0x0600
127 typedef INIT_ONCE pthread_once_t;
128 #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
130 static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
132 BOOL pending = FALSE;
133 InitOnceBeginInitialize(once_control, 0, &pending, NULL);
136 InitOnceComplete(once_control, 0, NULL);
140 static inline int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
142 InitializeConditionVariable(cond);
146 /* native condition variables do not destroy */
147 static inline int pthread_cond_destroy(pthread_cond_t *cond)
152 static inline int pthread_cond_broadcast(pthread_cond_t *cond)
154 WakeAllConditionVariable(cond);
158 static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
160 SleepConditionVariableCS(cond, mutex, INFINITE);
164 static inline int pthread_cond_signal(pthread_cond_t *cond)
166 WakeConditionVariable(cond);
170 #else // _WIN32_WINNT < 0x0600
172 /* atomic init state of dynamically loaded functions */
173 static LONG w32thread_init_state = 0;
174 static av_unused void w32thread_init(void);
176 /* for pre-Windows 6.0 platforms, define INIT_ONCE struct,
177 * compatible to the one used in the native API */
179 typedef union pthread_once_t {
180 void * Ptr; ///< For the Windows 6.0+ native functions
181 LONG state; ///< For the pre-Windows 6.0 compat code
184 #define PTHREAD_ONCE_INIT {0}
186 /* function pointers to init once API on windows 6.0+ kernels */
187 static BOOL (WINAPI *initonce_begin)(pthread_once_t *lpInitOnce, DWORD dwFlags, BOOL *fPending, void **lpContext);
188 static BOOL (WINAPI *initonce_complete)(pthread_once_t *lpInitOnce, DWORD dwFlags, void *lpContext);
190 /* pre-Windows 6.0 compat using a spin-lock */
191 static inline void w32thread_once_fallback(LONG volatile *state, void (*init_routine)(void))
193 switch (InterlockedCompareExchange(state, 1, 0)) {
197 InterlockedExchange(state, 2);
199 /* Another thread is running init */
208 /* Initialization complete */
214 static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
216 w32thread_once_fallback(&w32thread_init_state, w32thread_init);
218 /* Use native functions on Windows 6.0+ */
219 if (initonce_begin && initonce_complete) {
220 BOOL pending = FALSE;
221 initonce_begin(once_control, 0, &pending, NULL);
224 initonce_complete(once_control, 0, NULL);
228 w32thread_once_fallback(&once_control->state, init_routine);
232 /* for pre-Windows 6.0 platforms we need to define and use our own condition
233 * variable and api */
235 typedef struct win32_cond_t {
236 pthread_mutex_t mtx_broadcast;
237 pthread_mutex_t mtx_waiter_count;
238 volatile int waiter_count;
241 volatile int is_broadcast;
244 /* function pointers to conditional variable API on windows 6.0+ kernels */
245 static void (WINAPI *cond_broadcast)(pthread_cond_t *cond);
246 static void (WINAPI *cond_init)(pthread_cond_t *cond);
247 static void (WINAPI *cond_signal)(pthread_cond_t *cond);
248 static BOOL (WINAPI *cond_wait)(pthread_cond_t *cond, pthread_mutex_t *mutex,
251 static av_unused int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
253 win32_cond_t *win32_cond = NULL;
255 w32thread_once_fallback(&w32thread_init_state, w32thread_init);
262 /* non native condition variables */
263 win32_cond = av_mallocz(sizeof(win32_cond_t));
266 cond->Ptr = win32_cond;
267 win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
268 if (!win32_cond->semaphore)
270 win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
271 if (!win32_cond->waiters_done)
274 pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
275 pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
279 static av_unused int pthread_cond_destroy(pthread_cond_t *cond)
281 win32_cond_t *win32_cond = cond->Ptr;
282 /* native condition variables do not destroy */
286 /* non native condition variables */
287 CloseHandle(win32_cond->semaphore);
288 CloseHandle(win32_cond->waiters_done);
289 pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
290 pthread_mutex_destroy(&win32_cond->mtx_broadcast);
291 av_freep(&win32_cond);
296 static av_unused int pthread_cond_broadcast(pthread_cond_t *cond)
298 win32_cond_t *win32_cond = cond->Ptr;
301 if (cond_broadcast) {
302 cond_broadcast(cond);
306 /* non native condition variables */
307 pthread_mutex_lock(&win32_cond->mtx_broadcast);
308 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
311 if (win32_cond->waiter_count) {
312 win32_cond->is_broadcast = 1;
317 ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
318 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
319 WaitForSingleObject(win32_cond->waiters_done, INFINITE);
320 ResetEvent(win32_cond->waiters_done);
321 win32_cond->is_broadcast = 0;
323 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
324 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
328 static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
330 win32_cond_t *win32_cond = cond->Ptr;
333 cond_wait(cond, mutex, INFINITE);
337 /* non native condition variables */
338 pthread_mutex_lock(&win32_cond->mtx_broadcast);
339 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
340 win32_cond->waiter_count++;
341 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
342 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
344 // unlock the external mutex
345 pthread_mutex_unlock(mutex);
346 WaitForSingleObject(win32_cond->semaphore, INFINITE);
348 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
349 win32_cond->waiter_count--;
350 last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
351 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
354 SetEvent(win32_cond->waiters_done);
356 // lock the external mutex
357 return pthread_mutex_lock(mutex);
360 static av_unused int pthread_cond_signal(pthread_cond_t *cond)
362 win32_cond_t *win32_cond = cond->Ptr;
369 pthread_mutex_lock(&win32_cond->mtx_broadcast);
371 /* non-native condition variables */
372 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
373 have_waiter = win32_cond->waiter_count;
374 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
377 ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
378 WaitForSingleObject(win32_cond->waiters_done, INFINITE);
379 ResetEvent(win32_cond->waiters_done);
382 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
387 static av_unused void w32thread_init(void)
389 #if _WIN32_WINNT < 0x0600
390 HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
391 /* if one is available, then they should all be available */
393 (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
395 (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
397 (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
399 (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
401 (void*)GetProcAddress(kernel_dll, "InitOnceBeginInitialize");
403 (void*)GetProcAddress(kernel_dll, "InitOnceComplete");
408 #endif /* FFMPEG_COMPAT_W32PTHREADS_H */