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