FFmpeg
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 
42 #include "libavutil/attributes.h"
43 #include "libavutil/common.h"
44 #include "libavutil/internal.h"
45 #include "libavutil/mem.h"
46 #include "libavutil/time.h"
47 
48 typedef struct pthread_t {
49  void *handle;
50  void *(*func)(void* arg);
51  void *arg;
52  void *ret;
53 } pthread_t;
54 
55 /* use light weight mutex/condition variable API for Windows Vista and later */
56 typedef SRWLOCK pthread_mutex_t;
57 typedef CONDITION_VARIABLE pthread_cond_t;
58 
59 #define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT
60 #define PTHREAD_COND_INITIALIZER CONDITION_VARIABLE_INIT
61 
62 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
63 #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
64 
65 #define PTHREAD_CANCEL_ENABLE 1
66 #define PTHREAD_CANCEL_DISABLE 0
67 
68 #if HAVE_WINRT
69 #define THREADFUNC_RETTYPE DWORD
70 #else
71 #define THREADFUNC_RETTYPE unsigned
72 #endif
73 
76 {
77  pthread_t *h = (pthread_t*)arg;
78  h->ret = h->func(h->arg);
79  return 0;
80 }
81 
82 static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
83  void *(*start_routine)(void*), void *arg)
84 {
85  thread->func = start_routine;
86  thread->arg = arg;
87 #if HAVE_WINRT
88  thread->handle = (void*)CreateThread(NULL, 0, win32thread_worker, thread,
89  0, NULL);
90 #else
91  thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
92  0, NULL);
93 #endif
94  return !thread->handle;
95 }
96 
97 static av_unused int pthread_join(pthread_t thread, void **value_ptr)
98 {
99  DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
100  if (ret != WAIT_OBJECT_0) {
101  if (ret == WAIT_ABANDONED)
102  return EINVAL;
103  else
104  return EDEADLK;
105  }
106  if (value_ptr)
107  *value_ptr = thread.ret;
108  CloseHandle(thread.handle);
109  return 0;
110 }
111 
112 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
113 {
114  InitializeSRWLock(m);
115  return 0;
116 }
118 {
119  /* Unlocked SWR locks use no resources */
120  return 0;
121 }
122 static inline int pthread_mutex_lock(pthread_mutex_t *m)
123 {
124  AcquireSRWLockExclusive(m);
125  return 0;
126 }
128 {
129  ReleaseSRWLockExclusive(m);
130  return 0;
131 }
132 
134 #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
135 
136 static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
137 {
138  BOOL pending = FALSE;
139  InitOnceBeginInitialize(once_control, 0, &pending, NULL);
140  if (pending)
141  init_routine();
142  InitOnceComplete(once_control, 0, NULL);
143  return 0;
144 }
145 
146 static inline int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
147 {
148  InitializeConditionVariable(cond);
149  return 0;
150 }
151 
152 /* native condition variables do not destroy */
154 {
155  return 0;
156 }
157 
159 {
160  WakeAllConditionVariable(cond);
161  return 0;
162 }
163 
165 {
166  SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
167  return 0;
168 }
169 
171  const struct timespec *abstime)
172 {
173  int64_t abs_milli = abstime->tv_sec * 1000LL + abstime->tv_nsec / 1000000;
174  DWORD t = av_clip64(abs_milli - av_gettime() / 1000, 0, UINT32_MAX);
175 
176  if (!SleepConditionVariableSRW(cond, mutex, t, 0)) {
177  DWORD err = GetLastError();
178  if (err == ERROR_TIMEOUT)
179  return ETIMEDOUT;
180  else
181  return EINVAL;
182  }
183  return 0;
184 }
185 
187 {
188  WakeConditionVariable(cond);
189  return 0;
190 }
191 
192 static inline int pthread_setcancelstate(int state, int *oldstate)
193 {
194  return 0;
195 }
196 
197 #endif /* COMPAT_W32PTHREADS_H */
pthread_mutex_t
_fmutex pthread_mutex_t
Definition: os2threads.h:53
pthread_mutex_lock
static int pthread_mutex_lock(pthread_mutex_t *m)
Definition: w32pthreads.h:122
int64_t
long long int64_t
Definition: coverity.c:34
av_unused
#define av_unused
Definition: attributes.h:131
win32thread_worker
static av_unused THREADFUNC_RETTYPE __stdcall attribute_align_arg win32thread_worker(void *arg)
Definition: w32pthreads.h:75
pthread_cond_timedwait
static int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
Definition: w32pthreads.h:170
pthread_cond_init
static int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
Definition: w32pthreads.h:146
pthread_cond_broadcast
static int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: w32pthreads.h:158
state
static struct @464 state
pthread_cond_wait
static int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: w32pthreads.h:164
av_clip64
#define av_clip64
Definition: common.h:103
pthread_cond_destroy
static int pthread_cond_destroy(pthread_cond_t *cond)
Definition: w32pthreads.h:153
pthread_t::arg
void * arg
Definition: os2threads.h:47
pthread_once
static av_unused int pthread_once(pthread_once_t *once_control, void(*init_routine)(void))
Definition: w32pthreads.h:136
THREADFUNC_RETTYPE
#define THREADFUNC_RETTYPE
Definition: w32pthreads.h:71
pthread_mutex_t
SRWLOCK pthread_mutex_t
Definition: w32pthreads.h:56
pthread_mutex_destroy
static int pthread_mutex_destroy(pthread_mutex_t *m)
Definition: w32pthreads.h:117
arg
const char * arg
Definition: jacosubdec.c:67
NULL
#define NULL
Definition: coverity.c:32
pthread_t::ret
void * ret
Definition: w32pthreads.h:52
time.h
pthread_cond_t
CONDITION_VARIABLE pthread_cond_t
Definition: w32pthreads.h:57
attribute_align_arg
#define attribute_align_arg
Definition: internal.h:50
pthread_t::func
void *(* func)(void *arg)
Definition: w32pthreads.h:50
pthread_create
static av_unused int pthread_create(pthread_t *thread, const void *unused_attr, void *(*start_routine)(void *), void *arg)
Definition: w32pthreads.h:82
attributes.h
pthread_t
Definition: os2threads.h:44
pthread_cond_signal
static int pthread_cond_signal(pthread_cond_t *cond)
Definition: w32pthreads.h:186
pthread_t::handle
void * handle
Definition: w32pthreads.h:49
internal.h
common.h
pthread_cond_t
Definition: os2threads.h:58
pthread_join
static av_unused int pthread_join(pthread_t thread, void **value_ptr)
Definition: w32pthreads.h:97
pthread_setcancelstate
static int pthread_setcancelstate(int state, int *oldstate)
Definition: w32pthreads.h:192
ret
ret
Definition: filter_design.txt:187
pthread_mutex_init
static int pthread_mutex_init(pthread_mutex_t *m, void *attr)
Definition: w32pthreads.h:112
pthread_once_t
INIT_ONCE pthread_once_t
Definition: w32pthreads.h:133
INIT_ONCE
#define INIT_ONCE(id, name)
pthread_once_t
Definition: os2threads.h:66
av_gettime
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
mem.h
WaitForSingleObject
#define WaitForSingleObject(a, b)
Definition: w32pthreads.h:63
h
h
Definition: vp9dsp_template.c:2070
pthread_mutex_unlock
static int pthread_mutex_unlock(pthread_mutex_t *m)
Definition: w32pthreads.h:127
cond
int(* cond)(enum AVPixelFormat pix_fmt)
Definition: pixdesc_query.c:28
mutex
static AVMutex mutex
Definition: log.c:46