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 #endif
30 
31 #include "error.h"
32 
33 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
34 
35 #if HAVE_PTHREADS
36 #include <pthread.h>
37 
38 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
39 
40 #include <stdlib.h>
41 
42 #include "log.h"
43 #include "macros.h"
44 
45 #define ASSERT_PTHREAD_ABORT(func, ret) do { \
46  char errbuf[AV_ERROR_MAX_STRING_SIZE] = ""; \
47  av_log(NULL, AV_LOG_FATAL, AV_STRINGIFY(func) \
48  " failed with error: %s\n", \
49  av_make_error_string(errbuf, AV_ERROR_MAX_STRING_SIZE, \
50  AVERROR(ret))); \
51  abort(); \
52 } while (0)
53 
54 #define ASSERT_PTHREAD_NORET(func, ...) do { \
55  int ret = func(__VA_ARGS__); \
56  if (ret) \
57  ASSERT_PTHREAD_ABORT(func, ret); \
58 } while (0)
59 
60 #define ASSERT_PTHREAD(func, ...) do { \
61  ASSERT_PTHREAD_NORET(func, __VA_ARGS__); \
62  return 0; \
63 } while (0)
64 
65 static inline int strict_pthread_join(pthread_t thread, void **value_ptr)
66 {
67  ASSERT_PTHREAD(pthread_join, thread, value_ptr);
68 }
69 
70 static inline int strict_pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
71 {
72  if (attr) {
73  ASSERT_PTHREAD_NORET(pthread_mutex_init, mutex, attr);
74  } else {
75  pthread_mutexattr_t local_attr;
76  ASSERT_PTHREAD_NORET(pthread_mutexattr_init, &local_attr);
77  ASSERT_PTHREAD_NORET(pthread_mutexattr_settype, &local_attr, PTHREAD_MUTEX_ERRORCHECK);
78  ASSERT_PTHREAD_NORET(pthread_mutex_init, mutex, &local_attr);
79  ASSERT_PTHREAD_NORET(pthread_mutexattr_destroy, &local_attr);
80  }
81  return 0;
82 }
83 
84 static inline int strict_pthread_mutex_destroy(pthread_mutex_t *mutex)
85 {
86  ASSERT_PTHREAD(pthread_mutex_destroy, mutex);
87 }
88 
89 static inline int strict_pthread_mutex_lock(pthread_mutex_t *mutex)
90 {
91  ASSERT_PTHREAD(pthread_mutex_lock, mutex);
92 }
93 
94 static inline int strict_pthread_mutex_unlock(pthread_mutex_t *mutex)
95 {
96  ASSERT_PTHREAD(pthread_mutex_unlock, mutex);
97 }
98 
99 static inline int strict_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
100 {
101  ASSERT_PTHREAD(pthread_cond_init, cond, attr);
102 }
103 
104 static inline int strict_pthread_cond_destroy(pthread_cond_t *cond)
105 {
106  ASSERT_PTHREAD(pthread_cond_destroy, cond);
107 }
108 
109 static inline int strict_pthread_cond_signal(pthread_cond_t *cond)
110 {
111  ASSERT_PTHREAD(pthread_cond_signal, cond);
112 }
113 
114 static inline int strict_pthread_cond_broadcast(pthread_cond_t *cond)
115 {
116  ASSERT_PTHREAD(pthread_cond_broadcast, cond);
117 }
118 
119 static inline int strict_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
120 {
121  ASSERT_PTHREAD(pthread_cond_wait, cond, mutex);
122 }
123 
124 static inline int strict_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
125  const struct timespec *abstime)
126 {
127  int ret = pthread_cond_timedwait(cond, mutex, abstime);
128  if (ret && ret != ETIMEDOUT)
129  ASSERT_PTHREAD_ABORT(pthread_cond_timedwait, ret);
130  return ret;
131 }
132 
133 static inline int strict_pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
134 {
135  ASSERT_PTHREAD(pthread_once, once_control, init_routine);
136 }
137 
138 #define pthread_join strict_pthread_join
139 #define pthread_mutex_init strict_pthread_mutex_init
140 #define pthread_mutex_destroy strict_pthread_mutex_destroy
141 #define pthread_mutex_lock strict_pthread_mutex_lock
142 #define pthread_mutex_unlock strict_pthread_mutex_unlock
143 #define pthread_cond_init strict_pthread_cond_init
144 #define pthread_cond_destroy strict_pthread_cond_destroy
145 #define pthread_cond_signal strict_pthread_cond_signal
146 #define pthread_cond_broadcast strict_pthread_cond_broadcast
147 #define pthread_cond_wait strict_pthread_cond_wait
148 #define pthread_cond_timedwait strict_pthread_cond_timedwait
149 #define pthread_once strict_pthread_once
150 #endif
151 
152 #elif HAVE_OS2THREADS
153 #include "compat/os2threads.h"
154 #else
155 #include "compat/w32pthreads.h"
156 #endif
157 
158 #define AVMutex pthread_mutex_t
159 #define AV_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
160 
161 #define ff_mutex_init pthread_mutex_init
162 #define ff_mutex_lock pthread_mutex_lock
163 #define ff_mutex_unlock pthread_mutex_unlock
164 #define ff_mutex_destroy pthread_mutex_destroy
165 
166 #define AVCond pthread_cond_t
167 
168 #define ff_cond_init pthread_cond_init
169 #define ff_cond_destroy pthread_cond_destroy
170 #define ff_cond_signal pthread_cond_signal
171 #define ff_cond_broadcast pthread_cond_broadcast
172 #define ff_cond_wait pthread_cond_wait
173 #define ff_cond_timedwait pthread_cond_timedwait
174 
175 #define AVOnce pthread_once_t
176 #define AV_ONCE_INIT PTHREAD_ONCE_INIT
177 
178 #define ff_thread_once(control, routine) pthread_once(control, routine)
179 
180 #else
181 
182 #define AVMutex char
183 #define AV_MUTEX_INITIALIZER 0
184 
185 static inline int ff_mutex_init(AVMutex *mutex, const void *attr){ return 0; }
186 static inline int ff_mutex_lock(AVMutex *mutex){ return 0; }
187 static inline int ff_mutex_unlock(AVMutex *mutex){ return 0; }
188 static inline int ff_mutex_destroy(AVMutex *mutex){ return 0; }
189 
190 #define AVCond char
191 
192 static inline int ff_cond_init(AVCond *cond, const void *attr){ return 0; }
193 static inline int ff_cond_destroy(AVCond *cond){ return 0; }
194 static inline int ff_cond_signal(AVCond *cond){ return 0; }
195 static inline int ff_cond_broadcast(AVCond *cond){ return 0; }
196 static inline int ff_cond_wait(AVCond *cond, AVMutex *mutex){ return 0; }
198  const void *abstime){ return 0; }
199 
200 #define AVOnce char
201 #define AV_ONCE_INIT 0
202 
203 static inline int ff_thread_once(char *control, void (*routine)(void))
204 {
205  if (!*control) {
206  routine();
207  *control = 1;
208  }
209  return 0;
210 }
211 
212 #endif
213 
214 static inline int ff_thread_setname(const char *name)
215 {
216 #if HAVE_PRCTL
217  return AVERROR(prctl(PR_SET_NAME, name));
218 #endif
219 
220  return AVERROR(ENOSYS);
221 }
222 
223 #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:185
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:197
ff_cond_broadcast
static int ff_cond_broadcast(AVCond *cond)
Definition: thread.h:195
ff_mutex_unlock
static int ff_mutex_unlock(AVMutex *mutex)
Definition: thread.h:187
os2threads.h
macros.h
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:203
AVMutex
#define AVMutex
Definition: thread.h:182
ff_cond_wait
static int ff_cond_wait(AVCond *cond, AVMutex *mutex)
Definition: thread.h:196
AVCond
#define AVCond
Definition: thread.h:190
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:79
error.h
ff_mutex_destroy
static int ff_mutex_destroy(AVMutex *mutex)
Definition: thread.h:188
ff_mutex_lock
static int ff_mutex_lock(AVMutex *mutex)
Definition: thread.h:186
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:194
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:193
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:192
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:75
ff_thread_setname
static int ff_thread_setname(const char *name)
Definition: thread.h:214