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