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
44 void *(*func)(void* arg);
49 /* the conditional variable api for windows 6.0+ uses critical sections and
51 typedef CRITICAL_SECTION pthread_mutex_t;
53 /* This is the CONDITIONAL_VARIABLE typedef for using Window's native
54 * conditional variables on kernels 6.0+.
55 * MinGW does not currently have this typedef. */
60 /* function pointers to conditional variable API on windows 6.0+ kernels */
61 static void (WINAPI *cond_broadcast)(pthread_cond_t *cond);
62 static void (WINAPI *cond_init)(pthread_cond_t *cond);
63 static void (WINAPI *cond_signal)(pthread_cond_t *cond);
64 static BOOL (WINAPI *cond_wait)(pthread_cond_t *cond, pthread_mutex_t *mutex,
67 static unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
70 h->ret = h->func(h->arg);
74 static int pthread_create(pthread_t *thread, const void *unused_attr,
75 void *(*start_routine)(void*), void *arg)
77 thread->func = start_routine;
79 thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
81 return !thread->handle;
84 static void pthread_join(pthread_t thread, void **value_ptr)
86 DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
87 if (ret != WAIT_OBJECT_0)
90 *value_ptr = thread.ret;
91 CloseHandle(thread.handle);
94 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
96 InitializeCriticalSection(m);
99 static inline int pthread_mutex_destroy(pthread_mutex_t *m)
101 DeleteCriticalSection(m);
104 static inline int pthread_mutex_lock(pthread_mutex_t *m)
106 EnterCriticalSection(m);
109 static inline int pthread_mutex_unlock(pthread_mutex_t *m)
111 LeaveCriticalSection(m);
115 /* for pre-Windows 6.0 platforms we need to define and use our own condition
116 * variable and api */
118 pthread_mutex_t mtx_waiter_count;
119 volatile int waiter_count;
123 static void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
125 win32_cond_t *win32_cond = NULL;
131 /* non native condition variables */
132 win32_cond = av_mallocz(sizeof(win32_cond_t));
135 cond->ptr = win32_cond;
136 win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
137 if (!win32_cond->semaphore)
140 pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
143 static void pthread_cond_destroy(pthread_cond_t *cond)
145 win32_cond_t *win32_cond = cond->ptr;
146 /* native condition variables do not destroy */
150 /* non native condition variables */
151 CloseHandle(win32_cond->semaphore);
152 pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
153 av_freep(&win32_cond);
157 static void pthread_cond_broadcast(pthread_cond_t *cond)
159 win32_cond_t *win32_cond = cond->ptr;
160 if (cond_broadcast) {
161 cond_broadcast(cond);
165 /* non native condition variables */
166 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
167 if (win32_cond->waiter_count) {
168 ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
169 win32_cond->waiter_count = 0;
171 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
174 static void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
176 win32_cond_t *win32_cond = cond->ptr;
178 cond_wait(cond, mutex, INFINITE);
182 /* non native condition variables */
183 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
184 win32_cond->waiter_count++;
185 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
187 pthread_mutex_unlock(mutex);
188 WaitForSingleObject(win32_cond->semaphore, INFINITE);
189 pthread_mutex_lock(mutex);
192 static void pthread_cond_signal(pthread_cond_t *cond)
194 win32_cond_t *win32_cond = cond->ptr;
200 /* non-native condition variables */
201 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
202 if (win32_cond->waiter_count) {
203 ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
204 win32_cond->waiter_count--;
206 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
209 static void w32thread_init(void)
211 HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
212 /* if one is available, then they should all be available */
214 (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
216 (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
218 (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
220 (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
223 #endif /* AVCODEC_W32PTHREADS_H */