build: Drop support for legacy TI ARM compiler
[ffmpeg.git] / 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 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 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 #if _WIN32_WINNT < 0x0600 && defined(__MINGW32__)
43 #undef MemoryBarrier
44 #define MemoryBarrier __sync_synchronize
45 #endif
46
47 #include "libavutil/attributes.h"
48 #include "libavutil/internal.h"
49 #include "libavutil/mem.h"
50
51 typedef struct pthread_t {
52     void *handle;
53     void *(*func)(void* arg);
54     void *arg;
55     void *ret;
56 } pthread_t;
57
58 /* the conditional variable api for windows 6.0+ uses critical sections and
59  * not mutexes */
60 typedef CRITICAL_SECTION pthread_mutex_t;
61
62 /* This is the CONDITION_VARIABLE typedef for using Windows' native
63  * conditional variables on kernels 6.0+. */
64 #if HAVE_CONDITION_VARIABLE_PTR
65 typedef CONDITION_VARIABLE pthread_cond_t;
66 #else
67 typedef struct pthread_cond_t {
68     void *Ptr;
69 } pthread_cond_t;
70 #endif
71
72 #if _WIN32_WINNT >= 0x0600
73 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
74 #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
75 #endif
76
77 static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
78 {
79     pthread_t *h = arg;
80     h->ret = h->func(h->arg);
81     return 0;
82 }
83
84 static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
85                                     void *(*start_routine)(void*), void *arg)
86 {
87     thread->func   = start_routine;
88     thread->arg    = arg;
89     thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
90                                            0, NULL);
91     return !thread->handle;
92 }
93
94 static av_unused void pthread_join(pthread_t thread, void **value_ptr)
95 {
96     DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
97     if (ret != WAIT_OBJECT_0)
98         return;
99     if (value_ptr)
100         *value_ptr = thread.ret;
101     CloseHandle(thread.handle);
102 }
103
104 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
105 {
106     InitializeCriticalSection(m);
107     return 0;
108 }
109 static inline int pthread_mutex_destroy(pthread_mutex_t *m)
110 {
111     DeleteCriticalSection(m);
112     return 0;
113 }
114 static inline int pthread_mutex_lock(pthread_mutex_t *m)
115 {
116     EnterCriticalSection(m);
117     return 0;
118 }
119 static inline int pthread_mutex_unlock(pthread_mutex_t *m)
120 {
121     LeaveCriticalSection(m);
122     return 0;
123 }
124
125 #if _WIN32_WINNT >= 0x0600
126
127 typedef INIT_ONCE pthread_once_t;
128 #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
129
130 static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
131 {
132     BOOL pending = FALSE;
133     InitOnceBeginInitialize(once_control, 0, &pending, NULL);
134     if (pending)
135         init_routine();
136     InitOnceComplete(once_control, 0, NULL);
137     return 0;
138 }
139
140 static inline void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
141 {
142     InitializeConditionVariable(cond);
143 }
144
145 /* native condition variables do not destroy */
146 static inline void pthread_cond_destroy(pthread_cond_t *cond)
147 {
148     return;
149 }
150
151 static inline void pthread_cond_broadcast(pthread_cond_t *cond)
152 {
153     WakeAllConditionVariable(cond);
154 }
155
156 static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
157 {
158     SleepConditionVariableCS(cond, mutex, INFINITE);
159     return 0;
160 }
161
162 static inline void pthread_cond_signal(pthread_cond_t *cond)
163 {
164     WakeConditionVariable(cond);
165 }
166
167 #else // _WIN32_WINNT < 0x0600
168
169 /* atomic init state of dynamically loaded functions */
170 static LONG w32thread_init_state = 0;
171 static av_unused void w32thread_init(void);
172
173 /* for pre-Windows 6.0 platforms, define INIT_ONCE struct,
174  * compatible to the one used in the native API */
175
176 typedef union pthread_once_t  {
177     void * Ptr;    ///< For the Windows 6.0+ native functions
178     LONG state;    ///< For the pre-Windows 6.0 compat code
179 } pthread_once_t;
180
181 #define PTHREAD_ONCE_INIT {0}
182
183 /* function pointers to init once API on windows 6.0+ kernels */
184 static BOOL (WINAPI *initonce_begin)(pthread_once_t *lpInitOnce, DWORD dwFlags, BOOL *fPending, void **lpContext);
185 static BOOL (WINAPI *initonce_complete)(pthread_once_t *lpInitOnce, DWORD dwFlags, void *lpContext);
186
187 /* pre-Windows 6.0 compat using a spin-lock */
188 static inline void w32thread_once_fallback(LONG volatile *state, void (*init_routine)(void))
189 {
190     switch (InterlockedCompareExchange(state, 1, 0)) {
191     /* Initial run */
192     case 0:
193         init_routine();
194         InterlockedExchange(state, 2);
195         break;
196     /* Another thread is running init */
197     case 1:
198         while (1) {
199             MemoryBarrier();
200             if (*state == 2)
201                 break;
202             Sleep(0);
203         }
204         break;
205     /* Initialization complete */
206     case 2:
207         break;
208     }
209 }
210
211 static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
212 {
213     w32thread_once_fallback(&w32thread_init_state, w32thread_init);
214
215     /* Use native functions on Windows 6.0+ */
216     if (initonce_begin && initonce_complete) {
217         BOOL pending = FALSE;
218         initonce_begin(once_control, 0, &pending, NULL);
219         if (pending)
220             init_routine();
221         initonce_complete(once_control, 0, NULL);
222         return 0;
223     }
224
225     w32thread_once_fallback(&once_control->state, init_routine);
226     return 0;
227 }
228
229 /* for pre-Windows 6.0 platforms we need to define and use our own condition
230  * variable and api */
231
232 typedef struct  win32_cond_t {
233     pthread_mutex_t mtx_broadcast;
234     pthread_mutex_t mtx_waiter_count;
235     volatile int waiter_count;
236     HANDLE semaphore;
237     HANDLE waiters_done;
238     volatile int is_broadcast;
239 } win32_cond_t;
240
241 /* function pointers to conditional variable API on windows 6.0+ kernels */
242 static void (WINAPI *cond_broadcast)(pthread_cond_t *cond);
243 static void (WINAPI *cond_init)(pthread_cond_t *cond);
244 static void (WINAPI *cond_signal)(pthread_cond_t *cond);
245 static BOOL (WINAPI *cond_wait)(pthread_cond_t *cond, pthread_mutex_t *mutex,
246                                 DWORD milliseconds);
247
248 static av_unused void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
249 {
250     win32_cond_t *win32_cond = NULL;
251
252     w32thread_once_fallback(&w32thread_init_state, w32thread_init);
253
254     if (cond_init) {
255         cond_init(cond);
256         return;
257     }
258
259     /* non native condition variables */
260     win32_cond = av_mallocz(sizeof(win32_cond_t));
261     if (!win32_cond)
262         return;
263     cond->Ptr = win32_cond;
264     win32_cond->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
265     if (!win32_cond->semaphore)
266         return;
267     win32_cond->waiters_done = CreateEvent(NULL, TRUE, FALSE, NULL);
268     if (!win32_cond->waiters_done)
269         return;
270
271     pthread_mutex_init(&win32_cond->mtx_waiter_count, NULL);
272     pthread_mutex_init(&win32_cond->mtx_broadcast, NULL);
273 }
274
275 static av_unused void pthread_cond_destroy(pthread_cond_t *cond)
276 {
277     win32_cond_t *win32_cond = cond->Ptr;
278     /* native condition variables do not destroy */
279     if (cond_init)
280         return;
281
282     /* non native condition variables */
283     CloseHandle(win32_cond->semaphore);
284     CloseHandle(win32_cond->waiters_done);
285     pthread_mutex_destroy(&win32_cond->mtx_waiter_count);
286     pthread_mutex_destroy(&win32_cond->mtx_broadcast);
287     av_freep(&win32_cond);
288     cond->Ptr = NULL;
289 }
290
291 static av_unused void pthread_cond_broadcast(pthread_cond_t *cond)
292 {
293     win32_cond_t *win32_cond = cond->Ptr;
294     int have_waiter;
295
296     if (cond_broadcast) {
297         cond_broadcast(cond);
298         return;
299     }
300
301     /* non native condition variables */
302     pthread_mutex_lock(&win32_cond->mtx_broadcast);
303     pthread_mutex_lock(&win32_cond->mtx_waiter_count);
304     have_waiter = 0;
305
306     if (win32_cond->waiter_count) {
307         win32_cond->is_broadcast = 1;
308         have_waiter = 1;
309     }
310
311     if (have_waiter) {
312         ReleaseSemaphore(win32_cond->semaphore, win32_cond->waiter_count, NULL);
313         pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
314         WaitForSingleObject(win32_cond->waiters_done, INFINITE);
315         ResetEvent(win32_cond->waiters_done);
316         win32_cond->is_broadcast = 0;
317     } else
318         pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
319     pthread_mutex_unlock(&win32_cond->mtx_broadcast);
320 }
321
322 static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
323 {
324     win32_cond_t *win32_cond = cond->Ptr;
325     int last_waiter;
326     if (cond_wait) {
327         cond_wait(cond, mutex, INFINITE);
328         return 0;
329     }
330
331     /* non native condition variables */
332     pthread_mutex_lock(&win32_cond->mtx_broadcast);
333     pthread_mutex_lock(&win32_cond->mtx_waiter_count);
334     win32_cond->waiter_count++;
335     pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
336     pthread_mutex_unlock(&win32_cond->mtx_broadcast);
337
338     // unlock the external mutex
339     pthread_mutex_unlock(mutex);
340     WaitForSingleObject(win32_cond->semaphore, INFINITE);
341
342     pthread_mutex_lock(&win32_cond->mtx_waiter_count);
343     win32_cond->waiter_count--;
344     last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
345     pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
346
347     if (last_waiter)
348         SetEvent(win32_cond->waiters_done);
349
350     // lock the external mutex
351     return pthread_mutex_lock(mutex);
352 }
353
354 static av_unused void pthread_cond_signal(pthread_cond_t *cond)
355 {
356     win32_cond_t *win32_cond = cond->Ptr;
357     int have_waiter;
358     if (cond_signal) {
359         cond_signal(cond);
360         return;
361     }
362
363     pthread_mutex_lock(&win32_cond->mtx_broadcast);
364
365     /* non-native condition variables */
366     pthread_mutex_lock(&win32_cond->mtx_waiter_count);
367     have_waiter = win32_cond->waiter_count;
368     pthread_mutex_unlock(&win32_cond->mtx_waiter_count);
369
370     if (have_waiter) {
371         ReleaseSemaphore(win32_cond->semaphore, 1, NULL);
372         WaitForSingleObject(win32_cond->waiters_done, INFINITE);
373         ResetEvent(win32_cond->waiters_done);
374     }
375
376     pthread_mutex_unlock(&win32_cond->mtx_broadcast);
377 }
378 #endif
379
380 static av_unused void w32thread_init(void)
381 {
382 #if _WIN32_WINNT < 0x0600
383     HANDLE kernel_dll = GetModuleHandle(TEXT("kernel32.dll"));
384     /* if one is available, then they should all be available */
385     cond_init      = (void (WINAPI*)(pthread_cond_t *))
386         GetProcAddress(kernel_dll, "InitializeConditionVariable");
387     cond_broadcast = (void (WINAPI*)(pthread_cond_t *))
388         GetProcAddress(kernel_dll, "WakeAllConditionVariable");
389     cond_signal    = (void (WINAPI*)(pthread_cond_t *))
390         GetProcAddress(kernel_dll, "WakeConditionVariable");
391     cond_wait      = (BOOL (WINAPI*)(pthread_cond_t *, pthread_mutex_t *, DWORD))
392         GetProcAddress(kernel_dll, "SleepConditionVariableCS");
393     initonce_begin = (BOOL (WINAPI*)(pthread_once_t *, DWORD, BOOL *, void **))
394         GetProcAddress(kernel_dll, "InitOnceBeginInitialize");
395     initonce_complete = (BOOL (WINAPI*)(pthread_once_t *, DWORD, void *))
396         GetProcAddress(kernel_dll, "InitOnceComplete");
397 #endif
398
399 }
400
401 #endif /* LIBAV_COMPAT_W32PTHREADS_H */