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