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