w32threads: Make pthread_cond_wait follow POSIX
[ffmpeg.git] / libavcodec / 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 AVCODEC_W32PTHREADS_H
30 #define AVCODEC_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 typedef struct {
43     void *handle;
44     void *(*func)(void* arg);
45     void *arg;
46     void *ret;
47 } pthread_t;
48
49 /* the conditional variable api for windows 6.0+ uses critical sections and
50  * not mutexes */
51 typedef CRITICAL_SECTION pthread_mutex_t;
52
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. */
56 typedef struct {
57     void *ptr;
58 } pthread_cond_t;
59
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,
65                                 DWORD milliseconds);
66
67 static unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
68 {
69     pthread_t *h = arg;
70     h->ret = h->func(h->arg);
71     return 0;
72 }
73
74 static int pthread_create(pthread_t *thread, const void *unused_attr,
75                           void *(*start_routine)(void*), void *arg)
76 {
77     thread->func   = start_routine;
78     thread->arg    = arg;
79     thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
80                                            0, NULL);
81     return !thread->handle;
82 }
83
84 static void pthread_join(pthread_t thread, void **value_ptr)
85 {
86     DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
87     if (ret != WAIT_OBJECT_0)
88         return;
89     if (value_ptr)
90         *value_ptr = thread.ret;
91     CloseHandle(thread.handle);
92 }
93
94 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
95 {
96     InitializeCriticalSection(m);
97     return 0;
98 }
99 static inline int pthread_mutex_destroy(pthread_mutex_t *m)
100 {
101     DeleteCriticalSection(m);
102     return 0;
103 }
104 static inline int pthread_mutex_lock(pthread_mutex_t *m)
105 {
106     EnterCriticalSection(m);
107     return 0;
108 }
109 static inline int pthread_mutex_unlock(pthread_mutex_t *m)
110 {
111     LeaveCriticalSection(m);
112     return 0;
113 }
114
115 /* for pre-Windows 6.0 platforms we need to define and use our own condition
116  * variable and api */
117 typedef struct {
118     pthread_mutex_t mtx_broadcast;
119     pthread_mutex_t mtx_waiter_count;
120     volatile int waiter_count;
121     HANDLE semaphore;
122     HANDLE waiters_done;
123     volatile int is_broadcast;
124 } win32_cond_t;
125
126 static void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
127 {
128     win32_cond_t *win32_cond = NULL;
129     if (cond_init) {
130         cond_init(cond);
131         return;
132     }
133
134     /* non native condition variables */
135     win32_cond = av_mallocz(sizeof(win32_cond_t));
136     if (!win32_cond)
137         return;
138     cond->ptr = win32_cond;
139     win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
140     if (!win32_cond->semaphore)
141         return;
142     win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
143     if (!win32_cond->waiters_done)
144         return;
145
146     pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
147     pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
148 }
149
150 static void pthread_cond_destroy(pthread_cond_t *cond)
151 {
152     win32_cond_t *win32_cond = cond->ptr;
153     /* native condition variables do not destroy */
154     if (cond_init)
155         return;
156
157     /* non native condition variables */
158     CloseHandle(win32_cond->semaphore);
159     CloseHandle(win32_cond->waiters_done);
160     pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
161     pthread_mutex_destroy(&win32_cond->mtx_broadcast);
162     av_freep(&win32_cond);
163     cond->ptr = NULL;
164 }
165
166 static void pthread_cond_broadcast(pthread_cond_t *cond)
167 {
168     win32_cond_t *win32_cond = cond->ptr;
169     int have_waiter;
170
171     if (cond_broadcast) {
172         cond_broadcast(cond);
173         return;
174     }
175
176     /* non native condition variables */
177     pthread_mutex_lock(&win32_cond->mtx_broadcast);
178     pthread_mutex_lock(&win32_cond->mtx_waiter_count);
179     have_waiter = 0;
180
181     if (win32_cond->waiter_count) {
182         win32_cond->is_broadcast = 1;
183         have_waiter = 1;
184     }
185
186     if (have_waiter) {
187         ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
188         pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
189         WaitForSingleObject(win32_cond->waiters_done, INFINITE);
190         ResetEvent(win32_cond->waiters_done);
191         win32_cond->is_broadcast = 0;
192     } else
193         pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
194     pthread_mutex_unlock(&win32_cond->mtx_broadcast);
195 }
196
197 static int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
198 {
199     win32_cond_t *win32_cond = cond->ptr;
200     int last_waiter;
201     if (cond_wait) {
202         cond_wait(cond, mutex, INFINITE);
203         return 0;
204     }
205
206     /* non native condition variables */
207     pthread_mutex_lock(&win32_cond->mtx_broadcast);
208     pthread_mutex_lock(&win32_cond->mtx_waiter_count);
209     win32_cond->waiter_count++;
210     pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
211     pthread_mutex_unlock(&win32_cond->mtx_broadcast);
212
213     // unlock the external mutex
214     pthread_mutex_unlock(mutex);
215     WaitForSingleObject(win32_cond->semaphore, INFINITE);
216
217     pthread_mutex_lock(&win32_cond->mtx_waiter_count);
218     win32_cond->waiter_count--;
219     last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
220     pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
221
222     if (last_waiter)
223         SetEvent(win32_cond->waiters_done);
224
225     // lock the external mutex
226     return pthread_mutex_lock(mutex);
227 }
228
229 static void pthread_cond_signal(pthread_cond_t *cond)
230 {
231     win32_cond_t *win32_cond = cond->ptr;
232     int have_waiter;
233     if (cond_signal) {
234         cond_signal(cond);
235         return;
236     }
237
238     pthread_mutex_lock(&win32_cond->mtx_broadcast);
239
240     /* non-native condition variables */
241     pthread_mutex_lock(&win32_cond->mtx_waiter_count);
242     have_waiter = win32_cond->waiter_count;
243     pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
244
245     if (have_waiter) {
246         ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
247         WaitForSingleObject(win32_cond->waiters_done, INFINITE);
248         ResetEvent(win32_cond->waiters_done);
249     }
250
251     pthread_mutex_unlock(&win32_cond->mtx_broadcast);
252 }
253
254 static void w32thread_init(void)
255 {
256     HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
257     /* if one is available, then they should all be available */
258     cond_init      =
259         (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
260     cond_broadcast =
261         (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
262     cond_signal    =
263         (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
264     cond_wait      =
265         (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
266 }
267
268 #endif /* AVCODEC_W32PTHREADS_H */