w32pthreads: use the CONDITION_VARIABLE typedef if available
[ffmpeg.git] / compat / w32pthreads.h
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
29 #ifndef LIBAV_COMPAT_W32PTHREADS_H
30 #define LIBAV_COMPAT_W32PTHREADS_H
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
42 #include "libavutil/attributes.h"
43 #include "libavutil/internal.h"
44 #include "libavutil/mem.h"
45
46 typedef struct pthread_t {
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 */
55 typedef CRITICAL_SECTION pthread_mutex_t;
56
57 /* This is the CONDITION_VARIABLE typedef for using Windows' native
58  * conditional variables on kernels 6.0+. */
59 #if HAVE_CONDITION_VARIABLE_PTR
60 typedef CONDITION_VARIABLE pthread_cond_t;
61 #else
62 typedef struct pthread_cond_t {
63     void *Ptr;
64 } pthread_cond_t;
65 #endif
66
67 /* function pointers to conditional variable API on windows 6.0+ kernels */
68 #if _WIN32_WINNT < 0x0600
69 static void (WINAPI *cond_broadcast)(pthread_cond_t *cond);
70 static void (WINAPI *cond_init)(pthread_cond_t *cond);
71 static void (WINAPI *cond_signal)(pthread_cond_t *cond);
72 static BOOL (WINAPI *cond_wait)(pthread_cond_t *cond, pthread_mutex_t *mutex,
73                                 DWORD milliseconds);
74 #else
75 #define cond_init      InitializeConditionVariable
76 #define cond_broadcast WakeAllConditionVariable
77 #define cond_signal    WakeConditionVariable
78 #define cond_wait      SleepConditionVariableCS
79
80 #define CreateEvent(a, reset, init, name)                   \
81     CreateEventEx(a, name,                                  \
82                   (reset ? CREATE_EVENT_MANUAL_RESET : 0) | \
83                   (init ? CREATE_EVENT_INITIAL_SET : 0),    \
84                   EVENT_ALL_ACCESS)
85 // CreateSemaphoreExA seems to be desktop-only, but as long as we don't
86 // use named semaphores, it doesn't matter if we use the W version.
87 #define CreateSemaphore(a, b, c, d) \
88     CreateSemaphoreExW(a, b, c, d, 0, SEMAPHORE_ALL_ACCESS)
89 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
90 #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
91 #endif
92
93 static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
94 {
95     pthread_t *h = arg;
96     h->ret = h->func(h->arg);
97     return 0;
98 }
99
100 static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
101                                     void *(*start_routine)(void*), void *arg)
102 {
103     thread->func   = start_routine;
104     thread->arg    = arg;
105     thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
106                                            0, NULL);
107     return !thread->handle;
108 }
109
110 static av_unused void pthread_join(pthread_t thread, void **value_ptr)
111 {
112     DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
113     if (ret != WAIT_OBJECT_0)
114         return;
115     if (value_ptr)
116         *value_ptr = thread.ret;
117     CloseHandle(thread.handle);
118 }
119
120 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
121 {
122     InitializeCriticalSection(m);
123     return 0;
124 }
125 static inline int pthread_mutex_destroy(pthread_mutex_t *m)
126 {
127     DeleteCriticalSection(m);
128     return 0;
129 }
130 static inline int pthread_mutex_lock(pthread_mutex_t *m)
131 {
132     EnterCriticalSection(m);
133     return 0;
134 }
135 static inline int pthread_mutex_unlock(pthread_mutex_t *m)
136 {
137     LeaveCriticalSection(m);
138     return 0;
139 }
140
141 /* for pre-Windows 6.0 platforms we need to define and use our own condition
142  * variable and api */
143 typedef struct  win32_cond_t {
144     pthread_mutex_t mtx_broadcast;
145     pthread_mutex_t mtx_waiter_count;
146     volatile int waiter_count;
147     HANDLE semaphore;
148     HANDLE waiters_done;
149     volatile int is_broadcast;
150 } win32_cond_t;
151
152 static av_unused void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
153 {
154     win32_cond_t *win32_cond = NULL;
155     if (cond_init) {
156         cond_init(cond);
157         return;
158     }
159
160     /* non native condition variables */
161     win32_cond = av_mallocz(sizeof(win32_cond_t));
162     if (!win32_cond)
163         return;
164     cond->Ptr = win32_cond;
165     win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
166     if (!win32_cond->semaphore)
167         return;
168     win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
169     if (!win32_cond->waiters_done)
170         return;
171
172     pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
173     pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
174 }
175
176 static av_unused void pthread_cond_destroy(pthread_cond_t *cond)
177 {
178     win32_cond_t *win32_cond = cond->Ptr;
179     /* native condition variables do not destroy */
180     if (cond_init)
181         return;
182
183     /* non native condition variables */
184     CloseHandle(win32_cond->semaphore);
185     CloseHandle(win32_cond->waiters_done);
186     pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
187     pthread_mutex_destroy(&win32_cond->mtx_broadcast);
188     av_freep(&win32_cond);
189     cond->Ptr = NULL;
190 }
191
192 static av_unused void pthread_cond_broadcast(pthread_cond_t *cond)
193 {
194     win32_cond_t *win32_cond = cond->Ptr;
195     int have_waiter;
196
197     if (cond_broadcast) {
198         cond_broadcast(cond);
199         return;
200     }
201
202     /* non native condition variables */
203     pthread_mutex_lock(&win32_cond->mtx_broadcast);
204     pthread_mutex_lock(&win32_cond->mtx_waiter_count);
205     have_waiter = 0;
206
207     if (win32_cond->waiter_count) {
208         win32_cond->is_broadcast = 1;
209         have_waiter = 1;
210     }
211
212     if (have_waiter) {
213         ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
214         pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
215         WaitForSingleObject(win32_cond->waiters_done, INFINITE);
216         ResetEvent(win32_cond->waiters_done);
217         win32_cond->is_broadcast = 0;
218     } else
219         pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
220     pthread_mutex_unlock(&win32_cond->mtx_broadcast);
221 }
222
223 static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
224 {
225     win32_cond_t *win32_cond = cond->Ptr;
226     int last_waiter;
227     if (cond_wait) {
228         cond_wait(cond, mutex, INFINITE);
229         return 0;
230     }
231
232     /* non native condition variables */
233     pthread_mutex_lock(&win32_cond->mtx_broadcast);
234     pthread_mutex_lock(&win32_cond->mtx_waiter_count);
235     win32_cond->waiter_count++;
236     pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
237     pthread_mutex_unlock(&win32_cond->mtx_broadcast);
238
239     // unlock the external mutex
240     pthread_mutex_unlock(mutex);
241     WaitForSingleObject(win32_cond->semaphore, INFINITE);
242
243     pthread_mutex_lock(&win32_cond->mtx_waiter_count);
244     win32_cond->waiter_count--;
245     last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
246     pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
247
248     if (last_waiter)
249         SetEvent(win32_cond->waiters_done);
250
251     // lock the external mutex
252     return pthread_mutex_lock(mutex);
253 }
254
255 static av_unused void pthread_cond_signal(pthread_cond_t *cond)
256 {
257     win32_cond_t *win32_cond = cond->Ptr;
258     int have_waiter;
259     if (cond_signal) {
260         cond_signal(cond);
261         return;
262     }
263
264     pthread_mutex_lock(&win32_cond->mtx_broadcast);
265
266     /* non-native condition variables */
267     pthread_mutex_lock(&win32_cond->mtx_waiter_count);
268     have_waiter = win32_cond->waiter_count;
269     pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
270
271     if (have_waiter) {
272         ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
273         WaitForSingleObject(win32_cond->waiters_done, INFINITE);
274         ResetEvent(win32_cond->waiters_done);
275     }
276
277     pthread_mutex_unlock(&win32_cond->mtx_broadcast);
278 }
279
280 static av_unused void w32thread_init(void)
281 {
282 #if _WIN32_WINNT < 0x0600
283     HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
284     /* if one is available, then they should all be available */
285     cond_init      =
286         (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
287     cond_broadcast =
288         (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
289     cond_signal    =
290         (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
291     cond_wait      =
292         (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
293 #endif
294
295 }
296
297 #endif /* LIBAV_COMPAT_W32PTHREADS_H */