]> git.proxmox.com Git - mirror_frr.git/blob - lib/frratomic.h
lib: Hide list macros in linklist.c
[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 typedef std::atomic<uintptr_t> atomic_uintptr_t;
45
46 #elif defined(HAVE_STDATOMIC_H)
47 #include <stdatomic.h>
48
49 /* These are available in gcc, but not in stdatomic */
50 #define atomic_add_fetch_explicit __atomic_add_fetch
51 #define atomic_sub_fetch_explicit __atomic_sub_fetch
52 #define atomic_and_fetch_explicit __atomic_and_fetch
53 #define atomic_or_fetch_explicit __atomic_or_fetch
54
55 /* gcc 4.7 and newer */
56 #elif defined(HAVE___ATOMIC)
57
58 #define _Atomic volatile
59 #define _ATOMIC_WANT_TYPEDEFS
60
61 #define memory_order_relaxed __ATOMIC_RELAXED
62 #define memory_order_consume __ATOMIC_CONSUME
63 #define memory_order_acquire __ATOMIC_ACQUIRE
64 #define memory_order_release __ATOMIC_RELEASE
65 #define memory_order_acq_rel __ATOMIC_ACQ_REL
66 #define memory_order_seq_cst __ATOMIC_SEQ_CST
67
68 #define atomic_load_explicit __atomic_load_n
69 #define atomic_store_explicit __atomic_store_n
70 #define atomic_exchange_explicit __atomic_exchange_n
71 #define atomic_fetch_add_explicit __atomic_fetch_add
72 #define atomic_fetch_sub_explicit __atomic_fetch_sub
73 #define atomic_fetch_and_explicit __atomic_fetch_and
74 #define atomic_fetch_or_explicit __atomic_fetch_or
75
76 #define atomic_add_fetch_explicit __atomic_add_fetch
77 #define atomic_sub_fetch_explicit __atomic_sub_fetch
78 #define atomic_and_fetch_explicit __atomic_and_fetch
79 #define atomic_or_fetch_explicit __atomic_or_fetch
80
81 #define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
82 mem2) \
83 __atomic_compare_exchange_n(atom, expect, desire, 1, mem1, mem2)
84 #define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1, \
85 mem2) \
86 __atomic_compare_exchange_n(atom, expect, desire, 0, mem1, mem2)
87
88 /* gcc 4.1 and newer,
89 * clang 3.3 (possibly older)
90 *
91 * __sync_swap isn't in gcc's documentation, but clang has it
92 *
93 * note __sync_synchronize()
94 */
95 #elif defined(HAVE___SYNC)
96
97 #define _Atomic volatile
98 #define _ATOMIC_WANT_TYPEDEFS
99
100 #define memory_order_relaxed 0
101 #define memory_order_consume 0
102 #define memory_order_acquire 0
103 #define memory_order_release 0
104 #define memory_order_acq_rel 0
105 #define memory_order_seq_cst 0
106
107 #define atomic_load_explicit(ptr, mem) \
108 ({ \
109 __sync_synchronize(); \
110 typeof(*ptr) rval = __sync_fetch_and_add((ptr), 0); \
111 __sync_synchronize(); \
112 rval; \
113 })
114 #define atomic_store_explicit(ptr, val, mem) \
115 ({ \
116 __sync_synchronize(); \
117 *(ptr) = (val); \
118 __sync_synchronize(); \
119 (void)0; \
120 })
121 #ifdef HAVE___SYNC_SWAP
122 #define atomic_exchange_explicit(ptr, val, mem) \
123 ({ \
124 __sync_synchronize(); \
125 typeof(*ptr) rval = __sync_swap((ptr, val), 0); \
126 __sync_synchronize(); \
127 rval; \
128 })
129 #else /* !HAVE___SYNC_SWAP */
130 #define atomic_exchange_explicit(ptr, val, mem) \
131 ({ \
132 typeof(ptr) _ptr = (ptr); \
133 typeof(val) _val = (val); \
134 __sync_synchronize(); \
135 typeof(*ptr) old1, old2 = __sync_fetch_and_add(_ptr, 0); \
136 do { \
137 old1 = old2; \
138 old2 = __sync_val_compare_and_swap(_ptr, old1, _val); \
139 } while (old1 != old2); \
140 __sync_synchronize(); \
141 old2; \
142 })
143 #endif /* !HAVE___SYNC_SWAP */
144 #define atomic_fetch_add_explicit(ptr, val, mem) \
145 ({ \
146 __sync_synchronize(); \
147 typeof(*ptr) rval = __sync_fetch_and_add((ptr), (val)); \
148 __sync_synchronize(); \
149 rval; \
150 })
151 #define atomic_fetch_sub_explicit(ptr, val, mem) \
152 ({ \
153 __sync_synchronize(); \
154 typeof(*ptr) rval = __sync_fetch_and_sub((ptr), (val)); \
155 __sync_synchronize(); \
156 rval; \
157 })
158
159 #define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1, \
160 mem2) \
161 ({ \
162 typeof(atom) _atom = (atom); \
163 typeof(expect) _expect = (expect); \
164 typeof(desire) _desire = (desire); \
165 __sync_synchronize(); \
166 typeof(*atom) rval = \
167 __sync_val_compare_and_swap(_atom, *_expect, _desire); \
168 __sync_synchronize(); \
169 bool ret = (rval == *_expect); \
170 *_expect = rval; \
171 ret; \
172 })
173 #define atomic_compare_exchange_weak_explicit \
174 atomic_compare_exchange_strong_explicit
175
176 #define atomic_fetch_and_explicit(ptr, val, mem) \
177 ({ \
178 __sync_synchronize(); \
179 typeof(*ptr) rval = __sync_fetch_and_and(ptr, val); \
180 __sync_synchronize(); \
181 rval; \
182 })
183 #define atomic_fetch_or_explicit(ptr, val, mem) \
184 ({ \
185 __sync_synchronize(); \
186 typeof(*ptr) rval = __sync_fetch_and_or(ptr, val); \
187 __sync_synchronize(); \
188 rval; \
189 })
190
191 #define atomic_add_fetch_explicit(ptr, val, mem) \
192 ({ \
193 __sync_synchronize(); \
194 typeof(*ptr) rval = __sync_add_and_fetch((ptr), (val)); \
195 __sync_synchronize(); \
196 rval; \
197 })
198 #define atomic_sub_fetch_explicit(ptr, val, mem) \
199 ({ \
200 __sync_synchronize(); \
201 typeof(*ptr) rval = __sync_sub_and_fetch((ptr), (val)); \
202 __sync_synchronize(); \
203 rval; \
204 })
205
206 #define atomic_and_fetch_explicit(ptr, val, mem) \
207 ({ \
208 __sync_synchronize(); \
209 typeof(*ptr) rval = __sync_and_and_fetch(ptr, val); \
210 __sync_synchronize(); \
211 rval; \
212 })
213 #define atomic_or_fetch_explicit(ptr, val, mem) \
214 ({ \
215 __sync_synchronize(); \
216 typeof(*ptr) rval = __sync_or_and_fetch(ptr, val); \
217 __sync_synchronize(); \
218 rval; \
219 })
220
221 #else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */
222 #error no atomic functions...
223 #endif
224
225 #ifdef _ATOMIC_WANT_TYPEDEFS
226 #undef _ATOMIC_WANT_TYPEDEFS
227
228 #include <stdint.h>
229 #include <stdbool.h>
230
231 typedef _Atomic bool atomic_bool;
232 typedef _Atomic size_t atomic_size_t;
233 typedef _Atomic uint_fast32_t atomic_uint_fast32_t;
234 typedef _Atomic uintptr_t atomic_uintptr_t;
235 #endif
236
237 #endif /* _FRRATOMIC_H */