]> git.proxmox.com Git - mirror_frr.git/blob - lib/frratomic.h
Merge pull request #964 from opensourcerouting/plist-trie-corruption-3.0
[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
50 #define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
51 mem2) \
52 __atomic_compare_exchange_n(atom, expect, desire, 1, mem1, mem2)
53
54 /* gcc 4.1 and newer,
55 * clang 3.3 (possibly older)
56 *
57 * __sync_swap isn't in gcc's documentation, but clang has it
58 *
59 * note __sync_synchronize()
60 */
61 #elif defined(HAVE___SYNC)
62
63 #define _Atomic volatile
64
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
71
72 #define atomic_load_explicit(ptr, mem) \
73 ({ \
74 __sync_synchronize(); \
75 typeof(*ptr) rval = __sync_fetch_and_add((ptr), 0); \
76 __sync_synchronize(); \
77 rval; \
78 })
79 #define atomic_store_explicit(ptr, val, mem) \
80 ({ \
81 __sync_synchronize(); \
82 *(ptr) = (val); \
83 __sync_synchronize(); \
84 (void)0; \
85 })
86 #ifdef HAVE___SYNC_SWAP
87 #define atomic_exchange_explicit(ptr, val, mem) \
88 ({ \
89 __sync_synchronize(); \
90 typeof(*ptr) rval = __sync_swap((ptr, val), 0); \
91 __sync_synchronize(); \
92 rval; \
93 })
94 #else /* !HAVE___SYNC_SWAP */
95 #define atomic_exchange_explicit(ptr, val, mem) \
96 ({ \
97 typeof(ptr) _ptr = (ptr); \
98 typeof(val) _val = (val); \
99 __sync_synchronize(); \
100 typeof(*ptr) old1, old2 = __sync_fetch_and_add(_ptr, 0); \
101 do { \
102 old1 = old2; \
103 old2 = __sync_val_compare_and_swap(_ptr, old1, _val); \
104 } while (old1 != old2); \
105 __sync_synchronize(); \
106 old2; \
107 })
108 #endif /* !HAVE___SYNC_SWAP */
109 #define atomic_fetch_add_explicit(ptr, val, mem) \
110 ({ \
111 __sync_synchronize(); \
112 typeof(*ptr) rval = __sync_fetch_and_add((ptr), (val)); \
113 __sync_synchronize(); \
114 rval; \
115 })
116 #define atomic_fetch_sub_explicit(ptr, val, mem) \
117 ({ \
118 __sync_synchronize(); \
119 typeof(*ptr) rval = __sync_fetch_and_sub((ptr), (val)); \
120 __sync_synchronize(); \
121 rval; \
122 })
123
124 #define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
125 mem2) \
126 ({ \
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); \
135 *_expect = rval; \
136 ret; \
137 })
138
139 #else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */
140 #error no atomic functions...
141 #endif
142
143 #endif /* _FRRATOMIC_H */