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