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 */
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#if HAVE_WINRT
67#define THREADFUNC_RETTYPE DWORD
68#else
69#define THREADFUNC_RETTYPE unsigned
70#endif
71
74{
76 h->ret = h->func(h->arg);
77 return 0;
78}
79
80av_unused static int pthread_create(pthread_t *thread, const void *unused_attr,
81 void *(*start_routine)(void*), void *arg)
82{
84
85 ret = (pthread_t)av_mallocz(sizeof(*ret));
86 if (!ret)
87 return EAGAIN;
88
89 ret->func = start_routine;
90 ret->arg = arg;
91#if HAVE_WINRT
92 ret->handle = (void*)CreateThread(NULL, 0, win32thread_worker, ret,
93 0, NULL);
94#else
95 ret->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, ret,
96 0, NULL);
97#endif
98
99 if (!ret->handle) {
100 av_free(ret);
101 return EAGAIN;
102 }
103
104 *thread = ret;
105
106 return 0;
107}
108
109av_unused static int pthread_join(pthread_t thread, void **value_ptr)
110{
111 DWORD ret = WaitForSingleObject(thread->handle, INFINITE);
112 if (ret != WAIT_OBJECT_0) {
113 if (ret == WAIT_ABANDONED)
114 return EINVAL;
115 else
116 return EDEADLK;
117 }
118 if (value_ptr)
119 *value_ptr = thread->ret;
120 CloseHandle(thread->handle);
121 av_free(thread);
122 return 0;
123}
124
125static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
126{
127 InitializeSRWLock(m);
128 return 0;
129}
131{
132 /* Unlocked SWR locks use no resources */
133 return 0;
134}
136{
137 AcquireSRWLockExclusive(m);
138 return 0;
139}
141{
142 ReleaseSRWLockExclusive(m);
143 return 0;
144}
145
147{
148 return TryAcquireSRWLockExclusive(m) == 0 ? EBUSY : 0;
149}
150
151typedef INIT_ONCE pthread_once_t;
152#define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
153
154av_unused static int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
155{
156 BOOL pending = FALSE;
157 InitOnceBeginInitialize(once_control, 0, &pending, NULL);
158 if (pending)
159 init_routine();
160 InitOnceComplete(once_control, 0, NULL);
161 return 0;
162}
163
164static inline int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
165{
166 InitializeConditionVariable(cond);
167 return 0;
168}
169
170/* native condition variables do not destroy */
172{
173 return 0;
174}
175
177{
178 WakeAllConditionVariable(cond);
179 return 0;
180}
181
183{
184 SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
185 return 0;
186}
187
189 const struct timespec *abstime)
190{
191 int64_t abs_milli = abstime->tv_sec * 1000LL + abstime->tv_nsec / 1000000;
192 DWORD t = av_clip64(abs_milli - av_gettime() / 1000, 0, UINT32_MAX);
193
194 if (!SleepConditionVariableSRW(cond, mutex, t, 0)) {
195 DWORD err = GetLastError();
196 if (err == ERROR_TIMEOUT)
197 return ETIMEDOUT;
198 else
199 return EINVAL;
200 }
201 return 0;
202}
203
205{
206 WakeConditionVariable(cond);
207 return 0;
208}
209
210static inline int win32_thread_setname(const char *name)
211{
212#if !HAVE_UWP
213 typedef HRESULT (WINAPI *SetThreadDescriptionFn)(HANDLE, PCWSTR);
214
215 // Although SetThreadDescription lives in kernel32.dll, on Windows Server 2016,
216 // Windows 10 LTSB 2016 and Windows 10 version 1607, it was only available in
217 // kernelbase.dll. So, load it from there for maximum coverage.
218 HMODULE kernelbase = GetModuleHandleW(L"kernelbase.dll");
219 if (!kernelbase)
220 return AVERROR(ENOSYS);
221
222 SetThreadDescriptionFn pSetThreadDescription =
223 (SetThreadDescriptionFn)GetProcAddress(kernelbase, "SetThreadDescription");
224 if (!pSetThreadDescription)
225 return AVERROR(ENOSYS);
226
227 wchar_t *wname;
228 if (utf8towchar(name, &wname) < 0)
229 return AVERROR(ENOMEM);
230
231 HRESULT hr = pSetThreadDescription(GetCurrentThread(), wname);
232 av_free(wname);
233 return SUCCEEDED(hr) ? 0 : AVERROR(EINVAL);
234#else
235 // UWP is not supported because we cannot use LoadLibrary/GetProcAddress to
236 // detect the availability of the SetThreadDescription API. There is a small
237 // gap in Windows builds 1507-1607 where it was not available. UWP allows
238 // querying the availability of APIs with QueryOptionalDelayLoadedAPI, but it
239 // requires /DELAYLOAD:kernel32.dll during linking, and we cannot enforce that.
240 return AVERROR(ENOSYS);
241#endif
242}
243
244#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
int ret
Definition ffplay.c:391
#define AVERROR(e)
Definition error.h:45
const char * arg
Definition jacosubdec.c:66
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:51
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 * 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_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:73
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:80
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:69
static int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)