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