summaryrefslogtreecommitdiffstats
path: root/lib/frratomic.h
blob: bafc6445e5c6795d94c6efa4940e57094024785f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (c) 2015-16  David Lamparter, for NetDEF, Inc.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _FRRATOMIC_H
#define _FRRATOMIC_H

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifndef FRR_AUTOCONF_ATOMIC
#error autoconf checks for atomic functions were not properly run
#endif

/* ISO C11 */
#ifdef __cplusplus
#include <stdint.h>
#include <atomic>
using std::atomic_int;
using std::memory_order;
using std::memory_order_relaxed;
using std::memory_order_acquire;
using std::memory_order_release;
using std::memory_order_acq_rel;
using std::memory_order_consume;
using std::memory_order_seq_cst;

typedef std::atomic<bool>		atomic_bool;
typedef std::atomic<size_t>		atomic_size_t;
typedef std::atomic<uint_fast32_t>	atomic_uint_fast32_t;
typedef std::atomic<uintptr_t>		atomic_uintptr_t;

#elif defined(HAVE_STDATOMIC_H)
#include <stdatomic.h>

/* These are available in gcc, but not in stdatomic */
#define atomic_add_fetch_explicit __atomic_add_fetch
#define atomic_sub_fetch_explicit __atomic_sub_fetch
#define atomic_and_fetch_explicit __atomic_and_fetch
#define atomic_or_fetch_explicit __atomic_or_fetch

/* gcc 4.7 and newer */
#elif defined(HAVE___ATOMIC)

#define _Atomic volatile
#define _ATOMIC_WANT_TYPEDEFS

#define memory_order_relaxed __ATOMIC_RELAXED
#define memory_order_consume __ATOMIC_CONSUME
#define memory_order_acquire __ATOMIC_ACQUIRE
#define memory_order_release __ATOMIC_RELEASE
#define memory_order_acq_rel __ATOMIC_ACQ_REL
#define memory_order_seq_cst __ATOMIC_SEQ_CST

#define atomic_load_explicit __atomic_load_n
#define atomic_store_explicit __atomic_store_n
#define atomic_exchange_explicit __atomic_exchange_n
#define atomic_fetch_add_explicit __atomic_fetch_add
#define atomic_fetch_sub_explicit __atomic_fetch_sub
#define atomic_fetch_and_explicit __atomic_fetch_and
#define atomic_fetch_or_explicit __atomic_fetch_or

#define atomic_add_fetch_explicit __atomic_add_fetch
#define atomic_sub_fetch_explicit __atomic_sub_fetch
#define atomic_and_fetch_explicit __atomic_and_fetch
#define atomic_or_fetch_explicit __atomic_or_fetch

#define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1,      \
					      mem2)                            \
	__atomic_compare_exchange_n(atom, expect, desire, 1, mem1, mem2)
#define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1,    \
					      mem2)                            \
	__atomic_compare_exchange_n(atom, expect, desire, 0, mem1, mem2)

/* gcc 4.1 and newer,
 * clang 3.3 (possibly older)
 *
 * __sync_swap isn't in gcc's documentation, but clang has it
 *
 * note __sync_synchronize()
 */
#elif defined(HAVE___SYNC)

#define _Atomic volatile
#define _ATOMIC_WANT_TYPEDEFS

#define memory_order_relaxed 0
#define memory_order_consume 0
#define memory_order_acquire 0
#define memory_order_release 0
#define memory_order_acq_rel 0
#define memory_order_seq_cst 0

#define atomic_load_explicit(ptr, mem)                                         \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_fetch_and_add((ptr), 0);            \
		__sync_synchronize();                                          \
		rval;                                                          \
	})
#define atomic_store_explicit(ptr, val, mem)                                   \
	({                                                                     \
		__sync_synchronize();                                          \
		*(ptr) = (val);                                                \
		__sync_synchronize();                                          \
		(void)0;                                                       \
	})
#ifdef HAVE___SYNC_SWAP
#define atomic_exchange_explicit(ptr, val, mem)                                \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_swap((ptr, val), 0);                \
		__sync_synchronize();                                          \
		rval;                                                          \
	})
#else /* !HAVE___SYNC_SWAP */
#define atomic_exchange_explicit(ptr, val, mem)                                \
	({                                                                     \
		typeof(ptr) _ptr = (ptr);                                      \
		typeof(val) _val = (val);                                      \
		__sync_synchronize();                                          \
		typeof(*ptr) old1, old2 = __sync_fetch_and_add(_ptr, 0);       \
		do {                                                           \
			old1 = old2;                                           \
			old2 = __sync_val_compare_and_swap(_ptr, old1, _val);  \
		} while (old1 != old2);                                        \
		__sync_synchronize();                                          \
		old2;                                                          \
	})
#endif /* !HAVE___SYNC_SWAP */
#define atomic_fetch_add_explicit(ptr, val, mem)                               \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_fetch_and_add((ptr), (val));        \
		__sync_synchronize();                                          \
		rval;                                                          \
	})
#define atomic_fetch_sub_explicit(ptr, val, mem)                               \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_fetch_and_sub((ptr), (val));        \
		__sync_synchronize();                                          \
		rval;                                                          \
	})

#define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1,    \
					      mem2)                            \
	({                                                                     \
		typeof(atom) _atom = (atom);                                   \
		typeof(expect) _expect = (expect);                             \
		typeof(desire) _desire = (desire);                             \
		__sync_synchronize();                                          \
		typeof(*atom) rval =                                           \
			__sync_val_compare_and_swap(_atom, *_expect, _desire); \
		__sync_synchronize();                                          \
		bool ret = (rval == *_expect);                                 \
		*_expect = rval;                                               \
		ret;                                                           \
	})
#define atomic_compare_exchange_weak_explicit \
	atomic_compare_exchange_strong_explicit

#define atomic_fetch_and_explicit(ptr, val, mem)                               \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_fetch_and_and(ptr, val);            \
		__sync_synchronize();                                          \
		rval;                                                          \
	})
#define atomic_fetch_or_explicit(ptr, val, mem)                                \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_fetch_and_or(ptr, val);             \
		__sync_synchronize();                                          \
		rval;                                                          \
	})

#define atomic_add_fetch_explicit(ptr, val, mem)                               \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_add_and_fetch((ptr), (val));        \
		__sync_synchronize();                                          \
		rval;                                                          \
	})
#define atomic_sub_fetch_explicit(ptr, val, mem)                               \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_sub_and_fetch((ptr), (val));        \
		__sync_synchronize();                                          \
		rval;                                                          \
	})

#define atomic_and_fetch_explicit(ptr, val, mem)                               \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_and_and_fetch(ptr, val);            \
		__sync_synchronize();                                          \
		rval;                                                          \
	})
#define atomic_or_fetch_explicit(ptr, val, mem)                                \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_or_and_fetch(ptr, val);             \
		__sync_synchronize();                                          \
		rval;                                                          \
	})

#else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */
#error no atomic functions...
#endif

#ifdef _ATOMIC_WANT_TYPEDEFS
#undef _ATOMIC_WANT_TYPEDEFS

#include <stdint.h>
#include <stdbool.h>

typedef _Atomic bool		atomic_bool;
typedef _Atomic size_t		atomic_size_t;
typedef _Atomic uint_fast32_t	atomic_uint_fast32_t;
typedef _Atomic uintptr_t	atomic_uintptr_t;
#endif

#endif /* _FRRATOMIC_H */