]>
Commit | Line | Data |
---|---|---|
899a4fd2 DL |
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 */ | |
c8a65463 DL |
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) | |
899a4fd2 DL |
46 | #include <stdatomic.h> |
47 | ||
1ed98c2d MS |
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 | ||
899a4fd2 DL |
54 | /* gcc 4.7 and newer */ |
55 | #elif defined(HAVE___ATOMIC) | |
56 | ||
57 | #define _Atomic volatile | |
c8a65463 | 58 | #define _ATOMIC_WANT_TYPEDEFS |
899a4fd2 DL |
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 | |
f765d422 QY |
72 | #define atomic_fetch_and_explicit __atomic_fetch_and |
73 | #define atomic_fetch_or_explicit __atomic_fetch_or | |
899a4fd2 | 74 | |
b086e467 MS |
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 | ||
d62a17ae | 80 | #define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \ |
81 | mem2) \ | |
899a4fd2 | 82 | __atomic_compare_exchange_n(atom, expect, desire, 1, mem1, mem2) |
4cab68a2 DL |
83 | #define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1, \ |
84 | mem2) \ | |
85 | __atomic_compare_exchange_n(atom, expect, desire, 0, mem1, mem2) | |
899a4fd2 DL |
86 | |
87 | /* gcc 4.1 and newer, | |
88 | * clang 3.3 (possibly older) | |
89 | * | |
90 | * __sync_swap isn't in gcc's documentation, but clang has it | |
91 | * | |
d62a17ae | 92 | * note __sync_synchronize() |
899a4fd2 DL |
93 | */ |
94 | #elif defined(HAVE___SYNC) | |
95 | ||
96 | #define _Atomic volatile | |
c8a65463 | 97 | #define _ATOMIC_WANT_TYPEDEFS |
899a4fd2 DL |
98 | |
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 | |
105 | ||
d62a17ae | 106 | #define atomic_load_explicit(ptr, mem) \ |
107 | ({ \ | |
108 | __sync_synchronize(); \ | |
109 | typeof(*ptr) rval = __sync_fetch_and_add((ptr), 0); \ | |
110 | __sync_synchronize(); \ | |
111 | rval; \ | |
112 | }) | |
113 | #define atomic_store_explicit(ptr, val, mem) \ | |
114 | ({ \ | |
115 | __sync_synchronize(); \ | |
116 | *(ptr) = (val); \ | |
117 | __sync_synchronize(); \ | |
118 | (void)0; \ | |
119 | }) | |
899a4fd2 | 120 | #ifdef HAVE___SYNC_SWAP |
d62a17ae | 121 | #define atomic_exchange_explicit(ptr, val, mem) \ |
122 | ({ \ | |
123 | __sync_synchronize(); \ | |
124 | typeof(*ptr) rval = __sync_swap((ptr, val), 0); \ | |
125 | __sync_synchronize(); \ | |
126 | rval; \ | |
127 | }) | |
899a4fd2 | 128 | #else /* !HAVE___SYNC_SWAP */ |
d62a17ae | 129 | #define atomic_exchange_explicit(ptr, val, mem) \ |
130 | ({ \ | |
131 | typeof(ptr) _ptr = (ptr); \ | |
132 | typeof(val) _val = (val); \ | |
133 | __sync_synchronize(); \ | |
134 | typeof(*ptr) old1, old2 = __sync_fetch_and_add(_ptr, 0); \ | |
135 | do { \ | |
136 | old1 = old2; \ | |
137 | old2 = __sync_val_compare_and_swap(_ptr, old1, _val); \ | |
138 | } while (old1 != old2); \ | |
139 | __sync_synchronize(); \ | |
140 | old2; \ | |
899a4fd2 DL |
141 | }) |
142 | #endif /* !HAVE___SYNC_SWAP */ | |
d62a17ae | 143 | #define atomic_fetch_add_explicit(ptr, val, mem) \ |
144 | ({ \ | |
145 | __sync_synchronize(); \ | |
146 | typeof(*ptr) rval = __sync_fetch_and_add((ptr), (val)); \ | |
147 | __sync_synchronize(); \ | |
148 | rval; \ | |
149 | }) | |
150 | #define atomic_fetch_sub_explicit(ptr, val, mem) \ | |
151 | ({ \ | |
152 | __sync_synchronize(); \ | |
153 | typeof(*ptr) rval = __sync_fetch_and_sub((ptr), (val)); \ | |
154 | __sync_synchronize(); \ | |
155 | rval; \ | |
156 | }) | |
157 | ||
4cab68a2 | 158 | #define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1, \ |
d62a17ae | 159 | mem2) \ |
160 | ({ \ | |
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); \ | |
169 | *_expect = rval; \ | |
170 | ret; \ | |
171 | }) | |
4cab68a2 DL |
172 | #define atomic_compare_exchange_weak_explicit \ |
173 | atomic_compare_exchange_strong_explicit | |
b086e467 | 174 | |
f765d422 QY |
175 | #define atomic_fetch_and_explicit(ptr, val, mem) \ |
176 | ({ \ | |
177 | __sync_synchronize(); \ | |
178 | typeof(*ptr) rval = __sync_fetch_and_and(ptr, val); \ | |
179 | __sync_synchronize(); \ | |
180 | rval; \ | |
181 | }) | |
182 | #define atomic_fetch_or_explicit(ptr, val, mem) \ | |
183 | ({ \ | |
184 | __sync_synchronize(); \ | |
185 | typeof(*ptr) rval = __sync_fetch_and_or(ptr, val); \ | |
186 | __sync_synchronize(); \ | |
187 | rval; \ | |
188 | }) | |
899a4fd2 | 189 | |
b086e467 MS |
190 | #define atomic_add_fetch_explicit(ptr, val, mem) \ |
191 | ({ \ | |
192 | __sync_synchronize(); \ | |
193 | typeof(*ptr) rval = __sync_add_and_fetch((ptr), (val)); \ | |
194 | __sync_synchronize(); \ | |
195 | rval; \ | |
196 | }) | |
197 | #define atomic_sub_fetch_explicit(ptr, val, mem) \ | |
198 | ({ \ | |
199 | __sync_synchronize(); \ | |
200 | typeof(*ptr) rval = __sync_sub_and_fetch((ptr), (val)); \ | |
201 | __sync_synchronize(); \ | |
202 | rval; \ | |
203 | }) | |
204 | ||
205 | #define atomic_and_fetch_explicit(ptr, val, mem) \ | |
206 | ({ \ | |
207 | __sync_synchronize(); \ | |
208 | typeof(*ptr) rval = __sync_and_and_fetch(ptr, val); \ | |
209 | __sync_synchronize(); \ | |
210 | rval; \ | |
211 | }) | |
212 | #define atomic_or_fetch_explicit(ptr, val, mem) \ | |
213 | ({ \ | |
214 | __sync_synchronize(); \ | |
215 | typeof(*ptr) rval = __sync_or_and_fetch(ptr, val); \ | |
216 | __sync_synchronize(); \ | |
217 | rval; \ | |
218 | }) | |
219 | ||
899a4fd2 DL |
220 | #else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */ |
221 | #error no atomic functions... | |
222 | #endif | |
223 | ||
c8a65463 DL |
224 | #ifdef _ATOMIC_WANT_TYPEDEFS |
225 | #undef _ATOMIC_WANT_TYPEDEFS | |
226 | ||
227 | #include <stdint.h> | |
228 | #include <stdbool.h> | |
229 | ||
230 | typedef _Atomic bool atomic_bool; | |
231 | typedef _Atomic size_t atomic_size_t; | |
232 | typedef _Atomic uint_fast32_t atomic_uint_fast32_t; | |
233 | #endif | |
234 | ||
899a4fd2 | 235 | #endif /* _FRRATOMIC_H */ |