]> git.proxmox.com Git - mirror_frr.git/blob - lib/frratomic.h
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / lib / frratomic.h
1 // SPDX-License-Identifier: ISC
2 /*
3 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
4 */
5
6 #ifndef _FRRATOMIC_H
7 #define _FRRATOMIC_H
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #ifndef FRR_AUTOCONF_ATOMIC
14 #error autoconf checks for atomic functions were not properly run
15 #endif
16
17 /* ISO C11 */
18 #ifdef __cplusplus
19 #include <stdint.h>
20 #include <atomic>
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;
29
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;
34
35 #elif defined(HAVE_STDATOMIC_H)
36 #include <stdatomic.h>
37
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
43
44 /* gcc 4.7 and newer */
45 #elif defined(HAVE___ATOMIC)
46
47 #define _Atomic volatile
48 #define _ATOMIC_WANT_TYPEDEFS
49
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
56
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
64
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
69
70 #define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
71 mem2) \
72 __atomic_compare_exchange_n(atom, expect, desire, 1, mem1, mem2)
73 #define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1, \
74 mem2) \
75 __atomic_compare_exchange_n(atom, expect, desire, 0, mem1, mem2)
76
77 /* gcc 4.1 and newer,
78 * clang 3.3 (possibly older)
79 *
80 * __sync_swap isn't in gcc's documentation, but clang has it
81 *
82 * note __sync_synchronize()
83 */
84 #elif defined(HAVE___SYNC)
85
86 #define _Atomic volatile
87 #define _ATOMIC_WANT_TYPEDEFS
88
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
95
96 #define atomic_load_explicit(ptr, mem) \
97 ({ \
98 __sync_synchronize(); \
99 typeof(*ptr) rval = __sync_fetch_and_add((ptr), 0); \
100 __sync_synchronize(); \
101 rval; \
102 })
103 #define atomic_store_explicit(ptr, val, mem) \
104 ({ \
105 __sync_synchronize(); \
106 *(ptr) = (val); \
107 __sync_synchronize(); \
108 (void)0; \
109 })
110 #ifdef HAVE___SYNC_SWAP
111 #define atomic_exchange_explicit(ptr, val, mem) \
112 ({ \
113 __sync_synchronize(); \
114 typeof(*ptr) rval = __sync_swap((ptr, val), 0); \
115 __sync_synchronize(); \
116 rval; \
117 })
118 #else /* !HAVE___SYNC_SWAP */
119 #define atomic_exchange_explicit(ptr, val, mem) \
120 ({ \
121 typeof(ptr) _ptr = (ptr); \
122 typeof(val) _val = (val); \
123 __sync_synchronize(); \
124 typeof(*ptr) old1, old2 = __sync_fetch_and_add(_ptr, 0); \
125 do { \
126 old1 = old2; \
127 old2 = __sync_val_compare_and_swap(_ptr, old1, _val); \
128 } while (old1 != old2); \
129 __sync_synchronize(); \
130 old2; \
131 })
132 #endif /* !HAVE___SYNC_SWAP */
133 #define atomic_fetch_add_explicit(ptr, val, mem) \
134 ({ \
135 __sync_synchronize(); \
136 typeof(*ptr) rval = __sync_fetch_and_add((ptr), (val)); \
137 __sync_synchronize(); \
138 rval; \
139 })
140 #define atomic_fetch_sub_explicit(ptr, val, mem) \
141 ({ \
142 __sync_synchronize(); \
143 typeof(*ptr) rval = __sync_fetch_and_sub((ptr), (val)); \
144 __sync_synchronize(); \
145 rval; \
146 })
147
148 #define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1, \
149 mem2) \
150 ({ \
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); \
159 *_expect = rval; \
160 ret; \
161 })
162 #define atomic_compare_exchange_weak_explicit \
163 atomic_compare_exchange_strong_explicit
164
165 #define atomic_fetch_and_explicit(ptr, val, mem) \
166 ({ \
167 __sync_synchronize(); \
168 typeof(*ptr) rval = __sync_fetch_and_and(ptr, val); \
169 __sync_synchronize(); \
170 rval; \
171 })
172 #define atomic_fetch_or_explicit(ptr, val, mem) \
173 ({ \
174 __sync_synchronize(); \
175 typeof(*ptr) rval = __sync_fetch_and_or(ptr, val); \
176 __sync_synchronize(); \
177 rval; \
178 })
179
180 #define atomic_add_fetch_explicit(ptr, val, mem) \
181 ({ \
182 __sync_synchronize(); \
183 typeof(*ptr) rval = __sync_add_and_fetch((ptr), (val)); \
184 __sync_synchronize(); \
185 rval; \
186 })
187 #define atomic_sub_fetch_explicit(ptr, val, mem) \
188 ({ \
189 __sync_synchronize(); \
190 typeof(*ptr) rval = __sync_sub_and_fetch((ptr), (val)); \
191 __sync_synchronize(); \
192 rval; \
193 })
194
195 #define atomic_and_fetch_explicit(ptr, val, mem) \
196 ({ \
197 __sync_synchronize(); \
198 typeof(*ptr) rval = __sync_and_and_fetch(ptr, val); \
199 __sync_synchronize(); \
200 rval; \
201 })
202 #define atomic_or_fetch_explicit(ptr, val, mem) \
203 ({ \
204 __sync_synchronize(); \
205 typeof(*ptr) rval = __sync_or_and_fetch(ptr, val); \
206 __sync_synchronize(); \
207 rval; \
208 })
209
210 #else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */
211 #error no atomic functions...
212 #endif
213
214 #ifdef _ATOMIC_WANT_TYPEDEFS
215 #undef _ATOMIC_WANT_TYPEDEFS
216
217 #include <stdint.h>
218 #include <stdbool.h>
219
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;
224 #endif
225
226 #endif /* _FRRATOMIC_H */