FFmpeg
thread.h
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 // This header should only be used to simplify code where
20 // threading is optional, not as a generic threading abstraction.
21 
22 #ifndef AVUTIL_THREAD_H
23 #define AVUTIL_THREAD_H
24 
25 #include "config.h"
26 
27 #if HAVE_PRCTL
28 #include <sys/prctl.h>
29 #elif (HAVE_PTHREAD_SETNAME_NP || HAVE_PTHREAD_SET_NAME_NP) && HAVE_PTHREAD_NP_H
30 #include <pthread_np.h>
31 #endif
32 
33 #include "error.h"
34 
35 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
36 
37 #if HAVE_PTHREADS
38 #include <pthread.h>
39 
40 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
41 
42 #include <stdlib.h>
43 
44 #include "log.h"
45 #include "macros.h"
46 
47 #define ASSERT_PTHREAD_ABORT(func, ret) do { \
48  char errbuf[AV_ERROR_MAX_STRING_SIZE] = ""; \
49  av_log(NULL, AV_LOG_FATAL, AV_STRINGIFY(func) \
50  " failed with error: %s\n", \
51  av_make_error_string(errbuf, AV_ERROR_MAX_STRING_SIZE, \
52  AVERROR(ret))); \
53  abort(); \
54 } while (0)
55 
56 #define ASSERT_PTHREAD_NORET(func, ...) do { \
57  int ret = func(__VA_ARGS__); \
58  if (ret) \
59  ASSERT_PTHREAD_ABORT(func, ret); \
60 } while (0)
61 
62 #define ASSERT_PTHREAD(func, ...) do { \
63  ASSERT_PTHREAD_NORET(func, __VA_ARGS__); \
64  return 0; \
65 } while (0)
66 
67 static inline int strict_pthread_join(pthread_t thread, void **value_ptr)
68 {
69  ASSERT_PTHREAD(pthread_join, thread, value_ptr);
70 }
71 
72 static inline int strict_pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
73 {
74  if (attr) {
75  ASSERT_PTHREAD_NORET(pthread_mutex_init, mutex, attr);
76  } else {
77  pthread_mutexattr_t local_attr;
78  ASSERT_PTHREAD_NORET(pthread_mutexattr_init, &local_attr);
79  ASSERT_PTHREAD_NORET(pthread_mutexattr_settype, &local_attr, PTHREAD_MUTEX_ERRORCHECK);
80  ASSERT_PTHREAD_NORET(pthread_mutex_init, mutex, &local_attr);
81  ASSERT_PTHREAD_NORET(pthread_mutexattr_destroy, &local_attr);
82  }
83  return 0;
84 }
85 
86 static inline int strict_pthread_mutex_destroy(pthread_mutex_t *mutex)
87 {
88  ASSERT_PTHREAD(pthread_mutex_destroy, mutex);
89 }
90 
91 static inline int strict_pthread_mutex_lock(pthread_mutex_t *mutex)
92 {
93  ASSERT_PTHREAD(pthread_mutex_lock, mutex);
94 }
95 
96 static inline int strict_pthread_mutex_unlock(pthread_mutex_t *mutex)
97 {
98  ASSERT_PTHREAD(pthread_mutex_unlock, mutex);
99 }
100 
101 static inline int strict_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
102 {
103  ASSERT_PTHREAD(pthread_cond_init, cond, attr);
104 }
105 
106 static inline int strict_pthread_cond_destroy(pthread_cond_t *cond)
107 {
108  ASSERT_PTHREAD(pthread_cond_destroy, cond);
109 }
110 
111 static inline int strict_pthread_cond_signal(pthread_cond_t *cond)
112 {
113  ASSERT_PTHREAD(pthread_cond_signal, cond);
114 }
115 
116 static inline int strict_pthread_cond_broadcast(pthread_cond_t *cond)
117 {
118  ASSERT_PTHREAD(pthread_cond_broadcast, cond);
119 }
120 
121 static inline int strict_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
122 {
123  ASSERT_PTHREAD(pthread_cond_wait, cond, mutex);
124 }
125 
126 static inline int strict_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
127  const struct timespec *abstime)
128 {
129  int ret = pthread_cond_timedwait(cond, mutex, abstime);
130  if (ret && ret != ETIMEDOUT)
131  ASSERT_PTHREAD_ABORT(pthread_cond_timedwait, ret);
132  return ret;
133 }
134 
135 static inline int strict_pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
136 {
137  ASSERT_PTHREAD(pthread_once, once_control, init_routine);
138 }
139 
140 #define pthread_join strict_pthread_join
141 #define pthread_mutex_init strict_pthread_mutex_init
142 #define pthread_mutex_destroy strict_pthread_mutex_destroy
143 #define pthread_mutex_lock strict_pthread_mutex_lock
144 #define pthread_mutex_unlock strict_pthread_mutex_unlock
145 #define pthread_cond_init strict_pthread_cond_init
146 #define pthread_cond_destroy strict_pthread_cond_destroy
147 #define pthread_cond_signal strict_pthread_cond_signal
148 #define pthread_cond_broadcast strict_pthread_cond_broadcast
149 #define pthread_cond_wait strict_pthread_cond_wait
150 #define pthread_cond_timedwait strict_pthread_cond_timedwait
151 #define pthread_once strict_pthread_once
152 #endif
153 
154 #elif HAVE_OS2THREADS
155 #include "compat/os2threads.h"
156 #else
157 #include "compat/w32pthreads.h"
158 #endif
159 
160 #define AVMutex pthread_mutex_t
161 #define AV_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
162 
163 #define ff_mutex_init pthread_mutex_init
164 #define ff_mutex_lock pthread_mutex_lock
165 #define ff_mutex_unlock pthread_mutex_unlock
166 #define ff_mutex_destroy pthread_mutex_destroy
167 
168 #define AVCond pthread_cond_t
169 
170 #define ff_cond_init pthread_cond_init
171 #define ff_cond_destroy pthread_cond_destroy
172 #define ff_cond_signal pthread_cond_signal
173 #define ff_cond_broadcast pthread_cond_broadcast
174 #define ff_cond_wait pthread_cond_wait
175 #define ff_cond_timedwait pthread_cond_timedwait
176 
177 #define AVOnce pthread_once_t
178 #define AV_ONCE_INIT PTHREAD_ONCE_INIT
179 
180 #define ff_thread_once(control, routine) pthread_once(control, routine)
181 
182 #else
183 
184 #define AVMutex char
185 #define AV_MUTEX_INITIALIZER 0
186 
187 static inline int ff_mutex_init(AVMutex *mutex, const void *attr){ return 0; }
188 static inline int ff_mutex_lock(AVMutex *mutex){ return 0; }
189 static inline int ff_mutex_unlock(AVMutex *mutex){ return 0; }
190 static inline int ff_mutex_destroy(AVMutex *mutex){ return 0; }
191 
192 #define AVCond char
193 
194 static inline int ff_cond_init(AVCond *cond, const void *attr){ return 0; }
195 static inline int ff_cond_destroy(AVCond *cond){ return 0; }
196 static inline int ff_cond_signal(AVCond *cond){ return 0; }
197 static inline int ff_cond_broadcast(AVCond *cond){ return 0; }
198 static inline int ff_cond_wait(AVCond *cond, AVMutex *mutex){ return 0; }
200  const void *abstime){ return 0; }
201 
202 #define AVOnce char
203 #define AV_ONCE_INIT 0
204 
205 static inline int ff_thread_once(char *control, void (*routine)(void))
206 {
207  if (!*control) {
208  routine();
209  *control = 1;
210  }
211  return 0;
212 }
213 
214 #endif
215 
216 static inline int ff_thread_setname(const char *name)
217 {
218  int ret = 0;
219 
220 #if HAVE_PRCTL
221  ret = AVERROR(prctl(PR_SET_NAME, name));
222 #elif HAVE_PTHREAD_SETNAME_NP
223 #if defined(__APPLE__)
224  ret = AVERROR(pthread_setname_np(name));
225 #elif defined(__NetBSD__)
226  ret = AVERROR(pthread_setname_np(pthread_self(), "%s", name));
227 #else
228  ret = AVERROR(pthread_setname_np(pthread_self(), name));
229 #endif
230 #elif HAVE_PTHREAD_SET_NAME_NP
231  pthread_set_name_np(pthread_self(), name);
232 #else
233  ret = AVERROR(ENOSYS);
234 #endif
235 
236  return ret;
237 }
238 
239 #endif /* AVUTIL_THREAD_H */
pthread_mutex_t
_fmutex pthread_mutex_t
Definition: os2threads.h:53
pthread_join
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:94
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
ff_mutex_init
static int ff_mutex_init(AVMutex *mutex, const void *attr)
Definition: thread.h:187
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
w32pthreads.h
pthread_mutex_init
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:104
pthread_mutexattr_t
void pthread_mutexattr_t
Definition: os2threads.h:54
ff_cond_timedwait
static int ff_cond_timedwait(AVCond *cond, AVMutex *mutex, const void *abstime)
Definition: thread.h:199
ff_cond_broadcast
static int ff_cond_broadcast(AVCond *cond)
Definition: thread.h:197
ff_mutex_unlock
static int ff_mutex_unlock(AVMutex *mutex)
Definition: thread.h:189
os2threads.h
macros.h
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
AVMutex
#define AVMutex
Definition: thread.h:184
ff_cond_wait
static int ff_cond_wait(AVCond *cond, AVMutex *mutex)
Definition: thread.h:198
AVCond
#define AVCond
Definition: thread.h:192
pthread_cond_broadcast
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: os2threads.h:162
pthread_once
static av_always_inline int pthread_once(pthread_once_t *once_control, void(*init_routine)(void))
Definition: os2threads.h:210
pthread_mutex_unlock
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:81
error.h
ff_mutex_destroy
static int ff_mutex_destroy(AVMutex *mutex)
Definition: thread.h:190
ff_mutex_lock
static int ff_mutex_lock(AVMutex *mutex)
Definition: thread.h:188
pthread_t
Definition: os2threads.h:44
pthread_cond_destroy
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:144
pthread_mutex_destroy
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:112
log.h
pthread_cond_t
Definition: os2threads.h:58
ret
ret
Definition: filter_design.txt:187
pthread_cond_signal
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:152
ff_cond_signal
static int ff_cond_signal(AVCond *cond)
Definition: thread.h:196
pthread_once_t
Definition: os2threads.h:66
pthread_cond_wait
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:192
ff_cond_destroy
static int ff_cond_destroy(AVCond *cond)
Definition: thread.h:195
pthread_cond_timedwait
static av_always_inline int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
Definition: os2threads.h:170
pthread_cond_init
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: os2threads.h:133
ff_cond_init
static int ff_cond_init(AVCond *cond, const void *attr)
Definition: thread.h:194
pthread_condattr_t
void pthread_condattr_t
Definition: os2threads.h:64
cond
int(* cond)(enum AVPixelFormat pix_fmt)
Definition: pixdesc_query.c:28
mutex
static AVMutex mutex
Definition: log.c:46
pthread_mutex_lock
#define pthread_mutex_lock(a)
Definition: ffprobe.c:77
ff_thread_setname
static int ff_thread_setname(const char *name)
Definition: thread.h:216