29 # if !defined (__cplusplus) && (__STDC_VERSION__ >= 201112L) \
30 && !defined (__STDC_NO_ATOMICS__)
33 # include <stdatomic.h>
37 # define ATOMIC_FLAG_INIT false
39 # define ATOMIC_VAR_INIT(value) (value)
41 # define atomic_init(obj, value) \
42 do { *(obj) = (value); } while(0)
44 # define kill_dependency(y) \
47 # define atomic_thread_fence(order) \
50 # define atomic_signal_fence(order) \
53 # define atomic_is_lock_free(obj) \
60 typedef bool atomic_flag;
61 typedef bool atomic_bool;
62 typedef char atomic_char;
63 typedef signed char atomic_schar;
64 typedef unsigned char atomic_uchar;
65 typedef short atomic_short;
66 typedef unsigned short atomic_ushort;
67 typedef int atomic_int;
68 typedef unsigned int atomic_uint;
69 typedef long atomic_long;
70 typedef unsigned long atomic_ulong;
71 typedef long long atomic_llong;
72 typedef unsigned long long atomic_ullong;
75 typedef wchar_t atomic_wchar_t;
76 typedef int_least8_t atomic_int_least8_t;
77 typedef uint_least8_t atomic_uint_least8_t;
78 typedef int_least16_t atomic_int_least16_t;
79 typedef uint_least16_t atomic_uint_least16_t;
80 typedef int_least32_t atomic_int_least32_t;
81 typedef uint_least32_t atomic_uint_least32_t;
82 typedef int_least64_t atomic_int_least64_t;
83 typedef uint_least64_t atomic_uint_least64_t;
84 typedef int_fast8_t atomic_int_fast8_t;
85 typedef uint_fast8_t atomic_uint_fast8_t;
86 typedef int_fast16_t atomic_int_fast16_t;
87 typedef uint_fast16_t atomic_uint_fast16_t;
88 typedef int_fast32_t atomic_int_fast32_t;
89 typedef uint_fast32_t atomic_uint_fast32_t;
90 typedef int_fast64_t atomic_int_fast64_t;
91 typedef uint_fast64_t atomic_uint_fast64_t;
92 typedef intptr_t atomic_intptr_t;
93 typedef uintptr_t atomic_uintptr_t;
94 typedef size_t atomic_size_t;
95 typedef ptrdiff_t atomic_ptrdiff_t;
96 typedef intmax_t atomic_intmax_t;
97 typedef uintmax_t atomic_uintmax_t;
99 # if defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) || (defined (__clang__) && (defined (__x86_64__) || defined (__i386__)))
103 # define atomic_store(object,desired) \
105 *(object) = (desired); \
106 __sync_synchronize(); \
109 # define atomic_store_explicit(object,desired,order) \
110 atomic_store(object,desired)
112 # define atomic_load(object) \
113 (__sync_synchronize(), *(object))
115 # define atomic_load_explicit(object,order) \
118 # define atomic_exchange(object,desired) \
120 typeof (object) _obj = (object); \
121 typeof (*object) _old; \
123 _old = atomic_load(_obj); \
124 while (!__sync_bool_compare_and_swap(_obj, _old, (desired))); \
128 # define atomic_exchange_explicit(object,desired,order) \
129 atomic_exchange(object,desired)
131 # define atomic_compare_exchange(object,expected,desired) \
133 typeof (object) _exp = (expected); \
134 typeof (*object) _old = *_exp; \
135 *_exp = __sync_val_compare_and_swap((object), _old, (desired)); \
139 # define atomic_compare_exchange_strong(object,expected,desired) \
140 atomic_compare_exchange(object, expected, desired)
142 # define atomic_compare_exchange_strong_explicit(object,expected,desired,order) \
143 atomic_compare_exchange_strong(object, expected, desired)
145 # define atomic_compare_exchange_weak(object,expected,desired) \
146 atomic_compare_exchange(object, expected, desired)
148 # define atomic_compare_exchange_weak_explicit(object,expected,desired,order) \
149 atomic_compare_exchange_weak(object, expected, desired)
151 # define atomic_fetch_add(object,operand) \
152 __sync_fetch_and_add(object, operand)
154 # define atomic_fetch_add_explicit(object,operand,order) \
155 atomic_fetch_add(object,operand)
157 # define atomic_fetch_sub(object,operand) \
158 __sync_fetch_and_sub(object, operand)
160 # define atomic_fetch_sub_explicit(object,operand,order) \
161 atomic_fetch_sub(object,operand)
163 # define atomic_fetch_or(object,operand) \
164 __sync_fetch_and_or(object, operand)
166 # define atomic_fetch_or_explicit(object,operand,order) \
167 atomic_fetch_or(object,operand)
169 # define atomic_fetch_xor(object,operand) \
170 __sync_fetch_and_sub(object, operand)
172 # define atomic_fetch_xor_explicit(object,operand,order) \
173 atomic_fetch_sub(object,operand)
175 # define atomic_fetch_and(object,operand) \
176 __sync_fetch_and_and(object, operand)
178 # define atomic_fetch_and_explicit(object,operand,order) \
179 atomic_fetch_and(object,operand)
181 # define atomic_flag_test_and_set(object) \
182 atomic_exchange(object, true)
184 # define atomic_flag_test_and_set_explicit(object,order) \
185 atomic_flag_test_and_set(object)
187 # define atomic_flag_clear(object) \
188 atomic_store(object, false)
190 # define atomic_flag_clear_explicit(object,order) \
191 atomic_flag_clear(object)
193 # elif defined (__GNUC__)
197 # define atomic_store(object,desired) \
199 typeof (object) _obj = (object); \
200 typeof (*object) _des = (desired); \
201 vlc_global_lock(VLC_ATOMIC_MUTEX); \
203 vlc_global_unlock(VLC_ATOMIC_MUTEX); \
205 # define atomic_store_explicit(object,desired,order) \
206 atomic_store(object,desired)
208 # define atomic_load(object) \
210 typeof (object) _obj = (object); \
211 typeof (*object) _old; \
212 vlc_global_lock(VLC_ATOMIC_MUTEX); \
214 vlc_global_unlock(VLC_ATOMIC_MUTEX); \
217 # define atomic_load_explicit(object,order) \
220 # define atomic_exchange(object,desired) \
222 typeof (object) _obj = (object); \
223 typeof (*object) _des = (desired); \
224 typeof (*object) _old; \
225 vlc_global_lock(VLC_ATOMIC_MUTEX); \
228 vlc_global_unlock(VLC_ATOMIC_MUTEX); \
231 # define atomic_exchange_explicit(object,desired,order) \
232 atomic_exchange(object,desired)
234 # define atomic_compare_exchange_strong(object,expected,desired) \
236 typeof (object) _obj = (object); \
237 typeof (object) _exp = (expected); \
238 typeof (*object) _des = (desired); \
240 vlc_global_lock(VLC_ATOMIC_MUTEX); \
241 ret = *_obj == *_exp; \
246 vlc_global_unlock(VLC_ATOMIC_MUTEX); \
249 # define atomic_compare_exchange_strong_explicit(object,expected,desired,order) \
250 atomic_compare_exchange_strong(object, expected, desired)
251 # define atomic_compare_exchange_weak(object,expected,desired) \
252 atomic_compare_exchange_strong(object, expected, desired)
253 # define atomic_compare_exchange_weak_explicit(object,expected,desired,order) \
254 atomic_compare_exchange_weak(object, expected, desired)
256 # define atomic_fetch_OP(object,desired,op) \
258 typeof (object) _obj = (object); \
259 typeof (*object) _des = (desired); \
260 typeof (*object) _old; \
261 vlc_global_lock(VLC_ATOMIC_MUTEX); \
263 *_obj = (*_obj) op (_des); \
264 vlc_global_unlock(VLC_ATOMIC_MUTEX); \
268 # define atomic_fetch_add(object,operand) \
269 atomic_fetch_OP(object,operand,+)
270 # define atomic_fetch_add_explicit(object,operand,order) \
271 atomic_fetch_add(object,operand)
273 # define atomic_fetch_sub(object,operand) \
274 atomic_fetch_OP(object,operand,-)
275 # define atomic_fetch_sub_explicit(object,operand,order) \
276 atomic_fetch_sub(object,operand)
278 # define atomic_fetch_or(object,operand) \
279 atomic_fetch_OP(object,operand,|)
280 # define atomic_fetch_or_explicit(object,operand,order) \
281 atomic_fetch_or(object,operand)
283 # define atomic_fetch_xor(object,operand) \
284 atomic_fetch_OP(object,operand,^)
285 # define atomic_fetch_xor_explicit(object,operand,order) \
286 atomic_fetch_sub(object,operand)
288 # define atomic_fetch_and(object,operand) \
289 atomic_fetch_OP(object,operand,&)
290 # define atomic_fetch_and_explicit(object,operand,order) \
291 atomic_fetch_and(object,operand)
293 # define atomic_flag_test_and_set(object) \
294 atomic_exchange(object, true)
296 # define atomic_flag_test_and_set_explicit(object,order) \
297 atomic_flag_test_and_set(object)
299 # define atomic_flag_clear(object) \
300 atomic_store(object, false)
302 # define atomic_flag_clear_explicit(object,order) \
303 atomic_flag_clear(object)
306 # error FIXME: implement atomic operations for this compiler.
319 # define VLC_ATOMIC_INIT(val) { (val) }
322 static inline uintptr_t vlc_atomic_get(
vlc_atomic_t *atom)
324 return atomic_load(&atom->u);
327 static inline uintptr_t vlc_atomic_set(
vlc_atomic_t *atom, uintptr_t v)
329 atomic_store(&atom->u, v);
333 static inline uintptr_t vlc_atomic_add(
vlc_atomic_t *atom, uintptr_t v)
335 return atomic_fetch_add(&atom->u, v) + v;
338 static inline uintptr_t vlc_atomic_sub (
vlc_atomic_t *atom, uintptr_t v)
340 return atomic_fetch_sub (&atom->u, v) - v;
343 static inline uintptr_t vlc_atomic_inc (
vlc_atomic_t *atom)
345 return vlc_atomic_add (atom, 1);
348 static inline uintptr_t vlc_atomic_dec (
vlc_atomic_t *atom)
350 return vlc_atomic_sub (atom, 1);
353 static inline uintptr_t vlc_atomic_swap(
vlc_atomic_t *atom, uintptr_t v)
355 return atomic_exchange(&atom->u, v);
358 static inline uintptr_t vlc_atomic_compare_swap(
vlc_atomic_t *atom,
359 uintptr_t u, uintptr_t v)
361 atomic_compare_exchange_strong(&atom->u, &u, v);
365 typedef atomic_uint_least32_t vlc_atomic_float;
368 static inline float vlc_atomic_loadf(vlc_atomic_float *atom)
370 union {
float f; uint32_t i; } u;
371 u.i = atomic_load(atom);
376 static inline void vlc_atomic_storef(vlc_atomic_float *atom,
float f)
378 union {
float f; uint32_t i; } u;
380 atomic_store(atom, u.i);
Definition: vlc_atomic.h:313