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