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