Libav
w32pthreads.h
Go to the documentation of this file.
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 
29 #ifndef LIBAV_COMPAT_W32PTHREADS_H
30 #define LIBAV_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/attributes.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;
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 
77 #define CreateEvent(a, reset, init, name) \
78  CreateEventEx(a, name, \
79  (reset ? CREATE_EVENT_MANUAL_RESET : 0) | \
80  (init ? CREATE_EVENT_INITIAL_SET : 0), \
81  EVENT_ALL_ACCESS)
82 // CreateSemaphoreExA seems to be desktop-only, but as long as we don't
83 // use named semaphores, it doesn't matter if we use the W version.
84 #define CreateSemaphore(a, b, c, d) \
85  CreateSemaphoreExW(a, b, c, d, 0, SEMAPHORE_ALL_ACCESS)
86 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
87 #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
88 #endif
89 
90 static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
91 {
92  pthread_t *h = arg;
93  h->ret = h->func(h->arg);
94  return 0;
95 }
96 
97 static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
98  void *(*start_routine)(void*), void *arg)
99 {
100  thread->func = start_routine;
101  thread->arg = arg;
102  thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
103  0, NULL);
104  return !thread->handle;
105 }
106 
107 static av_unused void pthread_join(pthread_t thread, void **value_ptr)
108 {
109  DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
110  if (ret != WAIT_OBJECT_0)
111  return;
112  if (value_ptr)
113  *value_ptr = thread.ret;
114  CloseHandle(thread.handle);
115 }
116 
117 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
118 {
119  InitializeCriticalSection(m);
120  return 0;
121 }
123 {
124  DeleteCriticalSection(m);
125  return 0;
126 }
127 static inline int pthread_mutex_lock(pthread_mutex_t *m)
128 {
129  EnterCriticalSection(m);
130  return 0;
131 }
133 {
134  LeaveCriticalSection(m);
135  return 0;
136 }
137 
138 /* for pre-Windows 6.0 platforms we need to define and use our own condition
139  * variable and api */
140 typedef struct win32_cond_t {
143  volatile int waiter_count;
144  HANDLE semaphore;
145  HANDLE waiters_done;
146  volatile int is_broadcast;
147 } win32_cond_t;
148 
149 static av_unused void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
150 {
151  win32_cond_t *win32_cond = NULL;
152  if (cond_init) {
153  cond_init(cond);
154  return;
155  }
156 
157  /* non native condition variables */
158  win32_cond = av_mallocz(sizeof(win32_cond_t));
159  if (!win32_cond)
160  return;
161  cond->ptr = win32_cond;
162  win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
163  if (!win32_cond->semaphore)
164  return;
165  win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
166  if (!win32_cond->waiters_done)
167  return;
168 
170  pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
171 }
172 
174 {
175  win32_cond_t *win32_cond = cond->ptr;
176  /* native condition variables do not destroy */
177  if (cond_init)
178  return;
179 
180  /* non native condition variables */
181  CloseHandle(win32_cond->semaphore);
182  CloseHandle(win32_cond->waiters_done);
184  pthread_mutex_destroy(&win32_cond->mtx_broadcast);
185  av_freep(&win32_cond);
186  cond->ptr = NULL;
187 }
188 
190 {
191  win32_cond_t *win32_cond = cond->ptr;
192  int have_waiter;
193 
194  if (cond_broadcast) {
195  cond_broadcast(cond);
196  return;
197  }
198 
199  /* non native condition variables */
200  pthread_mutex_lock(&win32_cond->mtx_broadcast);
201  pthread_mutex_lock(&win32_cond->mtx_waiter_count);
202  have_waiter = 0;
203 
204  if (win32_cond->waiter_count) {
205  win32_cond->is_broadcast = 1;
206  have_waiter = 1;
207  }
208 
209  if (have_waiter) {
210  ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
212  WaitForSingleObject(win32_cond->waiters_done, INFINITE);
213  ResetEvent(win32_cond->waiters_done);
214  win32_cond->is_broadcast = 0;
215  } else
217  pthread_mutex_unlock(&win32_cond->mtx_broadcast);
218 }
219 
221 {
222  win32_cond_t *win32_cond = cond->ptr;
223  int last_waiter;
224  if (cond_wait) {
225  cond_wait(cond, mutex, INFINITE);
226  return 0;
227  }
228 
229  /* non native condition variables */
230  pthread_mutex_lock(&win32_cond->mtx_broadcast);
231  pthread_mutex_lock(&win32_cond->mtx_waiter_count);
232  win32_cond->waiter_count++;
234  pthread_mutex_unlock(&win32_cond->mtx_broadcast);
235 
236  // unlock the external mutex
237  pthread_mutex_unlock(mutex);
238  WaitForSingleObject(win32_cond->semaphore, INFINITE);
239 
240  pthread_mutex_lock(&win32_cond->mtx_waiter_count);
241  win32_cond->waiter_count--;
242  last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
244 
245  if (last_waiter)
246  SetEvent(win32_cond->waiters_done);
247 
248  // lock the external mutex
249  return pthread_mutex_lock(mutex);
250 }
251 
253 {
254  win32_cond_t *win32_cond = cond->ptr;
255  int have_waiter;
256  if (cond_signal) {
257  cond_signal(cond);
258  return;
259  }
260 
261  pthread_mutex_lock(&win32_cond->mtx_broadcast);
262 
263  /* non-native condition variables */
264  pthread_mutex_lock(&win32_cond->mtx_waiter_count);
265  have_waiter = win32_cond->waiter_count;
267 
268  if (have_waiter) {
269  ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
270  WaitForSingleObject(win32_cond->waiters_done, INFINITE);
271  ResetEvent(win32_cond->waiters_done);
272  }
273 
274  pthread_mutex_unlock(&win32_cond->mtx_broadcast);
275 }
276 
277 static av_unused void w32thread_init(void)
278 {
279 #if _WIN32_WINNT < 0x0600
280  HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
281  /* if one is available, then they should all be available */
282  cond_init =
283  (void*)GetProcAddress(kernel_dll, "InitializeConditionVariable");
284  cond_broadcast =
285  (void*)GetProcAddress(kernel_dll, "WakeAllConditionVariable");
286  cond_signal =
287  (void*)GetProcAddress(kernel_dll, "WakeConditionVariable");
288  cond_wait =
289  (void*)GetProcAddress(kernel_dll, "SleepConditionVariableCS");
290 #endif
291 
292 }
293 
294 #endif /* LIBAV_COMPAT_W32PTHREADS_H */
static av_unused void pthread_cond_signal(pthread_cond_t *cond)
Definition: w32pthreads.h:252
static av_unused void w32thread_init(void)
Definition: w32pthreads.h:277
volatile int is_broadcast
Definition: w32pthreads.h:146
static av_unused void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
Definition: w32pthreads.h:149
static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
Definition: w32pthreads.h:90
static av_unused void pthread_cond_broadcast(pthread_cond_t *cond)
Definition: w32pthreads.h:189
void * arg
Definition: w32pthreads.h:49
volatile int waiter_count
Definition: w32pthreads.h:143
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static pthread_mutex_t DWORD milliseconds
Definition: w32pthreads.h:69
CRITICAL_SECTION pthread_mutex_t
Definition: w32pthreads.h:55
static av_unused int pthread_create(pthread_t *thread, const void *unused_attr, void *(*start_routine)(void *), void *arg)
Definition: w32pthreads.h:97
void * ret
Definition: w32pthreads.h:50
HANDLE waiters_done
Definition: w32pthreads.h:145
static int pthread_mutex_init(pthread_mutex_t *m, void *attr)
Definition: w32pthreads.h:117
static int pthread_mutex_unlock(pthread_mutex_t *m)
Definition: w32pthreads.h:132
static av_unused void pthread_join(pthread_t thread, void **value_ptr)
Definition: w32pthreads.h:107
common internal API header
static int pthread_mutex_destroy(pthread_mutex_t *m)
Definition: w32pthreads.h:122
static av_unused void pthread_cond_destroy(pthread_cond_t *cond)
Definition: w32pthreads.h:173
static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: w32pthreads.h:220
void *(* func)(void *arg)
Definition: w32pthreads.h:48
HANDLE semaphore
Definition: w32pthreads.h:144
static pthread_mutex_t * mutex
Definition: w32pthreads.h:69
pthread_mutex_t mtx_broadcast
Definition: w32pthreads.h:141
#define attribute_align_arg
Definition: internal.h:54
NULL
Definition: eval.c:55
static int pthread_mutex_lock(pthread_mutex_t *m)
Definition: w32pthreads.h:127
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
#define av_unused
Definition: attributes.h:86
#define GetProcAddress
Definition: avisynth.c:56
void * handle
Definition: w32pthreads.h:47
pthread_mutex_t mtx_waiter_count
Definition: w32pthreads.h:142
static BOOL(WINAPI *cond_wait)(pthread_cond_t *cond
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205