2 * Copyright (C) 2010-2011 x264 project
4 * Authors: Steven Walters <kemuri9@gmail.com>
5 * Pegasys Inc. <http://www.pegasys-inc.com>
7 * This file is part of FFmpeg.
9 * FFmpeg 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.
14 * FFmpeg 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.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * w32threads to pthreads wrapper
29 #ifndef COMPAT_W32PTHREADS_H
30 #define COMPAT_W32PTHREADS_H
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
35 * As most functions here are used without checking return values,
36 * only implement return values as necessary. */
38 #define WIN32_LEAN_AND_MEAN
42 #include "libavutil/attributes.h"
43 #include "libavutil/common.h"
44 #include "libavutil/internal.h"
45 #include "libavutil/mem.h"
47 typedef struct pthread_t {
49 void *(*func)(void* arg);
54 /* use light weight mutex/condition variable API for Windows Vista and later */
55 typedef SRWLOCK pthread_mutex_t;
56 typedef CONDITION_VARIABLE pthread_cond_t;
58 #define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT
59 #define PTHREAD_COND_INITIALIZER CONDITION_VARIABLE_INIT
61 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
62 #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
64 static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
66 pthread_t *h = (pthread_t*)arg;
67 h->ret = h->func(h->arg);
71 static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
72 void *(*start_routine)(void*), void *arg)
74 thread->func = start_routine;
77 thread->handle = (void*)CreateThread(NULL, 0, win32thread_worker, thread,
80 thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
83 return !thread->handle;
86 static av_unused int pthread_join(pthread_t thread, void **value_ptr)
88 DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
89 if (ret != WAIT_OBJECT_0) {
90 if (ret == WAIT_ABANDONED)
96 *value_ptr = thread.ret;
97 CloseHandle(thread.handle);
101 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
103 InitializeSRWLock(m);
106 static inline int pthread_mutex_destroy(pthread_mutex_t *m)
108 /* Unlocked SWR locks use no resources */
111 static inline int pthread_mutex_lock(pthread_mutex_t *m)
113 AcquireSRWLockExclusive(m);
116 static inline int pthread_mutex_unlock(pthread_mutex_t *m)
118 ReleaseSRWLockExclusive(m);
122 typedef INIT_ONCE pthread_once_t;
123 #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
125 static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
127 BOOL pending = FALSE;
128 InitOnceBeginInitialize(once_control, 0, &pending, NULL);
131 InitOnceComplete(once_control, 0, NULL);
135 static inline int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
137 InitializeConditionVariable(cond);
141 /* native condition variables do not destroy */
142 static inline int pthread_cond_destroy(pthread_cond_t *cond)
147 static inline int pthread_cond_broadcast(pthread_cond_t *cond)
149 WakeAllConditionVariable(cond);
153 static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
155 SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
159 static inline int pthread_cond_signal(pthread_cond_t *cond)
161 WakeConditionVariable(cond);
165 #endif /* COMPAT_W32PTHREADS_H */