]>
git.proxmox.com Git - mirror_frr.git/blob - lib/frratomic.h
1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
13 #ifndef FRR_AUTOCONF_ATOMIC
14 #error autoconf checks for atomic functions were not properly run
21 using std::atomic_int
;
22 using std::memory_order
;
23 using std::memory_order_relaxed
;
24 using std::memory_order_acquire
;
25 using std::memory_order_release
;
26 using std::memory_order_acq_rel
;
27 using std::memory_order_consume
;
28 using std::memory_order_seq_cst
;
30 typedef std::atomic
<bool> atomic_bool
;
31 typedef std::atomic
<size_t> atomic_size_t
;
32 typedef std::atomic
<uint_fast32_t> atomic_uint_fast32_t
;
33 typedef std::atomic
<uintptr_t> atomic_uintptr_t
;
35 #elif defined(HAVE_STDATOMIC_H)
36 #include <stdatomic.h>
38 /* These are available in gcc, but not in stdatomic */
39 #define atomic_add_fetch_explicit __atomic_add_fetch
40 #define atomic_sub_fetch_explicit __atomic_sub_fetch
41 #define atomic_and_fetch_explicit __atomic_and_fetch
42 #define atomic_or_fetch_explicit __atomic_or_fetch
44 /* gcc 4.7 and newer */
45 #elif defined(HAVE___ATOMIC)
47 #define _Atomic volatile
48 #define _ATOMIC_WANT_TYPEDEFS
50 #define memory_order_relaxed __ATOMIC_RELAXED
51 #define memory_order_consume __ATOMIC_CONSUME
52 #define memory_order_acquire __ATOMIC_ACQUIRE
53 #define memory_order_release __ATOMIC_RELEASE
54 #define memory_order_acq_rel __ATOMIC_ACQ_REL
55 #define memory_order_seq_cst __ATOMIC_SEQ_CST
57 #define atomic_load_explicit __atomic_load_n
58 #define atomic_store_explicit __atomic_store_n
59 #define atomic_exchange_explicit __atomic_exchange_n
60 #define atomic_fetch_add_explicit __atomic_fetch_add
61 #define atomic_fetch_sub_explicit __atomic_fetch_sub
62 #define atomic_fetch_and_explicit __atomic_fetch_and
63 #define atomic_fetch_or_explicit __atomic_fetch_or
65 #define atomic_add_fetch_explicit __atomic_add_fetch
66 #define atomic_sub_fetch_explicit __atomic_sub_fetch
67 #define atomic_and_fetch_explicit __atomic_and_fetch
68 #define atomic_or_fetch_explicit __atomic_or_fetch
70 #define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
72 __atomic_compare_exchange_n(atom, expect, desire, 1, mem1, mem2)
73 #define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1, \
75 __atomic_compare_exchange_n(atom, expect, desire, 0, mem1, mem2)
78 * clang 3.3 (possibly older)
80 * __sync_swap isn't in gcc's documentation, but clang has it
82 * note __sync_synchronize()
84 #elif defined(HAVE___SYNC)
86 #define _Atomic volatile
87 #define _ATOMIC_WANT_TYPEDEFS
89 #define memory_order_relaxed 0
90 #define memory_order_consume 0
91 #define memory_order_acquire 0
92 #define memory_order_release 0
93 #define memory_order_acq_rel 0
94 #define memory_order_seq_cst 0
96 #define atomic_load_explicit(ptr, mem) \
98 __sync_synchronize(); \
99 typeof(*ptr) rval = __sync_fetch_and_add((ptr), 0); \
100 __sync_synchronize(); \
103 #define atomic_store_explicit(ptr, val, mem) \
105 __sync_synchronize(); \
107 __sync_synchronize(); \
110 #ifdef HAVE___SYNC_SWAP
111 #define atomic_exchange_explicit(ptr, val, mem) \
113 __sync_synchronize(); \
114 typeof(*ptr) rval = __sync_swap((ptr, val), 0); \
115 __sync_synchronize(); \
118 #else /* !HAVE___SYNC_SWAP */
119 #define atomic_exchange_explicit(ptr, val, mem) \
121 typeof(ptr) _ptr = (ptr); \
122 typeof(val) _val = (val); \
123 __sync_synchronize(); \
124 typeof(*ptr) old1, old2 = __sync_fetch_and_add(_ptr, 0); \
127 old2 = __sync_val_compare_and_swap(_ptr, old1, _val); \
128 } while (old1 != old2); \
129 __sync_synchronize(); \
132 #endif /* !HAVE___SYNC_SWAP */
133 #define atomic_fetch_add_explicit(ptr, val, mem) \
135 __sync_synchronize(); \
136 typeof(*ptr) rval = __sync_fetch_and_add((ptr), (val)); \
137 __sync_synchronize(); \
140 #define atomic_fetch_sub_explicit(ptr, val, mem) \
142 __sync_synchronize(); \
143 typeof(*ptr) rval = __sync_fetch_and_sub((ptr), (val)); \
144 __sync_synchronize(); \
148 #define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1, \
151 typeof(atom) _atom = (atom); \
152 typeof(expect) _expect = (expect); \
153 typeof(desire) _desire = (desire); \
154 __sync_synchronize(); \
155 typeof(*atom) rval = \
156 __sync_val_compare_and_swap(_atom, *_expect, _desire); \
157 __sync_synchronize(); \
158 bool ret = (rval == *_expect); \
162 #define atomic_compare_exchange_weak_explicit \
163 atomic_compare_exchange_strong_explicit
165 #define atomic_fetch_and_explicit(ptr, val, mem) \
167 __sync_synchronize(); \
168 typeof(*ptr) rval = __sync_fetch_and_and(ptr, val); \
169 __sync_synchronize(); \
172 #define atomic_fetch_or_explicit(ptr, val, mem) \
174 __sync_synchronize(); \
175 typeof(*ptr) rval = __sync_fetch_and_or(ptr, val); \
176 __sync_synchronize(); \
180 #define atomic_add_fetch_explicit(ptr, val, mem) \
182 __sync_synchronize(); \
183 typeof(*ptr) rval = __sync_add_and_fetch((ptr), (val)); \
184 __sync_synchronize(); \
187 #define atomic_sub_fetch_explicit(ptr, val, mem) \
189 __sync_synchronize(); \
190 typeof(*ptr) rval = __sync_sub_and_fetch((ptr), (val)); \
191 __sync_synchronize(); \
195 #define atomic_and_fetch_explicit(ptr, val, mem) \
197 __sync_synchronize(); \
198 typeof(*ptr) rval = __sync_and_and_fetch(ptr, val); \
199 __sync_synchronize(); \
202 #define atomic_or_fetch_explicit(ptr, val, mem) \
204 __sync_synchronize(); \
205 typeof(*ptr) rval = __sync_or_and_fetch(ptr, val); \
206 __sync_synchronize(); \
210 #else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */
211 #error no atomic functions...
214 #ifdef _ATOMIC_WANT_TYPEDEFS
215 #undef _ATOMIC_WANT_TYPEDEFS
220 typedef _Atomic
bool atomic_bool
;
221 typedef _Atomic
size_t atomic_size_t
;
222 typedef _Atomic
uint_fast32_t atomic_uint_fast32_t
;
223 typedef _Atomic
uintptr_t atomic_uintptr_t
;
226 #endif /* _FRRATOMIC_H */