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 /* Clang older versions support atomics but lacks the stdatomic.h header */
30 #if defined(__clang__)
31 # if !defined(__has_include) || !__has_include(<stdatomic.h>)
32 # define __STDC_NO_ATOMICS__ 1
37 # if (__STDC_VERSION__ >= 201112L) && !defined (__STDC_NO_ATOMICS__)
38 /*** Native C11 atomics ***/
39 # include <stdatomic.h>
42 /*** Intel/GCC atomics ***/
44 # define ATOMIC_FLAG_INIT false
46 # define ATOMIC_VAR_INIT(value) (value)
48 # define atomic_init(obj, value) \
49 do { *(obj) = (value); } while(0)
51 # define kill_dependency(y) \
54 # define atomic_thread_fence(order) \
57 # define atomic_signal_fence(order) \
60 # define atomic_is_lock_free(obj) \
63 typedef bool atomic_flag
;
64 typedef bool atomic_bool
;
65 typedef char atomic_char
;
66 typedef signed char atomic_schar
;
67 typedef unsigned char atomic_uchar
;
68 typedef short atomic_short
;
69 typedef unsigned short atomic_ushort
;
70 typedef int atomic_int
;
71 typedef unsigned int atomic_uint
;
72 typedef long atomic_long
;
73 typedef unsigned long atomic_ulong
;
74 typedef long long atomic_llong
;
75 typedef unsigned long long atomic_ullong
;
76 //typedef char16_t atomic_char16_t;
77 //typedef char32_t atomic_char32_t;
78 typedef wchar_t atomic_wchar_t
;
79 typedef int_least8_t atomic_int_least8_t
;
80 typedef uint_least8_t atomic_uint_least8_t
;
81 typedef int_least16_t atomic_int_least16_t
;
82 typedef uint_least16_t atomic_uint_least16_t
;
83 typedef int_least32_t atomic_int_least32_t
;
84 typedef uint_least32_t atomic_uint_least32_t
;
85 typedef int_least64_t atomic_int_least64_t
;
86 typedef uint_least64_t atomic_uint_least64_t
;
87 typedef int_fast8_t atomic_int_fast8_t
;
88 typedef uint_fast8_t atomic_uint_fast8_t
;
89 typedef int_fast16_t atomic_int_fast16_t
;
90 typedef uint_fast16_t atomic_uint_fast16_t
;
91 typedef int_fast32_t atomic_int_fast32_t
;
92 typedef uint_fast32_t atomic_uint_fast32_t
;
93 typedef int_fast64_t atomic_int_fast64_t
;
94 typedef uint_fast64_t atomic_uint_fast64_t
;
95 typedef intptr_t atomic_intptr_t
;
96 typedef uintptr_t atomic_uintptr_t
;
97 typedef size_t atomic_size_t
;
98 typedef ptrdiff_t atomic_ptrdiff_t
;
99 typedef intmax_t atomic_intmax_t
;
100 typedef uintmax_t atomic_uintmax_t
;
102 # define atomic_store(object,desired) \
104 *(object) = (desired); \
105 __sync_synchronize(); \
108 # define atomic_store_explicit(object,desired,order) \
109 atomic_store(object,desired)
111 # define atomic_load(object) \
112 (__sync_synchronize(), *(object))
114 # define atomic_load_explicit(object,order) \
117 # define atomic_exchange(object,desired) \
119 typeof (object) _obj = (object); \
120 typeof (*object) _old; \
122 _old = atomic_load(_obj); \
123 while (!__sync_bool_compare_and_swap(_obj, _old, (desired))); \
127 # define atomic_exchange_explicit(object,desired,order) \
128 atomic_exchange(object,desired)
130 # define atomic_compare_exchange(object,expected,desired) \
132 typeof (object) _exp = (expected); \
133 typeof (*object) _old = *_exp; \
134 *_exp = __sync_val_compare_and_swap((object), _old, (desired)); \
138 # define atomic_compare_exchange_strong(object,expected,desired) \
139 atomic_compare_exchange(object, expected, desired)
141 # define atomic_compare_exchange_strong_explicit(object,expected,desired,order,order_different) \
142 atomic_compare_exchange_strong(object, expected, desired)
144 # define atomic_compare_exchange_weak(object,expected,desired) \
145 atomic_compare_exchange(object, expected, desired)
147 # define atomic_compare_exchange_weak_explicit(object,expected,desired,order_equal,order_different) \
148 atomic_compare_exchange_weak(object, expected, desired)
150 # define atomic_fetch_add(object,operand) \
151 __sync_fetch_and_add(object, operand)
153 # define atomic_fetch_add_explicit(object,operand,order) \
154 atomic_fetch_add(object,operand)
156 # define atomic_fetch_sub(object,operand) \
157 __sync_fetch_and_sub(object, operand)
159 # define atomic_fetch_sub_explicit(object,operand,order) \
160 atomic_fetch_sub(object,operand)
162 # define atomic_fetch_or(object,operand) \
163 __sync_fetch_and_or(object, operand)
165 # define atomic_fetch_or_explicit(object,operand,order) \
166 atomic_fetch_or(object,operand)
168 # define atomic_fetch_xor(object,operand) \
169 __sync_fetch_and_sub(object, operand)
171 # define atomic_fetch_xor_explicit(object,operand,order) \
172 atomic_fetch_sub(object,operand)
174 # define atomic_fetch_and(object,operand) \
175 __sync_fetch_and_and(object, operand)
177 # define atomic_fetch_and_explicit(object,operand,order) \
178 atomic_fetch_and(object,operand)
180 # define atomic_flag_test_and_set(object) \
181 atomic_exchange(object, true)
183 # define atomic_flag_test_and_set_explicit(object,order) \
184 atomic_flag_test_and_set(object)
186 # define atomic_flag_clear(object) \
187 atomic_store(object, false)
189 # define atomic_flag_clear_explicit(object,order) \
190 atomic_flag_clear(object)
194 typedef atomic_uint_least32_t vlc_atomic_float
;
196 static inline void vlc_atomic_init_float(vlc_atomic_float
*var
, float f
)
198 union { float f
; uint32_t i
; } u
;
200 atomic_init(var
, u
.i
);
203 /** Helper to retrieve a single precision from an atom. */
204 static inline float vlc_atomic_load_float(vlc_atomic_float
*atom
)
206 union { float f
; uint32_t i
; } u
;
207 u
.i
= atomic_load(atom
);
211 /** Helper to store a single precision into an atom. */
212 static inline void vlc_atomic_store_float(vlc_atomic_float
*atom
, float f
)
214 union { float f
; uint32_t i
; } u
;
216 atomic_store(atom
, u
.i
);
220 /*** Native C++11 atomics ***/