2 * This file is part of FFmpeg.
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.
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.
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
19 // This header should only be used to simplify code where
20 // threading is optional, not as a generic threading abstraction.
22 #ifndef AVUTIL_THREAD_H
23 #define AVUTIL_THREAD_H
27 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
32 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
36 #define ASSERT_PTHREAD_NORET(func, ...) do { \
37 int ret = func(__VA_ARGS__); \
39 char errbuf[AV_ERROR_MAX_STRING_SIZE] = ""; \
40 av_log(NULL, AV_LOG_FATAL, AV_STRINGIFY(func) \
41 " failed with error: %s\n", \
42 av_make_error_string(errbuf, AV_ERROR_MAX_STRING_SIZE, \
48 #define ASSERT_PTHREAD(func, ...) do { \
49 ASSERT_PTHREAD_NORET(func, __VA_ARGS__); \
53 static inline int strict_pthread_join(pthread_t thread, void **value_ptr)
55 ASSERT_PTHREAD(pthread_join, thread, value_ptr);
58 static inline int strict_pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
61 ASSERT_PTHREAD_NORET(pthread_mutex_init, mutex, attr);
63 pthread_mutexattr_t local_attr;
64 ASSERT_PTHREAD_NORET(pthread_mutexattr_init, &local_attr);
65 ASSERT_PTHREAD_NORET(pthread_mutexattr_settype, &local_attr, PTHREAD_MUTEX_ERRORCHECK);
66 ASSERT_PTHREAD_NORET(pthread_mutex_init, mutex, &local_attr);
67 ASSERT_PTHREAD_NORET(pthread_mutexattr_destroy, &local_attr);
72 static inline int strict_pthread_mutex_destroy(pthread_mutex_t *mutex)
74 ASSERT_PTHREAD(pthread_mutex_destroy, mutex);
77 static inline int strict_pthread_mutex_lock(pthread_mutex_t *mutex)
79 ASSERT_PTHREAD(pthread_mutex_lock, mutex);
82 static inline int strict_pthread_mutex_unlock(pthread_mutex_t *mutex)
84 ASSERT_PTHREAD(pthread_mutex_unlock, mutex);
87 static inline int strict_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
89 ASSERT_PTHREAD(pthread_cond_init, cond, attr);
92 static inline int strict_pthread_cond_destroy(pthread_cond_t *cond)
94 ASSERT_PTHREAD(pthread_cond_destroy, cond);
97 static inline int strict_pthread_cond_signal(pthread_cond_t *cond)
99 ASSERT_PTHREAD(pthread_cond_signal, cond);
102 static inline int strict_pthread_cond_broadcast(pthread_cond_t *cond)
104 ASSERT_PTHREAD(pthread_cond_broadcast, cond);
107 static inline int strict_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
109 ASSERT_PTHREAD(pthread_cond_wait, cond, mutex);
112 static inline int strict_pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
114 ASSERT_PTHREAD(pthread_once, once_control, init_routine);
117 #define pthread_join strict_pthread_join
118 #define pthread_mutex_init strict_pthread_mutex_init
119 #define pthread_mutex_destroy strict_pthread_mutex_destroy
120 #define pthread_mutex_lock strict_pthread_mutex_lock
121 #define pthread_mutex_unlock strict_pthread_mutex_unlock
122 #define pthread_cond_init strict_pthread_cond_init
123 #define pthread_cond_destroy strict_pthread_cond_destroy
124 #define pthread_cond_signal strict_pthread_cond_signal
125 #define pthread_cond_broadcast strict_pthread_cond_broadcast
126 #define pthread_cond_wait strict_pthread_cond_wait
127 #define pthread_once strict_pthread_once
130 #elif HAVE_OS2THREADS
131 #include "compat/os2threads.h"
133 #include "compat/w32pthreads.h"
136 #define AVMutex pthread_mutex_t
138 #define ff_mutex_init pthread_mutex_init
139 #define ff_mutex_lock pthread_mutex_lock
140 #define ff_mutex_unlock pthread_mutex_unlock
141 #define ff_mutex_destroy pthread_mutex_destroy
143 #define AVOnce pthread_once_t
144 #define AV_ONCE_INIT PTHREAD_ONCE_INIT
146 #define ff_thread_once(control, routine) pthread_once(control, routine)
152 #define ff_mutex_init(mutex, attr) (0)
153 #define ff_mutex_lock(mutex) (0)
154 #define ff_mutex_unlock(mutex) (0)
155 #define ff_mutex_destroy(mutex) (0)
158 #define AV_ONCE_INIT 0
160 static inline int ff_thread_once(char *control, void (*routine)(void))
171 #endif /* AVUTIL_THREAD_H */