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