w32pthreads: always use Vista+ API, drop XP support
[libav.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 #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 /* use lightweight mutex/condition variable API for Windows Vista and later */
54 typedef SRWLOCK pthread_mutex_t;
55 typedef CONDITION_VARIABLE pthread_cond_t;
56
57 #define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT
58 #define PTHREAD_COND_INITIALIZER CONDITION_VARIABLE_INIT
59
60 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
61 #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
62
63 static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
64 {
65 pthread_t *h = arg;
66 h->ret = h->func(h->arg);
67 return 0;
68 }
69
70 static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
71 void *(*start_routine)(void*), void *arg)
72 {
73 thread->func = start_routine;
74 thread->arg = arg;
75 thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
76 0, NULL);
77 return !thread->handle;
78 }
79
80 static av_unused void pthread_join(pthread_t thread, void **value_ptr)
81 {
82 DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
83 if (ret != WAIT_OBJECT_0)
84 return;
85 if (value_ptr)
86 *value_ptr = thread.ret;
87 CloseHandle(thread.handle);
88 }
89
90 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
91 {
92 InitializeSRWLock(m);
93 return 0;
94 }
95 static inline int pthread_mutex_destroy(pthread_mutex_t *m)
96 {
97 /* Unlocked SWR locks use no resources */
98 return 0;
99 }
100 static inline int pthread_mutex_lock(pthread_mutex_t *m)
101 {
102 AcquireSRWLockExclusive(m);
103 return 0;
104 }
105 static inline int pthread_mutex_unlock(pthread_mutex_t *m)
106 {
107 ReleaseSRWLockExclusive(m);
108 return 0;
109 }
110
111
112 typedef INIT_ONCE pthread_once_t;
113 #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
114
115 static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
116 {
117 BOOL pending = FALSE;
118 InitOnceBeginInitialize(once_control, 0, &pending, NULL);
119 if (pending)
120 init_routine();
121 InitOnceComplete(once_control, 0, NULL);
122 return 0;
123 }
124
125 static inline void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
126 {
127 InitializeConditionVariable(cond);
128 }
129
130 /* native condition variables do not destroy */
131 static inline void pthread_cond_destroy(pthread_cond_t *cond)
132 {
133 return;
134 }
135
136 static inline void pthread_cond_broadcast(pthread_cond_t *cond)
137 {
138 WakeAllConditionVariable(cond);
139 }
140
141 static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
142 {
143 SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
144 return 0;
145 }
146
147 static inline void pthread_cond_signal(pthread_cond_t *cond)
148 {
149 WakeConditionVariable(cond);
150 }
151
152 #endif /* LIBAV_COMPAT_W32PTHREADS_H */