1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2010 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
26 * Atomic operations do not require locking, but they are not very powerful.
29 # if !defined (__cplusplus) && (__STDC_VERSION__ >= 201112L) \
30 && !defined (__STDC_NO_ATOMICS__)
32 /*** Native C11 atomics ***/
33 # include <stdatomic.h>
35 # elif defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) || (defined (__clang__) && (defined (__x86_64__) || defined (__i386__)))
37 /*** Intel/GCC atomics ***/
39 # define ATOMIC_FLAG_INIT false
41 # define ATOMIC_VAR_INIT(value) (value)
43 # define atomic_init(obj, value) \
44 do { *(obj) = (value); } while(0)
46 # define kill_dependency(y) \
49 # define atomic_thread_fence(order) \
52 # define atomic_signal_fence(order) \
55 # define atomic_is_lock_free(obj) \
58 /* In principles, __sync_*() only supports int, long and long long and their
59 * unsigned equivalents, i.e. 4-bytes and 8-bytes types, although GCC also
60 * supports 1 and 2-bytes types. Some non-x86 architectures do not support
61 * 8-byte atomic types (or not efficiently). */
62 typedef bool atomic_flag;
63 typedef bool atomic_bool;
64 typedef char atomic_char;
65 typedef signed char atomic_schar;
66 typedef unsigned char atomic_uchar;
67 typedef short atomic_short;
68 typedef unsigned short atomic_ushort;
69 typedef int atomic_int;
70 typedef unsigned int atomic_uint;
71 typedef long atomic_long;
72 typedef unsigned long atomic_ulong;
73 typedef long long atomic_llong;
74 typedef unsigned long long atomic_ullong;
75 //typedef char16_t atomic_char16_t;
76 //typedef char32_t atomic_char32_t;
77 typedef wchar_t atomic_wchar_t;
78 typedef int_least8_t atomic_int_least8_t;
79 typedef uint_least8_t atomic_uint_least8_t;
80 typedef int_least16_t atomic_int_least16_t;
81 typedef uint_least16_t atomic_uint_least16_t;
82 typedef int_least32_t atomic_int_least32_t;
83 typedef uint_least32_t atomic_uint_least32_t;
84 typedef int_least64_t atomic_int_least64_t;
85 typedef uint_least64_t atomic_uint_least64_t;
86 typedef int_fast8_t atomic_int_fast8_t;
87 typedef uint_fast8_t atomic_uint_fast8_t;
88 typedef int_fast16_t atomic_int_fast16_t;
89 typedef uint_fast16_t atomic_uint_fast16_t;
90 typedef int_fast32_t atomic_int_fast32_t;
91 typedef uint_fast32_t atomic_uint_fast32_t;
92 typedef int_fast64_t atomic_int_fast64_t;
93 typedef uint_fast64_t atomic_uint_fast64_t;
94 typedef intptr_t atomic_intptr_t;
95 typedef uintptr_t atomic_uintptr_t;
96 typedef size_t atomic_size_t;
97 typedef ptrdiff_t atomic_ptrdiff_t;
98 typedef intmax_t atomic_intmax_t;
99 typedef uintmax_t atomic_uintmax_t;
101 # define atomic_store(object,desired) \
103 *(object) = (desired); \
104 __sync_synchronize(); \
107 # define atomic_store_explicit(object,desired,order) \
108 atomic_store(object,desired)
110 # define atomic_load(object) \
111 (__sync_synchronize(), *(object))
113 # define atomic_load_explicit(object,order) \
116 # define atomic_exchange(object,desired) \
118 typeof (object) _obj = (object); \
119 typeof (*object) _old; \
121 _old = atomic_load(_obj); \
122 while (!__sync_bool_compare_and_swap(_obj, _old, (desired))); \
126 # define atomic_exchange_explicit(object,desired,order) \
127 atomic_exchange(object,desired)
129 # define atomic_compare_exchange(object,expected,desired) \
131 typeof (object) _exp = (expected); \
132 typeof (*object) _old = *_exp; \
133 *_exp = __sync_val_compare_and_swap((object), _old, (desired)); \
137 # define atomic_compare_exchange_strong(object,expected,desired) \
138 atomic_compare_exchange(object, expected, desired)
140 # define atomic_compare_exchange_strong_explicit(object,expected,desired,order) \
141 atomic_compare_exchange_strong(object, expected, desired)
143 # define atomic_compare_exchange_weak(object,expected,desired) \
144 atomic_compare_exchange(object, expected, desired)
146 # define atomic_compare_exchange_weak_explicit(object,expected,desired,order) \
147 atomic_compare_exchange_weak(object, expected, desired)
149 # define atomic_fetch_add(object,operand) \
150 __sync_fetch_and_add(object, operand)
152 # define atomic_fetch_add_explicit(object,operand,order) \
153 atomic_fetch_add(object,operand)
155 # define atomic_fetch_sub(object,operand) \
156 __sync_fetch_and_sub(object, operand)
158 # define atomic_fetch_sub_explicit(object,operand,order) \
159 atomic_fetch_sub(object,operand)
161 # define atomic_fetch_or(object,operand) \
162 __sync_fetch_and_or(object, operand)
164 # define atomic_fetch_or_explicit(object,operand,order) \
165 atomic_fetch_or(object,operand)
167 # define atomic_fetch_xor(object,operand) \
168 __sync_fetch_and_sub(object, operand)
170 # define atomic_fetch_xor_explicit(object,operand,order) \
171 atomic_fetch_sub(object,operand)
173 # define atomic_fetch_and(object,operand) \
174 __sync_fetch_and_and(object, operand)
176 # define atomic_fetch_and_explicit(object,operand,order) \
177 atomic_fetch_and(object,operand)
179 # define atomic_flag_test_and_set(object) \
180 atomic_exchange(object, true)
182 # define atomic_flag_test_and_set_explicit(object,order) \
183 atomic_flag_test_and_set(object)
185 # define atomic_flag_clear(object) \
186 atomic_store(object, false)
188 # define atomic_flag_clear_explicit(object,order) \
189 atomic_flag_clear(object)
195 # define ATOMIC_FLAG_INIT false
197 # define ATOMIC_VAR_INIT(value) (value)
199 # define atomic_init(obj, value) \
200 do { *(obj) = (value); } while(0)
202 # define kill_dependency(y) \
205 # define atomic_thread_fence(order) \
208 # define atomic_signal_fence(order) \
211 # define atomic_is_lock_free(obj) \
214 typedef uintptr_t atomic_generic_t;
215 typedef atomic_generic_t atomic_flag;
216 typedef atomic_generic_t atomic_bool;
217 //typedef atomic_generic_t atomic_char;
218 //typedef atomic_generic_t atomic_schar;
219 typedef atomic_generic_t atomic_uchar;
220 //typedef atomic_generic_t atomic_short;
221 typedef atomic_generic_t atomic_ushort;
222 //typedef atomic_generic_t atomic_int;
223 typedef atomic_generic_t atomic_uint;
224 //typedef atomic_generic_t atomic_long;
225 typedef atomic_generic_t atomic_ulong;
226 //typedef atomic_generic_t atomic_llong;
227 //typedef atomic_generic_t atomic_ullong;
228 //typedef atomic_generic_t atomic_char16_t;
229 //typedef atomic_generic_t atomic_char32_t;
230 //typedef atomic_generic_t atomic_wchar_t;
231 //typedef atomic_generic_t atomic_int_least8_t;
232 typedef atomic_generic_t atomic_uint_least8_t;
233 //typedef atomic_generic_t atomic_int_least16_t;
234 typedef atomic_generic_t atomic_uint_least16_t;
235 //typedef atomic_generic_t atomic_int_least32_t;
236 typedef atomic_generic_t atomic_uint_least32_t;
237 //typedef atomic_generic_t atomic_int_least64_t;
238 //typedef atomic_generic_t atomic_uint_least64_t;
239 //typedef atomic_generic_t atomic_int_fast8_t;
240 typedef atomic_generic_t atomic_uint_fast8_t;
241 //typedef atomic_generic_t atomic_int_fast16_t;
242 typedef atomic_generic_t atomic_uint_fast16_t;
243 //typedef atomic_generic_t atomic_int_fast32_t;
244 typedef atomic_generic_t atomic_uint_fast32_t;
245 //typedef atomic_generic_t atomic_int_fast64_t
246 //typedef atomic_generic_t atomic_uint_fast64_t;
247 //typedef atomic_generic_t atomic_intptr_t;
248 typedef atomic_generic_t atomic_uintptr_t;
249 typedef atomic_generic_t atomic_size_t;
250 //typedef atomic_generic_t atomic_ptrdiff_t;
251 //typedef atomic_generic_t atomic_intmax_t;
252 //typedef atomic_generic_t atomic_uintmax_t;
254 #define atomic_store(object,desired) \
256 vlc_global_lock(VLC_ATOMIC_MUTEX); \
257 *(object) = (desired); \
258 vlc_global_unlock(VLC_ATOMIC_MUTEX); \
261 # define atomic_store_explicit(object,desired,order) \
262 atomic_store(object,desired)
264 static inline uintptr_t atomic_load(atomic_generic_t *object)
268 vlc_global_lock(VLC_ATOMIC_MUTEX);
270 vlc_global_unlock(VLC_ATOMIC_MUTEX);
273 # define atomic_load_explicit(object,order) \
277 uintptr_t atomic_exchange(atomic_generic_t *object, uintptr_t desired)
281 vlc_global_lock(VLC_ATOMIC_MUTEX);
284 vlc_global_unlock(VLC_ATOMIC_MUTEX);
287 # define atomic_exchange_explicit(object,desired,order) \
288 atomic_exchange(object,desired)
291 bool vlc_atomic_compare_exchange(atomic_generic_t *object,
292 uintptr_t *expected, uintptr_t desired)
296 vlc_global_lock(VLC_ATOMIC_MUTEX);
297 ret = *object == *expected;
302 vlc_global_unlock(VLC_ATOMIC_MUTEX);
305 # define atomic_compare_exchange_strong(object,expected,desired) \
306 vlc_atomic_compare_exchange(object, expected, desired)
307 # define atomic_compare_exchange_strong_explicit(object,expected,desired,order) \
308 atomic_compare_exchange_strong(object, expected, desired)
309 # define atomic_compare_exchange_weak(object,expected,desired) \
310 vlc_atomic_compare_exchange(object, expected, desired)
311 # define atomic_compare_exchange_weak_explicit(object,expected,desired,order) \
312 atomic_compare_exchange_weak(object, expected, desired)
315 uintmax_t atomic_fetch_add(atomic_generic_t *object, uintptr_t operand)
319 vlc_global_lock(VLC_ATOMIC_MUTEX);
322 vlc_global_unlock(VLC_ATOMIC_MUTEX);
325 # define atomic_fetch_add_explicit(object,operand,order) \
326 atomic_fetch_add(object,operand)
329 uintptr_t atomic_fetch_sub(atomic_generic_t *object, uintptr_t operand)
333 vlc_global_lock(VLC_ATOMIC_MUTEX);
336 vlc_global_unlock(VLC_ATOMIC_MUTEX);
339 # define atomic_fetch_sub_explicit(object,operand,order) \
340 atomic_fetch_sub(object,operand)
343 uintptr_t atomic_fetch_or(atomic_generic_t *object, uintptr_t operand)
347 vlc_global_lock(VLC_ATOMIC_MUTEX);
350 vlc_global_unlock(VLC_ATOMIC_MUTEX);
353 # define atomic_fetch_or_explicit(object,operand,order) \
354 atomic_fetch_or(object,operand)
357 uintptr_t atomic_fetch_xor(atomic_generic_t *object, uintptr_t operand)
361 vlc_global_lock(VLC_ATOMIC_MUTEX);
364 vlc_global_unlock(VLC_ATOMIC_MUTEX);
367 # define atomic_fetch_xor_explicit(object,operand,order) \
368 atomic_fetch_sub(object,operand)
371 uintptr_t atomic_fetch_and(atomic_generic_t *object, uintptr_t operand)
375 vlc_global_lock(VLC_ATOMIC_MUTEX);
378 vlc_global_unlock(VLC_ATOMIC_MUTEX);
381 # define atomic_fetch_and_explicit(object,operand,order) \
382 atomic_fetch_and(object,operand)
384 # define atomic_flag_test_and_set(object) \
385 atomic_exchange(object, true)
387 # define atomic_flag_test_and_set_explicit(object,order) \
388 atomic_flag_test_and_set(object)
390 # define atomic_flag_clear(object) \
391 atomic_store(object, false)
393 # define atomic_flag_clear_explicit(object,order) \
394 atomic_flag_clear(object)
399 * Memory storage space for an atom. Never access it directly.
406 /** Static initializer for \ref vlc_atomic_t */
407 # define VLC_ATOMIC_INIT(val) { (val) }
409 /* All functions return the atom value _after_ the operation. */
410 static inline uintptr_t vlc_atomic_get(vlc_atomic_t *atom)
412 return atomic_load(&atom->u);
415 static inline uintptr_t vlc_atomic_set(vlc_atomic_t *atom, uintptr_t v)
417 atomic_store(&atom->u, v);
421 static inline uintptr_t vlc_atomic_add(vlc_atomic_t *atom, uintptr_t v)
423 return atomic_fetch_add(&atom->u, v) + v;
426 static inline uintptr_t vlc_atomic_sub (vlc_atomic_t *atom, uintptr_t v)
428 return atomic_fetch_sub (&atom->u, v) - v;
431 static inline uintptr_t vlc_atomic_inc (vlc_atomic_t *atom)
433 return vlc_atomic_add (atom, 1);
436 static inline uintptr_t vlc_atomic_dec (vlc_atomic_t *atom)
438 return vlc_atomic_sub (atom, 1);
441 static inline uintptr_t vlc_atomic_swap(vlc_atomic_t *atom, uintptr_t v)
443 return atomic_exchange(&atom->u, v);
446 static inline uintptr_t vlc_atomic_compare_swap(vlc_atomic_t *atom,
447 uintptr_t u, uintptr_t v)
449 atomic_compare_exchange_strong(&atom->u, &u, v);
453 /** Helper to retrieve a single precision from an atom. */
454 static inline float vlc_atomic_getf(vlc_atomic_t *atom)
456 union { float f; uintptr_t i; } u;
457 u.i = vlc_atomic_get(atom);
461 /** Helper to store a single precision into an atom. */
462 static inline float vlc_atomic_setf(vlc_atomic_t *atom, float f)
464 union { float f; uintptr_t i; } u;
466 vlc_atomic_set(atom, u.i);