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/common.h"
43 #include "libavutil/internal.h"
44 #include "libavutil/mem.h"
46 typedef struct pthread_t {
48 void *(*func)(void* arg);
53 /* the conditional variable api for windows 6.0+ uses critical sections and
55 typedef CRITICAL_SECTION pthread_mutex_t;
57 /* This is the CONDITIONAL_VARIABLE typedef for using Window's native
58 * conditional variables on kernels 6.0+.
59 * MinGW does not currently have this typedef. */
60 typedef struct pthread_cond_t {
64 /* function pointers to conditional variable API on windows 6.0+ kernels */
65 #if _WIN32_WINNT < 0x0600
66 static void (WINAPI *cond_broadcast)(pthread_cond_t *cond);
67 static void (WINAPI *cond_init)(pthread_cond_t *cond);
68 static void (WINAPI *cond_signal)(pthread_cond_t *cond);
69 static BOOL (WINAPI *cond_wait)(pthread_cond_t *cond, pthread_mutex_t *mutex,
72 #define cond_init InitializeConditionVariable
73 #define cond_broadcast WakeAllConditionVariable
74 #define cond_signal WakeConditionVariable
75 #define cond_wait SleepConditionVariableCS
77 #define CreateEvent(a, reset, init, name) \
78 CreateEventEx(a, name, \
79 (reset ? CREATE_EVENT_MANUAL_RESET : 0) | \
80 (init ? CREATE_EVENT_INITIAL_SET : 0), \
82 // CreateSemaphoreExA seems to be desktop-only, but as long as we don't
83 // use named semaphores, it doesn't matter if we use the W version.
84 #define CreateSemaphore(a, b, c, d) \
85 CreateSemaphoreExW(a, b, c, d, 0, SEMAPHORE_ALL_ACCESS)
86 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
87 #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
90 static unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
93 h->ret = h->func(h->arg);
97 static int pthread_create(pthread_t *thread, const void *unused_attr,
98 void *(*start_routine)(void*), void *arg)
100 thread->func = start_routine;
102 thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
104 return !thread->handle;
107 static void pthread_join(pthread_t thread, void **value_ptr)
109 DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
110 if (ret != WAIT_OBJECT_0)
113 *value_ptr = thread.ret;
114 CloseHandle(thread.handle);
117 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
119 InitializeCriticalSection(m);
122 static inline int pthread_mutex_destroy(pthread_mutex_t *m)
124 DeleteCriticalSection(m);
127 static inline int pthread_mutex_lock(pthread_mutex_t *m)
129 EnterCriticalSection(m);
132 static inline int pthread_mutex_unlock(pthread_mutex_t *m)
134 LeaveCriticalSection(m);
138 /* for pre-Windows 6.0 platforms we need to define and use our own condition
139 * variable and api */
140 typedef struct win32_cond_t {
141 pthread_mutex_t mtx_broadcast;
142 pthread_mutex_t mtx_waiter_count;
143 volatile int waiter_count;
146 volatile int is_broadcast;
149 static int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
151 win32_cond_t *win32_cond = NULL;
157 /* non native condition variables */
158 win32_cond = av_mallocz(sizeof(win32_cond_t));
161 cond->ptr = win32_cond;
162 win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
163 if (!win32_cond->semaphore)
165 win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
166 if (!win32_cond->waiters_done)
169 pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
170 pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
174 static void pthread_cond_destroy(pthread_cond_t *cond)
176 win32_cond_t *win32_cond = cond->ptr;
177 /* native condition variables do not destroy */
181 /* non native condition variables */
182 CloseHandle(win32_cond->semaphore);
183 CloseHandle(win32_cond->waiters_done);
184 pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
185 pthread_mutex_destroy(&win32_cond->mtx_broadcast);
186 av_freep(&win32_cond);
190 static void pthread_cond_broadcast(pthread_cond_t *cond)
192 win32_cond_t *win32_cond = cond->ptr;
195 if (cond_broadcast) {
196 cond_broadcast(cond);
200 /* non native condition variables */
201 pthread_mutex_lock(&win32_cond->mtx_broadcast);
202 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
205 if (win32_cond->waiter_count) {
206 win32_cond->is_broadcast = 1;
211 ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
212 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
213 WaitForSingleObject(win32_cond->waiters_done, INFINITE);
214 ResetEvent(win32_cond->waiters_done);
215 win32_cond->is_broadcast = 0;
217 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
218 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
221 static int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
223 win32_cond_t *win32_cond = cond->ptr;
226 cond_wait(cond, mutex, INFINITE);
230 /* non native condition variables */
231 pthread_mutex_lock(&win32_cond->mtx_broadcast);
232 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
233 win32_cond->waiter_count++;
234 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
235 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
237 // unlock the external mutex
238 pthread_mutex_unlock(mutex);
239 WaitForSingleObject(win32_cond->semaphore, INFINITE);
241 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
242 win32_cond->waiter_count--;
243 last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
244 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
247 SetEvent(win32_cond->waiters_done);
249 // lock the external mutex
250 return pthread_mutex_lock(mutex);
253 static void pthread_cond_signal(pthread_cond_t *cond)
255 win32_cond_t *win32_cond = cond->ptr;
262 pthread_mutex_lock(&win32_cond->mtx_broadcast);
264 /* non-native condition variables */
265 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
266 have_waiter = win32_cond->waiter_count;
267 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
270 ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
271 WaitForSingleObject(win32_cond->waiters_done, INFINITE);
272 ResetEvent(win32_cond->waiters_done);
275 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
278 static void w32thread_init(void)
280 #if _WIN32_WINNT < 0x0600
281 HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
282 /* if one is available, then they should all be available */
284 (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
286 (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
288 (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
290 (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
295 #endif /* FFMPEG_COMPAT_W32PTHREADS_H */