avconv: Do not try to configure filter outputs without streams
[libav.git] / compat / w32pthreads.h
CommitLineData
27237d52
SW
1/*
2 * Copyright (C) 2010-2011 x264 project
3 *
4 * Authors: Steven Walters <kemuri9@gmail.com>
5 * Pegasys Inc. <http://www.pegasys-inc.com>
6 *
7 * This file is part of Libav.
8 *
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.
13 *
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.
18 *
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
22 */
23
24/**
25 * @file
26 * w32threads to pthreads wrapper
27 */
28
439902e0
DB
29#ifndef LIBAV_COMPAT_W32PTHREADS_H
30#define LIBAV_COMPAT_W32PTHREADS_H
27237d52
SW
31
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
34 * library.
35 * As most functions here are used without checking return values,
36 * only implement return values as necessary. */
37
38#define WIN32_LEAN_AND_MEAN
39#include <windows.h>
40#include <process.h>
41
6baeadd1 42#include "libavutil/attributes.h"
11928d24
DB
43#include "libavutil/internal.h"
44#include "libavutil/mem.h"
45
e4cbf752 46typedef struct pthread_t {
27237d52
SW
47 void *handle;
48 void *(*func)(void* arg);
49 void *arg;
50 void *ret;
51} pthread_t;
52
53/* the conditional variable api for windows 6.0+ uses critical sections and
54 * not mutexes */
55typedef CRITICAL_SECTION pthread_mutex_t;
56
73ea3ffc
JA
57/* This is the CONDITION_VARIABLE typedef for using Windows' native
58 * conditional variables on kernels 6.0+. */
59#if HAVE_CONDITION_VARIABLE_PTR
60typedef CONDITION_VARIABLE pthread_cond_t;
61#else
e4cbf752 62typedef struct pthread_cond_t {
73ea3ffc 63 void *Ptr;
27237d52 64} pthread_cond_t;
73ea3ffc 65#endif
27237d52 66
b7c3bfd5 67#if _WIN32_WINNT >= 0x0600
428b0578
MS
68#define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
69#define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
4332bf98 70#endif
27237d52 71
6baeadd1 72static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
27237d52
SW
73{
74 pthread_t *h = arg;
75 h->ret = h->func(h->arg);
76 return 0;
77}
78
6baeadd1
DB
79static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
80 void *(*start_routine)(void*), void *arg)
27237d52
SW
81{
82 thread->func = start_routine;
83 thread->arg = arg;
84 thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
85 0, NULL);
86 return !thread->handle;
87}
88
6baeadd1 89static av_unused void pthread_join(pthread_t thread, void **value_ptr)
27237d52
SW
90{
91 DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
92 if (ret != WAIT_OBJECT_0)
93 return;
94 if (value_ptr)
95 *value_ptr = thread.ret;
96 CloseHandle(thread.handle);
97}
98
81486312
MS
99static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
100{
101 InitializeCriticalSection(m);
102 return 0;
103}
104static inline int pthread_mutex_destroy(pthread_mutex_t *m)
105{
106 DeleteCriticalSection(m);
107 return 0;
108}
109static inline int pthread_mutex_lock(pthread_mutex_t *m)
110{
111 EnterCriticalSection(m);
112 return 0;
113}
114static inline int pthread_mutex_unlock(pthread_mutex_t *m)
115{
116 LeaveCriticalSection(m);
117 return 0;
118}
27237d52 119
b7c3bfd5
JA
120#if _WIN32_WINNT >= 0x0600
121static inline void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
122{
123 InitializeConditionVariable(cond);
124}
125
126/* native condition variables do not destroy */
127static inline void pthread_cond_destroy(pthread_cond_t *cond)
128{
129 return;
130}
131
132static inline void pthread_cond_broadcast(pthread_cond_t *cond)
133{
134 WakeAllConditionVariable(cond);
135}
136
137static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
138{
139 SleepConditionVariableCS(cond, mutex, INFINITE);
140 return 0;
141}
142
143static inline void pthread_cond_signal(pthread_cond_t *cond)
144{
145 WakeConditionVariable(cond);
146}
147
148#else // _WIN32_WINNT < 0x0600
27237d52
SW
149/* for pre-Windows 6.0 platforms we need to define and use our own condition
150 * variable and api */
e4cbf752 151typedef struct win32_cond_t {
8b03d7fe 152 pthread_mutex_t mtx_broadcast;
27237d52
SW
153 pthread_mutex_t mtx_waiter_count;
154 volatile int waiter_count;
155 HANDLE semaphore;
8b03d7fe 156 HANDLE waiters_done;
01eb9805 157 volatile int is_broadcast;
27237d52
SW
158} win32_cond_t;
159
b7c3bfd5
JA
160/* function pointers to conditional variable API on windows 6.0+ kernels */
161static void (WINAPI *cond_broadcast)(pthread_cond_t *cond);
162static void (WINAPI *cond_init)(pthread_cond_t *cond);
163static void (WINAPI *cond_signal)(pthread_cond_t *cond);
164static BOOL (WINAPI *cond_wait)(pthread_cond_t *cond, pthread_mutex_t *mutex,
165 DWORD milliseconds);
166
6baeadd1 167static av_unused void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
27237d52
SW
168{
169 win32_cond_t *win32_cond = NULL;
ef51692a 170 if (cond_init) {
27237d52
SW
171 cond_init(cond);
172 return;
173 }
174
175 /* non native condition variables */
176 win32_cond = av_mallocz(sizeof(win32_cond_t));
177 if (!win32_cond)
178 return;
73ea3ffc 179 cond->Ptr = win32_cond;
27237d52
SW
180 win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
181 if (!win32_cond->semaphore)
182 return;
2003a292 183 win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
8b03d7fe
RB
184 if (!win32_cond->waiters_done)
185 return;
27237d52
SW
186
187 pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
8b03d7fe 188 pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
27237d52
SW
189}
190
6baeadd1 191static av_unused void pthread_cond_destroy(pthread_cond_t *cond)
27237d52 192{
73ea3ffc 193 win32_cond_t *win32_cond = cond->Ptr;
27237d52 194 /* native condition variables do not destroy */
ef51692a 195 if (cond_init)
27237d52
SW
196 return;
197
198 /* non native condition variables */
199 CloseHandle(win32_cond->semaphore);
8b03d7fe 200 CloseHandle(win32_cond->waiters_done);
27237d52 201 pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
8b03d7fe 202 pthread_mutex_destroy(&win32_cond->mtx_broadcast);
27237d52 203 av_freep(&win32_cond);
73ea3ffc 204 cond->Ptr = NULL;
27237d52
SW
205}
206
6baeadd1 207static av_unused void pthread_cond_broadcast(pthread_cond_t *cond)
27237d52 208{
73ea3ffc 209 win32_cond_t *win32_cond = cond->Ptr;
8b03d7fe
RB
210 int have_waiter;
211
ef51692a 212 if (cond_broadcast) {
27237d52
SW
213 cond_broadcast(cond);
214 return;
215 }
216
217 /* non native condition variables */
8b03d7fe 218 pthread_mutex_lock(&win32_cond->mtx_broadcast);
27237d52 219 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
8b03d7fe
RB
220 have_waiter = 0;
221
27237d52 222 if (win32_cond->waiter_count) {
8b03d7fe
RB
223 win32_cond->is_broadcast = 1;
224 have_waiter = 1;
27237d52 225 }
8b03d7fe
RB
226
227 if (have_waiter) {
228 ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
229 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
230 WaitForSingleObject(win32_cond->waiters_done, INFINITE);
01eb9805 231 ResetEvent(win32_cond->waiters_done);
8b03d7fe
RB
232 win32_cond->is_broadcast = 0;
233 } else
234 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
235 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
27237d52
SW
236}
237
6baeadd1 238static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
27237d52 239{
73ea3ffc 240 win32_cond_t *win32_cond = cond->Ptr;
8b03d7fe 241 int last_waiter;
ef51692a 242 if (cond_wait) {
27237d52 243 cond_wait(cond, mutex, INFINITE);
91dffb4c 244 return 0;
27237d52
SW
245 }
246
247 /* non native condition variables */
8b03d7fe 248 pthread_mutex_lock(&win32_cond->mtx_broadcast);
27237d52
SW
249 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
250 win32_cond->waiter_count++;
251 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
2003a292 252 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
27237d52 253
8b03d7fe 254 // unlock the external mutex
27237d52
SW
255 pthread_mutex_unlock(mutex);
256 WaitForSingleObject(win32_cond->semaphore, INFINITE);
8b03d7fe
RB
257
258 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
259 win32_cond->waiter_count--;
2003a292 260 last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
8b03d7fe
RB
261 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
262
263 if (last_waiter)
264 SetEvent(win32_cond->waiters_done);
265
266 // lock the external mutex
267 return pthread_mutex_lock(mutex);
27237d52
SW
268}
269
6baeadd1 270static av_unused void pthread_cond_signal(pthread_cond_t *cond)
27237d52 271{
73ea3ffc 272 win32_cond_t *win32_cond = cond->Ptr;
8b03d7fe 273 int have_waiter;
ef51692a 274 if (cond_signal) {
27237d52
SW
275 cond_signal(cond);
276 return;
277 }
278
2003a292
SR
279 pthread_mutex_lock(&win32_cond->mtx_broadcast);
280
27237d52
SW
281 /* non-native condition variables */
282 pthread_mutex_lock(&win32_cond->mtx_waiter_count);
8b03d7fe 283 have_waiter = win32_cond->waiter_count;
27237d52 284 pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
8b03d7fe 285
2003a292 286 if (have_waiter) {
8b03d7fe 287 ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
2003a292
SR
288 WaitForSingleObject(win32_cond->waiters_done, INFINITE);
289 ResetEvent(win32_cond->waiters_done);
290 }
291
292 pthread_mutex_unlock(&win32_cond->mtx_broadcast);
27237d52 293}
b7c3bfd5 294#endif
27237d52 295
6baeadd1 296static av_unused void w32thread_init(void)
27237d52 297{
c360fb17 298#if _WIN32_WINNT < 0x0600
27237d52
SW
299 HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
300 /* if one is available, then they should all be available */
301 cond_init =
302 (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
303 cond_broadcast =
304 (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
305 cond_signal =
306 (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
307 cond_wait =
308 (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
c360fb17
JBK
309#endif
310
27237d52
SW
311}
312
439902e0 313#endif /* LIBAV_COMPAT_W32PTHREADS_H */