9e46a5769c92fc99142adf55989aa0d37581647c
1 #ifndef _URCU_POINTER_STATIC_H
2 #define _URCU_POINTER_STATIC_H
5 * urcu/static/pointer.h
7 * Userspace RCU header. Operations on pointers.
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.
12 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
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.
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.
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
29 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
32 #include <urcu/compiler.h>
33 #include <urcu/arch.h>
34 #include <urcu/system.h>
35 #include <urcu/uatomic.h>
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.
46 * This ensures that the pointer copy is invariant thorough the whole critical
49 * Inserts memory barriers on architectures that require them (currently only
50 * Alpha) and documents which pointers are protected by RCU.
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.
60 * With C standards C11/C++11, concerns about dependency-breaking
61 * optimizations are taken care of by the "memory_order_consume" atomic
64 * Use the gcc __atomic_load() rather than C11/C++11 atomic load
65 * explicit because the pointer used as input argument is a pointer,
66 * not an _Atomic type as required by C11/C++11.
68 * By defining URCU_DEREFERENCE_USE_VOLATILE, the user requires use of
69 * volatile access to implement rcu_dereference rather than
70 * memory_order_consume load from the C11/C++11 standards.
72 * This may improve performance on weakly-ordered architectures where
73 * the compiler implements memory_order_consume as a
74 * memory_order_acquire, which is stricter than required by the
77 * Note that using volatile accesses for rcu_dereference may cause
78 * LTO to generate incorrectly ordered code starting from C11/C++11.
80 * Should match rcu_assign_pointer() or rcu_xchg_pointer().
82 * This macro is less than 10 lines long. The intent is that this macro
83 * meets the 10-line criterion in LGPL, allowing this function to be
84 * expanded directly in non-LGPL code.
87 #if !defined (URCU_DEREFERENCE_USE_VOLATILE) && \
88 ((defined (__cplusplus) && __cplusplus >= 201103L) || \
89 (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L))
90 # define __URCU_DEREFERENCE_USE_ATOMIC_CONSUME
94 * If p is const (the pointer itself, not what it points to), using
95 * __typeof__(p) would declare a const variable, leading to
96 * -Wincompatible-pointer-types errors. Using the statement expression
97 * makes it an rvalue and gets rid of the const-ness.
99 #ifdef __URCU_DEREFERENCE_USE_ATOMIC_CONSUME
100 # define _rcu_dereference(p) __extension__ ({ \
101 __typeof__(__extension__ ({ \
102 __typeof__(p) __attribute__((unused)) _________p0 = { 0 }; \
105 __atomic_load(&(p), &_________p1, __ATOMIC_CONSUME); \
109 # define _rcu_dereference(p) __extension__ ({ \
110 __typeof__(p) _________p1 = CMM_LOAD_SHARED(p); \
111 cmm_smp_read_barrier_depends(); \
117 * _rcu_cmpxchg_pointer - same as rcu_assign_pointer, but tests if the pointer
118 * is as expected by "old". If succeeds, returns the previous pointer to the
119 * data structure, which can be safely freed after waiting for a quiescent state
120 * using synchronize_rcu(). If fails (unexpected value), returns old (which
121 * should not be freed !).
123 * uatomic_cmpxchg() acts as both release and acquire barriers.
125 * This macro is less than 10 lines long. The intent is that this macro
126 * meets the 10-line criterion in LGPL, allowing this function to be
127 * expanded directly in non-LGPL code.
129 #define _rcu_cmpxchg_pointer(p, old, _new) \
132 __typeof__(*p) _________pold = (old); \
133 __typeof__(*p) _________pnew = (_new); \
134 uatomic_cmpxchg(p, _________pold, _________pnew); \
138 * _rcu_xchg_pointer - same as rcu_assign_pointer, but returns the previous
139 * pointer to the data structure, which can be safely freed after waiting for a
140 * quiescent state using synchronize_rcu().
142 * uatomic_xchg() acts as both release and acquire barriers.
144 * This macro is less than 10 lines long. The intent is that this macro
145 * meets the 10-line criterion in LGPL, allowing this function to be
146 * expanded directly in non-LGPL code.
148 #define _rcu_xchg_pointer(p, v) \
151 __typeof__(*p) _________pv = (v); \
152 uatomic_xchg(p, _________pv); \
156 #define _rcu_set_pointer(p, v) \
158 __typeof__(*p) _________pv = (v); \
159 if (!__builtin_constant_p(v) || \
162 uatomic_set(p, _________pv); \
166 * _rcu_assign_pointer - assign (publicize) a pointer to a new data structure
167 * meant to be read by RCU read-side critical sections. Returns the assigned
170 * Documents which pointers will be dereferenced by RCU read-side critical
171 * sections and adds the required memory barriers on architectures requiring
172 * them. It also makes sure the compiler does not reorder code initializing the
173 * data structure before its publication.
175 * Should match rcu_dereference().
177 * This macro is less than 10 lines long. The intent is that this macro
178 * meets the 10-line criterion in LGPL, allowing this function to be
179 * expanded directly in non-LGPL code.
181 #define _rcu_assign_pointer(p, v) _rcu_set_pointer(&(p), v)
187 #endif /* _URCU_POINTER_STATIC_H */
This page took 0.036645 seconds and 4 git commands to generate.