]>
git.proxmox.com Git - mirror_frr.git/blob - lib/frratomic.h
2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #ifndef FRR_AUTOCONF_ATOMIC
25 #error autoconf checks for atomic functions were not properly run
32 using std::atomic_int
;
33 using std::memory_order
;
34 using std::memory_order_relaxed
;
35 using std::memory_order_acquire
;
36 using std::memory_order_release
;
37 using std::memory_order_acq_rel
;
38 using std::memory_order_consume
;
39 using std::memory_order_seq_cst
;
41 typedef std::atomic
<bool> atomic_bool
;
42 typedef std::atomic
<size_t> atomic_size_t
;
43 typedef std::atomic
<uint_fast32_t> atomic_uint_fast32_t
;
45 #elif defined(HAVE_STDATOMIC_H)
46 #include <stdatomic.h>
48 /* These are available in gcc, but not in stdatomic */
49 #define atomic_add_fetch_explicit __atomic_add_fetch
50 #define atomic_sub_fetch_explicit __atomic_sub_fetch
51 #define atomic_and_fetch_explicit __atomic_and_fetch
52 #define atomic_or_fetch_explicit __atomic_or_fetch
54 /* gcc 4.7 and newer */
55 #elif defined(HAVE___ATOMIC)
57 #define _Atomic volatile
58 #define _ATOMIC_WANT_TYPEDEFS
60 #define memory_order_relaxed __ATOMIC_RELAXED
61 #define memory_order_consume __ATOMIC_CONSUME
62 #define memory_order_acquire __ATOMIC_ACQUIRE
63 #define memory_order_release __ATOMIC_RELEASE
64 #define memory_order_acq_rel __ATOMIC_ACQ_REL
65 #define memory_order_seq_cst __ATOMIC_SEQ_CST
67 #define atomic_load_explicit __atomic_load_n
68 #define atomic_store_explicit __atomic_store_n
69 #define atomic_exchange_explicit __atomic_exchange_n
70 #define atomic_fetch_add_explicit __atomic_fetch_add
71 #define atomic_fetch_sub_explicit __atomic_fetch_sub
72 #define atomic_fetch_and_explicit __atomic_fetch_and
73 #define atomic_fetch_or_explicit __atomic_fetch_or
75 #define atomic_add_fetch_explicit __atomic_add_fetch
76 #define atomic_sub_fetch_explicit __atomic_sub_fetch
77 #define atomic_and_fetch_explicit __atomic_and_fetch
78 #define atomic_or_fetch_explicit __atomic_or_fetch
80 #define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
82 __atomic_compare_exchange_n(atom, expect, desire, 1, mem1, mem2)
83 #define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1, \
85 __atomic_compare_exchange_n(atom, expect, desire, 0, mem1, mem2)
88 * clang 3.3 (possibly older)
90 * __sync_swap isn't in gcc's documentation, but clang has it
92 * note __sync_synchronize()
94 #elif defined(HAVE___SYNC)
96 #define _Atomic volatile
97 #define _ATOMIC_WANT_TYPEDEFS
99 #define memory_order_relaxed 0
100 #define memory_order_consume 0
101 #define memory_order_acquire 0
102 #define memory_order_release 0
103 #define memory_order_acq_rel 0
104 #define memory_order_seq_cst 0
106 #define atomic_load_explicit(ptr, mem) \
108 __sync_synchronize(); \
109 typeof(*ptr) rval = __sync_fetch_and_add((ptr), 0); \
110 __sync_synchronize(); \
113 #define atomic_store_explicit(ptr, val, mem) \
115 __sync_synchronize(); \
117 __sync_synchronize(); \
120 #ifdef HAVE___SYNC_SWAP
121 #define atomic_exchange_explicit(ptr, val, mem) \
123 __sync_synchronize(); \
124 typeof(*ptr) rval = __sync_swap((ptr, val), 0); \
125 __sync_synchronize(); \
128 #else /* !HAVE___SYNC_SWAP */
129 #define atomic_exchange_explicit(ptr, val, mem) \
131 typeof(ptr) _ptr = (ptr); \
132 typeof(val) _val = (val); \
133 __sync_synchronize(); \
134 typeof(*ptr) old1, old2 = __sync_fetch_and_add(_ptr, 0); \
137 old2 = __sync_val_compare_and_swap(_ptr, old1, _val); \
138 } while (old1 != old2); \
139 __sync_synchronize(); \
142 #endif /* !HAVE___SYNC_SWAP */
143 #define atomic_fetch_add_explicit(ptr, val, mem) \
145 __sync_synchronize(); \
146 typeof(*ptr) rval = __sync_fetch_and_add((ptr), (val)); \
147 __sync_synchronize(); \
150 #define atomic_fetch_sub_explicit(ptr, val, mem) \
152 __sync_synchronize(); \
153 typeof(*ptr) rval = __sync_fetch_and_sub((ptr), (val)); \
154 __sync_synchronize(); \
158 #define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1, \
161 typeof(atom) _atom = (atom); \
162 typeof(expect) _expect = (expect); \
163 typeof(desire) _desire = (desire); \
164 __sync_synchronize(); \
165 typeof(*atom) rval = \
166 __sync_val_compare_and_swap(_atom, *_expect, _desire); \
167 __sync_synchronize(); \
168 bool ret = (rval == *_expect); \
172 #define atomic_compare_exchange_weak_explicit \
173 atomic_compare_exchange_strong_explicit
175 #define atomic_fetch_and_explicit(ptr, val, mem) \
177 __sync_synchronize(); \
178 typeof(*ptr) rval = __sync_fetch_and_and(ptr, val); \
179 __sync_synchronize(); \
182 #define atomic_fetch_or_explicit(ptr, val, mem) \
184 __sync_synchronize(); \
185 typeof(*ptr) rval = __sync_fetch_and_or(ptr, val); \
186 __sync_synchronize(); \
190 #define atomic_add_fetch_explicit(ptr, val, mem) \
192 __sync_synchronize(); \
193 typeof(*ptr) rval = __sync_add_and_fetch((ptr), (val)); \
194 __sync_synchronize(); \
197 #define atomic_sub_fetch_explicit(ptr, val, mem) \
199 __sync_synchronize(); \
200 typeof(*ptr) rval = __sync_sub_and_fetch((ptr), (val)); \
201 __sync_synchronize(); \
205 #define atomic_and_fetch_explicit(ptr, val, mem) \
207 __sync_synchronize(); \
208 typeof(*ptr) rval = __sync_and_and_fetch(ptr, val); \
209 __sync_synchronize(); \
212 #define atomic_or_fetch_explicit(ptr, val, mem) \
214 __sync_synchronize(); \
215 typeof(*ptr) rval = __sync_or_and_fetch(ptr, val); \
216 __sync_synchronize(); \
220 #else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */
221 #error no atomic functions...
224 #ifdef _ATOMIC_WANT_TYPEDEFS
225 #undef _ATOMIC_WANT_TYPEDEFS
230 typedef _Atomic
bool atomic_bool
;
231 typedef _Atomic
size_t atomic_size_t
;
232 typedef _Atomic
uint_fast32_t atomic_uint_fast32_t
;
235 #endif /* _FRRATOMIC_H */