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 Libav.
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.
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.
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
26 * w32threads to pthreads wrapper
29 #ifndef AVCODEC_W32PTHREADS_H
30 #define AVCODEC_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"
46 void *(*func)(void* arg);
51 /* the conditional variable api for windows 6.0+ uses critical sections and
53 typedef CRITICAL_SECTION pthread_mutex_t;
55 /* This is the CONDITIONAL_VARIABLE typedef for using Window's native
56 * conditional variables on kernels 6.0+.
57 * MinGW does not currently have this typedef. */
62 /* function pointers to conditional variable API on windows 6.0+ kernels */
63 static void (WINAPI *cond_broadcast)(pthread_cond_t *cond);
64 static void (WINAPI *cond_init)(pthread_cond_t *cond);
65 static void (WINAPI *cond_signal)(pthread_cond_t *cond);
66 static BOOL (WINAPI *cond_wait)(pthread_cond_t *cond, pthread_mutex_t *mutex,
69 static unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
72 h->ret = h->func(h->arg);
76 static int pthread_create(pthread_t *thread, const void *unused_attr,
77 void *(*start_routine)(void*), void *arg)
79 thread->func = start_routine;
81 thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
83 return !thread->handle;
86 static void pthread_join(pthread_t thread, void **value_ptr)
88 DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
89 if (ret != WAIT_OBJECT_0)
92 *value_ptr = thread.ret;
93 CloseHandle(thread.handle);
96 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
98 InitializeCriticalSection(m);
101 static inline int pthread_mutex_destroy(pthread_mutex_t *m)
103 DeleteCriticalSection(m);
106 static inline int pthread_mutex_lock(pthread_mutex_t *m)
108 EnterCriticalSection(m);
111 static inline int pthread_mutex_unlock(pthread_mutex_t *m)
113 LeaveCriticalSection(m);
117 /* for pre-Windows 6.0 platforms we need to define and use our own condition
118 * variable and api */
120 pthread_mutex_t mtx_broadcast;
121 pthread_mutex_t mtx_waiter_count;
122 volatile int waiter_count;
125 volatile int is_broadcast;
128 static void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
130 win32_cond_t *win32_cond = NULL;
136 /* non native condition variables */
137 win32_cond = av_mallocz(sizeof(win32_cond_t));
140 cond->ptr = win32_cond;
141 win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
142 if (!win32_cond->semaphore)
144 win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
145 if (!win32_cond->waiters_done)
148 pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
149 pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
152 static void pthread_cond_destroy(pthread_cond_t *cond)
154 win32_cond_t *win32_cond = cond->ptr;
155 /* native condition variables do not destroy */
159 /* non native condition variables */
160 CloseHandle(win32_cond->semaphore);
161 CloseHandle(win32_cond->waiters_done);
162 pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
163 pthread_mutex_destroy(&win32_cond->mtx_broadcast);
164 av_freep(&win32_cond);
168 static void pthread_cond_broadcast(pthread_cond_t *cond)
170 win32_cond_t *win32_cond = cond->ptr;
173 if (cond_broadcast) {
174 cond_broadcast(cond);
178 /* non native condition variables */
179 pthread_mutex_lock(&win32_cond->mtx_broadcast);
180 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
183 if (win32_cond->waiter_count) {
184 win32_cond->is_broadcast = 1;
189 ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
190 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
191 WaitForSingleObject(win32_cond->waiters_done, INFINITE);
192 ResetEvent(win32_cond->waiters_done);
193 win32_cond->is_broadcast = 0;
195 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
196 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
199 static int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
201 win32_cond_t *win32_cond = cond->ptr;
204 cond_wait(cond, mutex, INFINITE);
208 /* non native condition variables */
209 pthread_mutex_lock(&win32_cond->mtx_broadcast);
210 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
211 win32_cond->waiter_count++;
212 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
213 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
215 // unlock the external mutex
216 pthread_mutex_unlock(mutex);
217 WaitForSingleObject(win32_cond->semaphore, INFINITE);
219 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
220 win32_cond->waiter_count--;
221 last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
222 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
225 SetEvent(win32_cond->waiters_done);
227 // lock the external mutex
228 return pthread_mutex_lock(mutex);
231 static void pthread_cond_signal(pthread_cond_t *cond)
233 win32_cond_t *win32_cond = cond->ptr;
240 pthread_mutex_lock(&win32_cond->mtx_broadcast);
242 /* non-native condition variables */
243 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
244 have_waiter = win32_cond->waiter_count;
245 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
248 ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
249 WaitForSingleObject(win32_cond->waiters_done, INFINITE);
250 ResetEvent(win32_cond->waiters_done);
253 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
256 static void w32thread_init(void)
258 HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
259 /* if one is available, then they should all be available */
261 (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
263 (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
265 (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
267 (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
270 #endif /* AVCODEC_W32PTHREADS_H */