]> git.proxmox.com Git - mirror_frr.git/blob - lib/frratomic.h
Merge pull request #4165 from dslicenc/rnh-invalid-nexthops
[mirror_frr.git] / lib / frratomic.h
1 /*
2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
3 *
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.
7 *
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.
15 */
16
17 #ifndef _FRRATOMIC_H
18 #define _FRRATOMIC_H
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #ifndef FRR_AUTOCONF_ATOMIC
25 #error autoconf checks for atomic functions were not properly run
26 #endif
27
28 /* ISO C11 */
29 #ifdef __cplusplus
30 #include <stdint.h>
31 #include <atomic>
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;
40
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;
44
45 #elif defined(HAVE_STDATOMIC_H)
46 #include <stdatomic.h>
47
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
53
54 /* gcc 4.7 and newer */
55 #elif defined(HAVE___ATOMIC)
56
57 #define _Atomic volatile
58 #define _ATOMIC_WANT_TYPEDEFS
59
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
66
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
74
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
79
80 #define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
81 mem2) \
82 __atomic_compare_exchange_n(atom, expect, desire, 1, mem1, mem2)
83
84 /* gcc 4.1 and newer,
85 * clang 3.3 (possibly older)
86 *
87 * __sync_swap isn't in gcc's documentation, but clang has it
88 *
89 * note __sync_synchronize()
90 */
91 #elif defined(HAVE___SYNC)
92
93 #define _Atomic volatile
94 #define _ATOMIC_WANT_TYPEDEFS
95
96 #define memory_order_relaxed 0
97 #define memory_order_consume 0
98 #define memory_order_acquire 0
99 #define memory_order_release 0
100 #define memory_order_acq_rel 0
101 #define memory_order_seq_cst 0
102
103 #define atomic_load_explicit(ptr, mem) \
104 ({ \
105 __sync_synchronize(); \
106 typeof(*ptr) rval = __sync_fetch_and_add((ptr), 0); \
107 __sync_synchronize(); \
108 rval; \
109 })
110 #define atomic_store_explicit(ptr, val, mem) \
111 ({ \
112 __sync_synchronize(); \
113 *(ptr) = (val); \
114 __sync_synchronize(); \
115 (void)0; \
116 })
117 #ifdef HAVE___SYNC_SWAP
118 #define atomic_exchange_explicit(ptr, val, mem) \
119 ({ \
120 __sync_synchronize(); \
121 typeof(*ptr) rval = __sync_swap((ptr, val), 0); \
122 __sync_synchronize(); \
123 rval; \
124 })
125 #else /* !HAVE___SYNC_SWAP */
126 #define atomic_exchange_explicit(ptr, val, mem) \
127 ({ \
128 typeof(ptr) _ptr = (ptr); \
129 typeof(val) _val = (val); \
130 __sync_synchronize(); \
131 typeof(*ptr) old1, old2 = __sync_fetch_and_add(_ptr, 0); \
132 do { \
133 old1 = old2; \
134 old2 = __sync_val_compare_and_swap(_ptr, old1, _val); \
135 } while (old1 != old2); \
136 __sync_synchronize(); \
137 old2; \
138 })
139 #endif /* !HAVE___SYNC_SWAP */
140 #define atomic_fetch_add_explicit(ptr, val, mem) \
141 ({ \
142 __sync_synchronize(); \
143 typeof(*ptr) rval = __sync_fetch_and_add((ptr), (val)); \
144 __sync_synchronize(); \
145 rval; \
146 })
147 #define atomic_fetch_sub_explicit(ptr, val, mem) \
148 ({ \
149 __sync_synchronize(); \
150 typeof(*ptr) rval = __sync_fetch_and_sub((ptr), (val)); \
151 __sync_synchronize(); \
152 rval; \
153 })
154
155 #define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
156 mem2) \
157 ({ \
158 typeof(atom) _atom = (atom); \
159 typeof(expect) _expect = (expect); \
160 typeof(desire) _desire = (desire); \
161 __sync_synchronize(); \
162 typeof(*atom) rval = \
163 __sync_val_compare_and_swap(_atom, *_expect, _desire); \
164 __sync_synchronize(); \
165 bool ret = (rval == *_expect); \
166 *_expect = rval; \
167 ret; \
168 })
169
170 #define atomic_fetch_and_explicit(ptr, val, mem) \
171 ({ \
172 __sync_synchronize(); \
173 typeof(*ptr) rval = __sync_fetch_and_and(ptr, val); \
174 __sync_synchronize(); \
175 rval; \
176 })
177 #define atomic_fetch_or_explicit(ptr, val, mem) \
178 ({ \
179 __sync_synchronize(); \
180 typeof(*ptr) rval = __sync_fetch_and_or(ptr, val); \
181 __sync_synchronize(); \
182 rval; \
183 })
184
185 #define atomic_add_fetch_explicit(ptr, val, mem) \
186 ({ \
187 __sync_synchronize(); \
188 typeof(*ptr) rval = __sync_add_and_fetch((ptr), (val)); \
189 __sync_synchronize(); \
190 rval; \
191 })
192 #define atomic_sub_fetch_explicit(ptr, val, mem) \
193 ({ \
194 __sync_synchronize(); \
195 typeof(*ptr) rval = __sync_sub_and_fetch((ptr), (val)); \
196 __sync_synchronize(); \
197 rval; \
198 })
199
200 #define atomic_and_fetch_explicit(ptr, val, mem) \
201 ({ \
202 __sync_synchronize(); \
203 typeof(*ptr) rval = __sync_and_and_fetch(ptr, val); \
204 __sync_synchronize(); \
205 rval; \
206 })
207 #define atomic_or_fetch_explicit(ptr, val, mem) \
208 ({ \
209 __sync_synchronize(); \
210 typeof(*ptr) rval = __sync_or_and_fetch(ptr, val); \
211 __sync_synchronize(); \
212 rval; \
213 })
214
215 #else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */
216 #error no atomic functions...
217 #endif
218
219 #ifdef _ATOMIC_WANT_TYPEDEFS
220 #undef _ATOMIC_WANT_TYPEDEFS
221
222 #include <stdint.h>
223 #include <stdbool.h>
224
225 typedef _Atomic bool atomic_bool;
226 typedef _Atomic size_t atomic_size_t;
227 typedef _Atomic uint_fast32_t atomic_uint_fast32_t;
228 #endif
229
230 #endif /* _FRRATOMIC_H */