w32pthreads: always use Vista+ API, drop XP support
[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
6baeadd1 42#include "libavutil/attributes.h"
11928d24
DB
43#include "libavutil/internal.h"
44#include "libavutil/mem.h"
45
e4cbf752 46typedef struct pthread_t {
27237d52
SW
47 void *handle;
48 void *(*func)(void* arg);
49 void *arg;
50 void *ret;
51} pthread_t;
52
c7ab6aff 53/* use lightweight mutex/condition variable API for Windows Vista and later */
54typedef SRWLOCK pthread_mutex_t;
73ea3ffc 55typedef CONDITION_VARIABLE pthread_cond_t;
27237d52 56
c7ab6aff 57#define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT
58#define PTHREAD_COND_INITIALIZER CONDITION_VARIABLE_INIT
59
428b0578
MS
60#define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
61#define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
27237d52 62
6baeadd1 63static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
27237d52
SW
64{
65 pthread_t *h = arg;
66 h->ret = h->func(h->arg);
67 return 0;
68}
69
6baeadd1
DB
70static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
71 void *(*start_routine)(void*), void *arg)
27237d52
SW
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
6baeadd1 80static av_unused void pthread_join(pthread_t thread, void **value_ptr)
27237d52
SW
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
81486312
MS
90static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
91{
c7ab6aff 92 InitializeSRWLock(m);
81486312
MS
93 return 0;
94}
95static inline int pthread_mutex_destroy(pthread_mutex_t *m)
96{
c7ab6aff 97 /* Unlocked SWR locks use no resources */
81486312
MS
98 return 0;
99}
100static inline int pthread_mutex_lock(pthread_mutex_t *m)
101{
c7ab6aff 102 AcquireSRWLockExclusive(m);
81486312
MS
103 return 0;
104}
105static inline int pthread_mutex_unlock(pthread_mutex_t *m)
106{
c7ab6aff 107 ReleaseSRWLockExclusive(m);
81486312
MS
108 return 0;
109}
27237d52 110
b22693b0
HL
111
112typedef INIT_ONCE pthread_once_t;
113#define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
114
115static 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
b7c3bfd5
JA
125static 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 */
131static inline void pthread_cond_destroy(pthread_cond_t *cond)
132{
133 return;
134}
135
136static inline void pthread_cond_broadcast(pthread_cond_t *cond)
137{
138 WakeAllConditionVariable(cond);
139}
140
141static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
142{
c7ab6aff 143 SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
b7c3bfd5
JA
144 return 0;
145}
146
147static inline void pthread_cond_signal(pthread_cond_t *cond)
148{
149 WakeConditionVariable(cond);
150}
151
439902e0 152#endif /* LIBAV_COMPAT_W32PTHREADS_H */