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