]> git.proxmox.com Git - mirror_frr.git/blob - lib/frratomic.h
Merge pull request #1831 from qlyoung/frr-pthread-fixups
[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_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
53 mem2) \
54 __atomic_compare_exchange_n(atom, expect, desire, 1, mem1, mem2)
55
56 /* gcc 4.1 and newer,
57 * clang 3.3 (possibly older)
58 *
59 * __sync_swap isn't in gcc's documentation, but clang has it
60 *
61 * note __sync_synchronize()
62 */
63 #elif defined(HAVE___SYNC)
64
65 #define _Atomic volatile
66
67 #define memory_order_relaxed 0
68 #define memory_order_consume 0
69 #define memory_order_acquire 0
70 #define memory_order_release 0
71 #define memory_order_acq_rel 0
72 #define memory_order_seq_cst 0
73
74 #define atomic_load_explicit(ptr, mem) \
75 ({ \
76 __sync_synchronize(); \
77 typeof(*ptr) rval = __sync_fetch_and_add((ptr), 0); \
78 __sync_synchronize(); \
79 rval; \
80 })
81 #define atomic_store_explicit(ptr, val, mem) \
82 ({ \
83 __sync_synchronize(); \
84 *(ptr) = (val); \
85 __sync_synchronize(); \
86 (void)0; \
87 })
88 #ifdef HAVE___SYNC_SWAP
89 #define atomic_exchange_explicit(ptr, val, mem) \
90 ({ \
91 __sync_synchronize(); \
92 typeof(*ptr) rval = __sync_swap((ptr, val), 0); \
93 __sync_synchronize(); \
94 rval; \
95 })
96 #else /* !HAVE___SYNC_SWAP */
97 #define atomic_exchange_explicit(ptr, val, mem) \
98 ({ \
99 typeof(ptr) _ptr = (ptr); \
100 typeof(val) _val = (val); \
101 __sync_synchronize(); \
102 typeof(*ptr) old1, old2 = __sync_fetch_and_add(_ptr, 0); \
103 do { \
104 old1 = old2; \
105 old2 = __sync_val_compare_and_swap(_ptr, old1, _val); \
106 } while (old1 != old2); \
107 __sync_synchronize(); \
108 old2; \
109 })
110 #endif /* !HAVE___SYNC_SWAP */
111 #define atomic_fetch_add_explicit(ptr, val, mem) \
112 ({ \
113 __sync_synchronize(); \
114 typeof(*ptr) rval = __sync_fetch_and_add((ptr), (val)); \
115 __sync_synchronize(); \
116 rval; \
117 })
118 #define atomic_fetch_sub_explicit(ptr, val, mem) \
119 ({ \
120 __sync_synchronize(); \
121 typeof(*ptr) rval = __sync_fetch_and_sub((ptr), (val)); \
122 __sync_synchronize(); \
123 rval; \
124 })
125
126 #define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
127 mem2) \
128 ({ \
129 typeof(atom) _atom = (atom); \
130 typeof(expect) _expect = (expect); \
131 typeof(desire) _desire = (desire); \
132 __sync_synchronize(); \
133 typeof(*atom) rval = \
134 __sync_val_compare_and_swap(_atom, *_expect, _desire); \
135 __sync_synchronize(); \
136 bool ret = (rval == *_expect); \
137 *_expect = rval; \
138 ret; \
139 })
140 #define atomic_fetch_and_explicit(ptr, val, mem) \
141 ({ \
142 __sync_synchronize(); \
143 typeof(*ptr) rval = __sync_fetch_and_and(ptr, val); \
144 __sync_synchronize(); \
145 rval; \
146 })
147 #define atomic_fetch_or_explicit(ptr, val, mem) \
148 ({ \
149 __sync_synchronize(); \
150 typeof(*ptr) rval = __sync_fetch_and_or(ptr, val); \
151 __sync_synchronize(); \
152 rval; \
153 })
154
155 #else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */
156 #error no atomic functions...
157 #endif
158
159 #endif /* _FRRATOMIC_H */