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