Commit | Line | Data |
---|---|---|
7e30abe3 MD |
1 | #ifndef _URCU_POINTER_STATIC_H |
2 | #define _URCU_POINTER_STATIC_H | |
3 | ||
4 | /* | |
6cd23d47 | 5 | * urcu/static/pointer.h |
7e30abe3 MD |
6 | * |
7 | * Userspace RCU header. Operations on pointers. | |
8 | * | |
a5a9f428 PM |
9 | * TO BE INCLUDED ONLY IN CODE THAT IS TO BE RECOMPILED ON EACH LIBURCU |
10 | * RELEASE. See urcu.h for linking dynamically with the userspace rcu library. | |
7e30abe3 | 11 | * |
6982d6d7 | 12 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
7e30abe3 MD |
13 | * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. |
14 | * | |
15 | * This library is free software; you can redistribute it and/or | |
16 | * modify it under the terms of the GNU Lesser General Public | |
17 | * License as published by the Free Software Foundation; either | |
18 | * version 2.1 of the License, or (at your option) any later version. | |
19 | * | |
20 | * This library is distributed in the hope that it will be useful, | |
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
23 | * Lesser General Public License for more details. | |
24 | * | |
25 | * You should have received a copy of the GNU Lesser General Public | |
26 | * License along with this library; if not, write to the Free Software | |
27 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
28 | * | |
29 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. | |
30 | */ | |
31 | ||
32 | #include <urcu/compiler.h> | |
33 | #include <urcu/arch.h> | |
34 | #include <urcu/system.h> | |
a2e7bf9c | 35 | #include <urcu/uatomic.h> |
7e30abe3 | 36 | |
36bc70a8 MD |
37 | #ifdef __cplusplus |
38 | extern "C" { | |
67ecffc0 | 39 | #endif |
36bc70a8 | 40 | |
7e30abe3 MD |
41 | /** |
42 | * _rcu_dereference - reads (copy) a RCU-protected pointer to a local variable | |
43 | * into a RCU read-side critical section. The pointer can later be safely | |
44 | * dereferenced within the critical section. | |
45 | * | |
46 | * This ensures that the pointer copy is invariant thorough the whole critical | |
47 | * section. | |
48 | * | |
49 | * Inserts memory barriers on architectures that require them (currently only | |
50 | * Alpha) and documents which pointers are protected by RCU. | |
51 | * | |
58c90a34 MD |
52 | * With C standards prior to C11/C++11, the compiler memory barrier in |
53 | * CMM_LOAD_SHARED() ensures that value-speculative optimizations (e.g. | |
54 | * VSS: Value Speculation Scheduling) does not perform the data read | |
55 | * before the pointer read by speculating the value of the pointer. | |
56 | * Correct ordering is ensured because the pointer is read as a volatile | |
57 | * access. This acts as a global side-effect operation, which forbids | |
58 | * reordering of dependent memory operations. | |
59 | * | |
60 | * With C standards C11/C++11, concerns about dependency-breaking | |
61 | * optimizations are taken care of by the "memory_order_consume" atomic | |
62 | * load. | |
63 | * | |
64 | * By defining URCU_DEREFERENCE_USE_VOLATILE, the user requires use of | |
65 | * volatile access to implement rcu_dereference rather than | |
66 | * memory_order_consume load from the C11/C++11 standards. | |
67 | * | |
68 | * This may improve performance on weakly-ordered architectures where | |
69 | * the compiler implements memory_order_consume as a | |
70 | * memory_order_acquire, which is stricter than required by the | |
71 | * standard. | |
72 | * | |
73 | * Note that using volatile accesses for rcu_dereference may cause | |
74 | * LTO to generate incorrectly ordered code starting from C11/C++11. | |
7e30abe3 MD |
75 | * |
76 | * Should match rcu_assign_pointer() or rcu_xchg_pointer(). | |
a5a9f428 PM |
77 | * |
78 | * This macro is less than 10 lines long. The intent is that this macro | |
79 | * meets the 10-line criterion in LGPL, allowing this function to be | |
80 | * expanded directly in non-LGPL code. | |
7e30abe3 | 81 | */ |
58c90a34 MD |
82 | |
83 | #ifdef URCU_DEREFERENCE_USE_VOLATILE | |
84 | # define __rcu_dereference(p) CMM_LOAD_SHARED(p) | |
85 | #else | |
86 | # if defined (__cplusplus) | |
87 | # if __cplusplus >= 201103L | |
88 | # include <atomic> | |
89 | # define __rcu_dereference(p) ((std::atomic<__typeof__(p)>)(p)).load(std::memory_order_consume) | |
90 | # else | |
91 | # define __rcu_dereference(p) CMM_LOAD_SHARED(x) | |
92 | # endif | |
93 | # else | |
94 | # if (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) | |
95 | # include <stdatomic.h> | |
96 | # define __rcu_dereference(p) atomic_load_explicit(&(p), memory_order_consume) | |
97 | # else | |
98 | # define __rcu_dereference(p) CMM_LOAD_SHARED(p) | |
99 | # endif | |
100 | # endif | |
101 | #endif | |
102 | ||
1b85da85 LB |
103 | #define _rcu_dereference(p) \ |
104 | __extension__ \ | |
105 | ({ \ | |
58c90a34 | 106 | __typeof__(p) _________p1 = __rcu_dereference(p); \ |
5481ddb3 | 107 | cmm_smp_read_barrier_depends(); \ |
7e30abe3 MD |
108 | (_________p1); \ |
109 | }) | |
110 | ||
111 | /** | |
112 | * _rcu_cmpxchg_pointer - same as rcu_assign_pointer, but tests if the pointer | |
113 | * is as expected by "old". If succeeds, returns the previous pointer to the | |
114 | * data structure, which can be safely freed after waiting for a quiescent state | |
115 | * using synchronize_rcu(). If fails (unexpected value), returns old (which | |
116 | * should not be freed !). | |
a5a9f428 | 117 | * |
2f10494a MD |
118 | * uatomic_cmpxchg() acts as both release and acquire barriers. |
119 | * | |
a5a9f428 PM |
120 | * This macro is less than 10 lines long. The intent is that this macro |
121 | * meets the 10-line criterion in LGPL, allowing this function to be | |
122 | * expanded directly in non-LGPL code. | |
7e30abe3 | 123 | */ |
2b5554c9 | 124 | #define _rcu_cmpxchg_pointer(p, old, _new) \ |
1b85da85 | 125 | __extension__ \ |
2b5554c9 | 126 | ({ \ |
bdffa73a MD |
127 | __typeof__(*p) _________pold = (old); \ |
128 | __typeof__(*p) _________pnew = (_new); \ | |
bf9de1b7 | 129 | uatomic_cmpxchg(p, _________pold, _________pnew); \ |
7e30abe3 MD |
130 | }) |
131 | ||
132 | /** | |
133 | * _rcu_xchg_pointer - same as rcu_assign_pointer, but returns the previous | |
134 | * pointer to the data structure, which can be safely freed after waiting for a | |
135 | * quiescent state using synchronize_rcu(). | |
a5a9f428 | 136 | * |
2f10494a MD |
137 | * uatomic_xchg() acts as both release and acquire barriers. |
138 | * | |
a5a9f428 PM |
139 | * This macro is less than 10 lines long. The intent is that this macro |
140 | * meets the 10-line criterion in LGPL, allowing this function to be | |
141 | * expanded directly in non-LGPL code. | |
7e30abe3 | 142 | */ |
7e30abe3 | 143 | #define _rcu_xchg_pointer(p, v) \ |
1b85da85 | 144 | __extension__ \ |
7e30abe3 | 145 | ({ \ |
bdffa73a | 146 | __typeof__(*p) _________pv = (v); \ |
2b5554c9 | 147 | uatomic_xchg(p, _________pv); \ |
7e30abe3 MD |
148 | }) |
149 | ||
150 | ||
151 | #define _rcu_set_pointer(p, v) \ | |
3daae22a | 152 | do { \ |
bdffa73a | 153 | __typeof__(*p) _________pv = (v); \ |
7e30abe3 MD |
154 | if (!__builtin_constant_p(v) || \ |
155 | ((v) != NULL)) \ | |
5481ddb3 | 156 | cmm_wmb(); \ |
bf9de1b7 | 157 | uatomic_set(p, _________pv); \ |
3daae22a | 158 | } while (0) |
7e30abe3 | 159 | |
7e30abe3 MD |
160 | /** |
161 | * _rcu_assign_pointer - assign (publicize) a pointer to a new data structure | |
162 | * meant to be read by RCU read-side critical sections. Returns the assigned | |
163 | * value. | |
164 | * | |
165 | * Documents which pointers will be dereferenced by RCU read-side critical | |
166 | * sections and adds the required memory barriers on architectures requiring | |
167 | * them. It also makes sure the compiler does not reorder code initializing the | |
168 | * data structure before its publication. | |
169 | * | |
edf8de69 | 170 | * Should match rcu_dereference(). |
a5a9f428 PM |
171 | * |
172 | * This macro is less than 10 lines long. The intent is that this macro | |
173 | * meets the 10-line criterion in LGPL, allowing this function to be | |
174 | * expanded directly in non-LGPL code. | |
7e30abe3 | 175 | */ |
7e30abe3 MD |
176 | #define _rcu_assign_pointer(p, v) _rcu_set_pointer(&(p), v) |
177 | ||
67ecffc0 | 178 | #ifdef __cplusplus |
36bc70a8 MD |
179 | } |
180 | #endif | |
181 | ||
7e30abe3 | 182 | #endif /* _URCU_POINTER_STATIC_H */ |