Browse code

Add a compat stdatomic.h implementation based on GCC atomics

Adapted from the code by Rémi Denis-Courmont from VLC

This merges libav commit 4e928ef340ac20325f529d92fcbc51e768085358.

Signed-off-by: Wan-Teh Chang <wtc@google.com>
Reviewed-by: wm4 <nfxjfg@googlemail.com>
Signed-off-by: James Almer <jamrial@gmail.com>

Anton Khirnov authored on 2016/11/29 06:24:22
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,173 @@
0
+/*
1
+ * This file is part of FFmpeg.
2
+ *
3
+ * FFmpeg is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * FFmpeg is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with FFmpeg; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+/*
19
+ * based on vlc_atomic.h from VLC
20
+ * Copyright (C) 2010 Rémi Denis-Courmont
21
+ */
22
+
23
+#ifndef FFMPEG_COMPAT_ATOMICS_GCC_STDATOMIC_H
24
+#define FFMPEG_COMPAT_ATOMICS_GCC_STDATOMIC_H
25
+
26
+#include <stddef.h>
27
+#include <stdint.h>
28
+
29
+#define ATOMIC_FLAG_INIT 0
30
+
31
+#define ATOMIC_VAR_INIT(value) (value)
32
+
33
+#define atomic_init(obj, value) \
34
+do {                            \
35
+    *(obj) = (value);           \
36
+} while(0)
37
+
38
+#define kill_dependency(y) ((void)0)
39
+
40
+#define atomic_thread_fence(order) \
41
+    __sync_synchronize()
42
+
43
+#define atomic_signal_fence(order) \
44
+    ((void)0)
45
+
46
+#define atomic_is_lock_free(obj) 0
47
+
48
+typedef         _Bool      atomic_flag;
49
+typedef         _Bool      atomic_bool;
50
+typedef          char      atomic_char;
51
+typedef   signed char      atomic_schar;
52
+typedef unsigned char      atomic_uchar;
53
+typedef          short     atomic_short;
54
+typedef unsigned short     atomic_ushort;
55
+typedef          int       atomic_int;
56
+typedef unsigned int       atomic_uint;
57
+typedef          long      atomic_long;
58
+typedef unsigned long      atomic_ulong;
59
+typedef          long long atomic_llong;
60
+typedef unsigned long long atomic_ullong;
61
+typedef          wchar_t   atomic_wchar_t;
62
+typedef       int_least8_t atomic_int_least8_t;
63
+typedef      uint_least8_t atomic_uint_least8_t;
64
+typedef      int_least16_t atomic_int_least16_t;
65
+typedef     uint_least16_t atomic_uint_least16_t;
66
+typedef      int_least32_t atomic_int_least32_t;
67
+typedef     uint_least32_t atomic_uint_least32_t;
68
+typedef      int_least64_t atomic_int_least64_t;
69
+typedef     uint_least64_t atomic_uint_least64_t;
70
+typedef       int_fast8_t atomic_int_fast8_t;
71
+typedef      uint_fast8_t atomic_uint_fast8_t;
72
+typedef      int_fast16_t atomic_int_fast16_t;
73
+typedef     uint_fast16_t atomic_uint_fast16_t;
74
+typedef      int_fast32_t atomic_int_fast32_t;
75
+typedef     uint_fast32_t atomic_uint_fast32_t;
76
+typedef      int_fast64_t atomic_int_fast64_t;
77
+typedef     uint_fast64_t atomic_uint_fast64_t;
78
+typedef          intptr_t atomic_intptr_t;
79
+typedef         uintptr_t atomic_uintptr_t;
80
+typedef            size_t atomic_size_t;
81
+typedef         ptrdiff_t atomic_ptrdiff_t;
82
+typedef          intmax_t atomic_intmax_t;
83
+typedef         uintmax_t atomic_uintmax_t;
84
+
85
+#define atomic_store(object, desired)   \
86
+do {                                    \
87
+    *(object) = (desired);              \
88
+    __sync_synchronize();               \
89
+} while (0)
90
+
91
+#define atomic_store_explicit(object, desired, order) \
92
+    atomic_store(object, desired)
93
+
94
+#define atomic_load(object) \
95
+    (__sync_synchronize(), *(object))
96
+
97
+#define atomic_load_explicit(object, order) \
98
+    atomic_load(object)
99
+
100
+#define atomic_exchange(object, desired)                            \
101
+({                                                                  \
102
+    typeof(object) _obj = (object);                                 \
103
+    typeof(*object) _old;                                           \
104
+    do                                                              \
105
+        _old = atomic_load(_obj);                                   \
106
+    while (!__sync_bool_compare_and_swap(_obj, _old, (desired)));   \
107
+    _old;                                                           \
108
+})
109
+
110
+#define atomic_exchange_explicit(object, desired, order) \
111
+    atomic_exchange(object, desired)
112
+
113
+#define atomic_compare_exchange_strong(object, expected, desired)   \
114
+({                                                                  \
115
+    typeof(object) _exp = (expected);                               \
116
+    typeof(*object) _old = *_exp;                                   \
117
+    *_exp = __sync_val_compare_and_swap((object), _old, (desired)); \
118
+    *_exp == _old;                                                  \
119
+})
120
+
121
+#define atomic_compare_exchange_strong_explicit(object, expected, desired, success, failure) \
122
+    atomic_compare_exchange_strong(object, expected, desired)
123
+
124
+#define atomic_compare_exchange_weak(object, expected, desired) \
125
+    atomic_compare_exchange_strong(object, expected, desired)
126
+
127
+#define atomic_compare_exchange_weak_explicit(object, expected, desired, success, failure) \
128
+    atomic_compare_exchange_weak(object, expected, desired)
129
+
130
+#define atomic_fetch_add(object, operand) \
131
+    __sync_fetch_and_add(object, operand)
132
+
133
+#define atomic_fetch_add_explicit(object, operand, order) \
134
+    atomic_fetch_add(object, operand)
135
+
136
+#define atomic_fetch_sub(object, operand) \
137
+    __sync_fetch_and_sub(object, operand)
138
+
139
+#define atomic_fetch_sub_explicit(object, operand, order) \
140
+    atomic_fetch_sub(object, operand)
141
+
142
+#define atomic_fetch_or(object, operand) \
143
+    __sync_fetch_and_or(object, operand)
144
+
145
+#define atomic_fetch_or_explicit(object, operand, order) \
146
+    atomic_fetch_or(object, operand)
147
+
148
+#define atomic_fetch_xor(object, operand) \
149
+    __sync_fetch_and_sub(object, operand)
150
+
151
+#define atomic_fetch_xor_explicit(object, operand, order) \
152
+    atomic_fetch_sub(object, operand)
153
+
154
+#define atomic_fetch_and(object, operand) \
155
+    __sync_fetch_and_and(object, operand)
156
+
157
+#define atomic_fetch_and_explicit(object, operand, order) \
158
+    atomic_fetch_and(object, operand)
159
+
160
+#define atomic_flag_test_and_set(object) \
161
+    atomic_exchange(object, 1)
162
+
163
+#define atomic_flag_test_and_set_explicit(object, order) \
164
+    atomic_flag_test_and_set(object)
165
+
166
+#define atomic_flag_clear(object) \
167
+    atomic_store(object, 0)
168
+
169
+#define atomic_flag_clear_explicit(object, order) \
170
+    atomic_flag_clear(object)
171
+
172
+#endif /* FFMPEG_COMPAT_ATOMICS_GCC_STDATOMIC_H */
... ...
@@ -6381,6 +6381,12 @@ for thread in $THREADS_LIST; do
6381 6381
     fi
6382 6382
 done
6383 6383
 
6384
+if disabled stdatomic_h; then
6385
+    if enabled atomics_gcc; then
6386
+        add_cppflags '-I\$(SRC_PATH)/compat/atomics/gcc'
6387
+    fi
6388
+fi
6389
+
6384 6390
 enabled zlib && add_cppflags -DZLIB_CONST
6385 6391
 
6386 6392
 # conditional library dependencies, in linking order