]>
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
50 #define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
52 __atomic_compare_exchange_n(atom, expect, desire, 1, mem1, mem2)
55 * clang 3.3 (possibly older)
57 * __sync_swap isn't in gcc's documentation, but clang has it
59 * note __sync_synchronize()
61 #elif defined(HAVE___SYNC)
63 #define _Atomic volatile
65 #define memory_order_relaxed 0
66 #define memory_order_consume 0
67 #define memory_order_acquire 0
68 #define memory_order_release 0
69 #define memory_order_acq_rel 0
70 #define memory_order_seq_cst 0
72 #define atomic_load_explicit(ptr, mem) \
74 __sync_synchronize(); \
75 typeof(*ptr) rval = __sync_fetch_and_add((ptr), 0); \
76 __sync_synchronize(); \
79 #define atomic_store_explicit(ptr, val, mem) \
81 __sync_synchronize(); \
83 __sync_synchronize(); \
86 #ifdef HAVE___SYNC_SWAP
87 #define atomic_exchange_explicit(ptr, val, mem) \
89 __sync_synchronize(); \
90 typeof(*ptr) rval = __sync_swap((ptr, val), 0); \
91 __sync_synchronize(); \
94 #else /* !HAVE___SYNC_SWAP */
95 #define atomic_exchange_explicit(ptr, val, mem) \
97 typeof(ptr) _ptr = (ptr); \
98 typeof(val) _val = (val); \
99 __sync_synchronize(); \
100 typeof(*ptr) old1, old2 = __sync_fetch_and_add(_ptr, 0); \
103 old2 = __sync_val_compare_and_swap(_ptr, old1, _val); \
104 } while (old1 != old2); \
105 __sync_synchronize(); \
108 #endif /* !HAVE___SYNC_SWAP */
109 #define atomic_fetch_add_explicit(ptr, val, mem) \
111 __sync_synchronize(); \
112 typeof(*ptr) rval = __sync_fetch_and_add((ptr), (val)); \
113 __sync_synchronize(); \
116 #define atomic_fetch_sub_explicit(ptr, val, mem) \
118 __sync_synchronize(); \
119 typeof(*ptr) rval = __sync_fetch_and_sub((ptr), (val)); \
120 __sync_synchronize(); \
124 #define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
127 typeof(atom) _atom = (atom); \
128 typeof(expect) _expect = (expect); \
129 typeof(desire) _desire = (desire); \
130 __sync_synchronize(); \
131 typeof(*atom) rval = \
132 __sync_val_compare_and_swap(_atom, *_expect, _desire); \
133 __sync_synchronize(); \
134 bool ret = (rval == *_expect); \
139 #else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */
140 #error no atomic functions...
143 #endif /* _FRRATOMIC_H */