]>
git.proxmox.com Git - mirror_frr.git/blob - lib/frratomic.h
2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
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.
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.
24 #ifndef FRR_AUTOCONF_ATOMIC
25 #error autoconf checks for atomic functions were not properly run
29 #ifdef HAVE_STDATOMIC_H
30 #include <stdatomic.h>
32 /* gcc 4.7 and newer */
33 #elif defined(HAVE___ATOMIC)
35 #define _Atomic volatile
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
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
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
57 #define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
59 __atomic_compare_exchange_n(atom, expect, desire, 1, mem1, mem2)
62 * clang 3.3 (possibly older)
64 * __sync_swap isn't in gcc's documentation, but clang has it
66 * note __sync_synchronize()
68 #elif defined(HAVE___SYNC)
70 #define _Atomic volatile
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
79 #define atomic_load_explicit(ptr, mem) \
81 __sync_synchronize(); \
82 typeof(*ptr) rval = __sync_fetch_and_add((ptr), 0); \
83 __sync_synchronize(); \
86 #define atomic_store_explicit(ptr, val, mem) \
88 __sync_synchronize(); \
90 __sync_synchronize(); \
93 #ifdef HAVE___SYNC_SWAP
94 #define atomic_exchange_explicit(ptr, val, mem) \
96 __sync_synchronize(); \
97 typeof(*ptr) rval = __sync_swap((ptr, val), 0); \
98 __sync_synchronize(); \
101 #else /* !HAVE___SYNC_SWAP */
102 #define atomic_exchange_explicit(ptr, val, mem) \
104 typeof(ptr) _ptr = (ptr); \
105 typeof(val) _val = (val); \
106 __sync_synchronize(); \
107 typeof(*ptr) old1, old2 = __sync_fetch_and_add(_ptr, 0); \
110 old2 = __sync_val_compare_and_swap(_ptr, old1, _val); \
111 } while (old1 != old2); \
112 __sync_synchronize(); \
115 #endif /* !HAVE___SYNC_SWAP */
116 #define atomic_fetch_add_explicit(ptr, val, mem) \
118 __sync_synchronize(); \
119 typeof(*ptr) rval = __sync_fetch_and_add((ptr), (val)); \
120 __sync_synchronize(); \
123 #define atomic_fetch_sub_explicit(ptr, val, mem) \
125 __sync_synchronize(); \
126 typeof(*ptr) rval = __sync_fetch_and_sub((ptr), (val)); \
127 __sync_synchronize(); \
131 #define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
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); \
146 #define atomic_fetch_and_explicit(ptr, val, mem) \
148 __sync_synchronize(); \
149 typeof(*ptr) rval = __sync_fetch_and_and(ptr, val); \
150 __sync_synchronize(); \
153 #define atomic_fetch_or_explicit(ptr, val, mem) \
155 __sync_synchronize(); \
156 typeof(*ptr) rval = __sync_fetch_and_or(ptr, val); \
157 __sync_synchronize(); \
161 #define atomic_add_fetch_explicit(ptr, val, mem) \
163 __sync_synchronize(); \
164 typeof(*ptr) rval = __sync_add_and_fetch((ptr), (val)); \
165 __sync_synchronize(); \
168 #define atomic_sub_fetch_explicit(ptr, val, mem) \
170 __sync_synchronize(); \
171 typeof(*ptr) rval = __sync_sub_and_fetch((ptr), (val)); \
172 __sync_synchronize(); \
176 #define atomic_and_fetch_explicit(ptr, val, mem) \
178 __sync_synchronize(); \
179 typeof(*ptr) rval = __sync_and_and_fetch(ptr, val); \
180 __sync_synchronize(); \
183 #define atomic_or_fetch_explicit(ptr, val, mem) \
185 __sync_synchronize(); \
186 typeof(*ptr) rval = __sync_or_and_fetch(ptr, val); \
187 __sync_synchronize(); \
191 #else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */
192 #error no atomic functions...
195 #endif /* _FRRATOMIC_H */