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