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