FFmpeg
Loading...
Searching...
No Matches
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 FFmpeg.
8 *
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.
13 *
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.
18 *
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
22 */
23
24/**
25 * @file
26 * w32threads to pthreads wrapper
27 */
28
29#ifndef COMPAT_W32PTHREADS_H
30#define 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#include <windows.h>
39#include <process.h>
40#include <time.h>
41
43#include "libavutil/common.h"
44#include "libavutil/internal.h"
45#include "libavutil/mem.h"
46#include "libavutil/time.h"
48
49typedef struct w32pthread_t {
50 void *handle;
51 void *(*func)(void* arg);
52 void *arg;
53 void *ret;
54} *pthread_t;
55
56/* use light weight mutex/condition variable API for Windows Vista and later */
57typedef SRWLOCK pthread_mutex_t;
58typedef CONDITION_VARIABLE pthread_cond_t;
59
60#define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT
61#define PTHREAD_COND_INITIALIZER CONDITION_VARIABLE_INIT
62
63#define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
64#define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
65
66#define PTHREAD_CANCEL_ENABLE 1
67#define PTHREAD_CANCEL_DISABLE 0
68
69#if HAVE_WINRT
70#define THREADFUNC_RETTYPE DWORD
71#else
72#define THREADFUNC_RETTYPE unsigned
73#endif
74
77{
79 h->ret = h->func(h->arg);
80 return 0;
81}
82
83av_unused static int pthread_create(pthread_t *thread, const void *unused_attr,
84 void *(*start_routine)(void*), void *arg)
85{
86 pthread_t ret;
87
88 ret = (pthread_t)av_mallocz(sizeof(*ret));
89 if (!ret)
90 return EAGAIN;
91
92 ret->func = start_routine;
93 ret->arg = arg;
94#if HAVE_WINRT
95 ret->handle = (void*)CreateThread(NULL, 0, win32thread_worker, ret,
96 0, NULL);
97#else
98 ret->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, ret,
99 0, NULL);
100#endif
101
102 if (!ret->handle) {
103 av_free(ret);
104 return EAGAIN;
105 }
106
107 *thread = ret;
108
109 return 0;
110}
111
112av_unused static int pthread_join(pthread_t thread, void **value_ptr)
113{
114 DWORD ret = WaitForSingleObject(thread->handle, INFINITE);
115 if (ret != WAIT_OBJECT_0) {
116 if (ret == WAIT_ABANDONED)
117 return EINVAL;
118 else
119 return EDEADLK;
120 }
121 if (value_ptr)
122 *value_ptr = thread->ret;
123 CloseHandle(thread->handle);
124 av_free(thread);
125 return 0;
126}
127
128static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
129{
130 InitializeSRWLock(m);
131 return 0;
132}
134{
135 /* Unlocked SWR locks use no resources */
136 return 0;
137}
139{
140 AcquireSRWLockExclusive(m);
141 return 0;
142}
144{
145 ReleaseSRWLockExclusive(m);
146 return 0;
147}
148
150{
151 return TryAcquireSRWLockExclusive(m) == 0 ? EBUSY : 0;
152}
153
154typedef INIT_ONCE pthread_once_t;
155#define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
156
157av_unused static int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
158{
159 BOOL pending = FALSE;
160 InitOnceBeginInitialize(once_control, 0, &pending, NULL);
161 if (pending)
162 init_routine();
163 InitOnceComplete(once_control, 0, NULL);
164 return 0;
165}
166
167static inline int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
168{
169 InitializeConditionVariable(cond);
170 return 0;
171}
172
173/* native condition variables do not destroy */
175{
176 return 0;
177}
178
180{
181 WakeAllConditionVariable(cond);
182 return 0;
183}
184
186{
187 SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
188 return 0;
189}
190
192 const struct timespec *abstime)
193{
194 int64_t abs_milli = abstime->tv_sec * 1000LL + abstime->tv_nsec / 1000000;
195 DWORD t = av_clip64(abs_milli - av_gettime() / 1000, 0, UINT32_MAX);
196
197 if (!SleepConditionVariableSRW(cond, mutex, t, 0)) {
198 DWORD err = GetLastError();
199 if (err == ERROR_TIMEOUT)
200 return ETIMEDOUT;
201 else
202 return EINVAL;
203 }
204 return 0;
205}
206
208{
209 WakeConditionVariable(cond);
210 return 0;
211}
212
213static inline int pthread_setcancelstate(int state, int *oldstate)
214{
215 return 0;
216}
217
218static inline int win32_thread_setname(const char *name)
219{
220#if !HAVE_UWP
221 typedef HRESULT (WINAPI *SetThreadDescriptionFn)(HANDLE, PCWSTR);
222
223 // Although SetThreadDescription lives in kernel32.dll, on Windows Server 2016,
224 // Windows 10 LTSB 2016 and Windows 10 version 1607, it was only available in
225 // kernelbase.dll. So, load it from there for maximum coverage.
226 HMODULE kernelbase = GetModuleHandleW(L"kernelbase.dll");
227 if (!kernelbase)
228 return AVERROR(ENOSYS);
229
230 SetThreadDescriptionFn pSetThreadDescription =
231 (SetThreadDescriptionFn)GetProcAddress(kernelbase, "SetThreadDescription");
232 if (!pSetThreadDescription)
233 return AVERROR(ENOSYS);
234
235 wchar_t *wname;
236 if (utf8towchar(name, &wname) < 0)
237 return AVERROR(ENOMEM);
238
239 HRESULT hr = pSetThreadDescription(GetCurrentThread(), wname);
240 av_free(wname);
241 return SUCCEEDED(hr) ? 0 : AVERROR(EINVAL);
242#else
243 // UWP is not supported because we cannot use LoadLibrary/GetProcAddress to
244 // detect the availability of the SetThreadDescription API. There is a small
245 // gap in Windows builds 1507-1607 where it was not available. UWP allows
246 // querying the availability of APIs with QueryOptionalDelayLoadedAPI, but it
247 // requires /DELAYLOAD:kernel32.dll during linking, and we cannot enforce that.
248 return AVERROR(ENOSYS);
249#endif
250}
251
252#endif /* COMPAT_W32PTHREADS_H */
#define L(x)
Definition vpx_arith.h:36
common internal and external API header
#define av_clip64
Definition common.h:103
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static struct @346255127015250356166251341105367306144006377143 state
#define AVERROR(e)
Definition error.h:45
const char * arg
Definition jacosubdec.c:65
Macro definitions for various function/variable attributes.
#define av_unused
Definition attributes.h:164
common internal API header
#define attribute_align_arg
Definition internal.h:50
Memory handling functions.
_fmutex pthread_mutex_t
Definition os2threads.h:53
int(* cond)(enum AVPixelFormat pix_fmt)
const char * name
Definition qsvenc.c:142
static AVMutex mutex
Definition resman.c:61
void * arg
Definition os2threads.h:47
void * handle
Definition w32pthreads.h:50
#define av_free(p)
#define av_mallocz(s)
int64_t av_gettime(void)
Get the current time in microseconds.
Definition time.c:40
static int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
static int pthread_mutex_unlock(pthread_mutex_t *m)
static int win32_thread_setname(const char *name)
static int pthread_setcancelstate(int state, int *oldstate)
static int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
static int pthread_mutex_trylock(pthread_mutex_t *m)
static av_unused THREADFUNC_RETTYPE __stdcall attribute_align_arg win32thread_worker(void *arg)
Definition w32pthreads.h:76
static int pthread_cond_destroy(pthread_cond_t *cond)
static av_unused int pthread_create(pthread_t *thread, const void *unused_attr, void *(*start_routine)(void *), void *arg)
Definition w32pthreads.h:83
static int pthread_mutex_destroy(pthread_mutex_t *m)
static av_unused int pthread_join(pthread_t thread, void **value_ptr)
static av_unused int pthread_once(pthread_once_t *once_control, void(*init_routine)(void))
static int pthread_cond_signal(pthread_cond_t *cond)
static int pthread_mutex_lock(pthread_mutex_t *m)
#define WaitForSingleObject(a, b)
Definition w32pthreads.h:64
static int pthread_mutex_init(pthread_mutex_t *m, void *attr)
static int pthread_cond_broadcast(pthread_cond_t *cond)
#define THREADFUNC_RETTYPE
Definition w32pthreads.h:72
static int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)