urcu/arch/generic: Use atomic builtins if configured
[urcu.git] / include / urcu / static / pointer.h
CommitLineData
d3d3857f
MJ
1// SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2// SPDX-FileCopyrightText: 2009 Paul E. McKenney, IBM Corporation.
3//
4// SPDX-License-Identifier: LGPL-2.1-or-later
5
7e30abe3
MD
6#ifndef _URCU_POINTER_STATIC_H
7#define _URCU_POINTER_STATIC_H
8
9/*
7e30abe3
MD
10 * Userspace RCU header. Operations on pointers.
11 *
a5a9f428
PM
12 * TO BE INCLUDED ONLY IN CODE THAT IS TO BE RECOMPILED ON EACH LIBURCU
13 * RELEASE. See urcu.h for linking dynamically with the userspace rcu library.
7e30abe3 14 *
7e30abe3
MD
15 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
16 */
17
18#include <urcu/compiler.h>
19#include <urcu/arch.h>
20#include <urcu/system.h>
a2e7bf9c 21#include <urcu/uatomic.h>
7e30abe3 22
36bc70a8
MD
23#ifdef __cplusplus
24extern "C" {
67ecffc0 25#endif
36bc70a8 26
7e30abe3
MD
27/**
28 * _rcu_dereference - reads (copy) a RCU-protected pointer to a local variable
29 * into a RCU read-side critical section. The pointer can later be safely
30 * dereferenced within the critical section.
31 *
32 * This ensures that the pointer copy is invariant thorough the whole critical
33 * section.
34 *
35 * Inserts memory barriers on architectures that require them (currently only
36 * Alpha) and documents which pointers are protected by RCU.
37 *
380f4b19
MD
38 * With C standards prior to C11/C++11, the compiler memory barrier in
39 * CMM_LOAD_SHARED() ensures that value-speculative optimizations (e.g.
40 * VSS: Value Speculation Scheduling) does not perform the data read
41 * before the pointer read by speculating the value of the pointer.
42 * Correct ordering is ensured because the pointer is read as a volatile
43 * access. This acts as a global side-effect operation, which forbids
44 * reordering of dependent memory operations.
45 *
46 * With C standards C11/C++11, concerns about dependency-breaking
47 * optimizations are taken care of by the "memory_order_consume" atomic
48 * load.
49 *
3abcdbd1
MD
50 * Use the gcc __atomic_load() rather than C11/C++11 atomic load
51 * explicit because the pointer used as input argument is a pointer,
52 * not an _Atomic type as required by C11/C++11.
53 *
380f4b19
MD
54 * By defining URCU_DEREFERENCE_USE_VOLATILE, the user requires use of
55 * volatile access to implement rcu_dereference rather than
56 * memory_order_consume load from the C11/C++11 standards.
57 *
58 * This may improve performance on weakly-ordered architectures where
59 * the compiler implements memory_order_consume as a
60 * memory_order_acquire, which is stricter than required by the
61 * standard.
62 *
63 * Note that using volatile accesses for rcu_dereference may cause
64 * LTO to generate incorrectly ordered code starting from C11/C++11.
7e30abe3
MD
65 *
66 * Should match rcu_assign_pointer() or rcu_xchg_pointer().
a5a9f428
PM
67 *
68 * This macro is less than 10 lines long. The intent is that this macro
69 * meets the 10-line criterion in LGPL, allowing this function to be
70 * expanded directly in non-LGPL code.
7e30abe3 71 */
380f4b19 72
3abcdbd1
MD
73#if !defined (URCU_DEREFERENCE_USE_VOLATILE) && \
74 ((defined (__cplusplus) && __cplusplus >= 201103L) || \
75 (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L))
76# define __URCU_DEREFERENCE_USE_ATOMIC_CONSUME
380f4b19
MD
77#endif
78
1e41ec3b
SM
79/*
80 * If p is const (the pointer itself, not what it points to), using
81 * __typeof__(p) would declare a const variable, leading to
2d466a63
SM
82 * -Wincompatible-pointer-types errors. Using the statement expression
83 * makes it an rvalue and gets rid of the const-ness.
1e41ec3b 84 */
3abcdbd1
MD
85#ifdef __URCU_DEREFERENCE_USE_ATOMIC_CONSUME
86# define _rcu_dereference(p) __extension__ ({ \
2d466a63 87 __typeof__(__extension__ ({ \
fada682f 88 __typeof__(p) __attribute__((unused)) _________p0 = { 0 }; \
2d466a63
SM
89 _________p0; \
90 })) _________p1; \
3abcdbd1
MD
91 __atomic_load(&(p), &_________p1, __ATOMIC_CONSUME); \
92 (_________p1); \
93 })
94#else
95# define _rcu_dereference(p) __extension__ ({ \
96 __typeof__(p) _________p1 = CMM_LOAD_SHARED(p); \
97 cmm_smp_read_barrier_depends(); \
98 (_________p1); \
99 })
100#endif
7e30abe3
MD
101
102/**
103 * _rcu_cmpxchg_pointer - same as rcu_assign_pointer, but tests if the pointer
104 * is as expected by "old". If succeeds, returns the previous pointer to the
105 * data structure, which can be safely freed after waiting for a quiescent state
106 * using synchronize_rcu(). If fails (unexpected value), returns old (which
107 * should not be freed !).
a5a9f428 108 *
2f10494a
MD
109 * uatomic_cmpxchg() acts as both release and acquire barriers.
110 *
a5a9f428
PM
111 * This macro is less than 10 lines long. The intent is that this macro
112 * meets the 10-line criterion in LGPL, allowing this function to be
113 * expanded directly in non-LGPL code.
7e30abe3 114 */
2b5554c9 115#define _rcu_cmpxchg_pointer(p, old, _new) \
1b85da85 116 __extension__ \
2b5554c9 117 ({ \
bdffa73a
MD
118 __typeof__(*p) _________pold = (old); \
119 __typeof__(*p) _________pnew = (_new); \
bf9de1b7 120 uatomic_cmpxchg(p, _________pold, _________pnew); \
7e30abe3
MD
121 })
122
123/**
124 * _rcu_xchg_pointer - same as rcu_assign_pointer, but returns the previous
125 * pointer to the data structure, which can be safely freed after waiting for a
126 * quiescent state using synchronize_rcu().
a5a9f428 127 *
2f10494a
MD
128 * uatomic_xchg() acts as both release and acquire barriers.
129 *
a5a9f428
PM
130 * This macro is less than 10 lines long. The intent is that this macro
131 * meets the 10-line criterion in LGPL, allowing this function to be
132 * expanded directly in non-LGPL code.
7e30abe3 133 */
7e30abe3 134#define _rcu_xchg_pointer(p, v) \
1b85da85 135 __extension__ \
7e30abe3 136 ({ \
bdffa73a 137 __typeof__(*p) _________pv = (v); \
2b5554c9 138 uatomic_xchg(p, _________pv); \
7e30abe3
MD
139 })
140
141
142#define _rcu_set_pointer(p, v) \
3daae22a 143 do { \
bdffa73a 144 __typeof__(*p) _________pv = (v); \
7e30abe3
MD
145 if (!__builtin_constant_p(v) || \
146 ((v) != NULL)) \
5481ddb3 147 cmm_wmb(); \
bf9de1b7 148 uatomic_set(p, _________pv); \
3daae22a 149 } while (0)
7e30abe3 150
7e30abe3
MD
151/**
152 * _rcu_assign_pointer - assign (publicize) a pointer to a new data structure
153 * meant to be read by RCU read-side critical sections. Returns the assigned
154 * value.
155 *
156 * Documents which pointers will be dereferenced by RCU read-side critical
157 * sections and adds the required memory barriers on architectures requiring
158 * them. It also makes sure the compiler does not reorder code initializing the
159 * data structure before its publication.
160 *
edf8de69 161 * Should match rcu_dereference().
a5a9f428
PM
162 *
163 * This macro is less than 10 lines long. The intent is that this macro
164 * meets the 10-line criterion in LGPL, allowing this function to be
165 * expanded directly in non-LGPL code.
7e30abe3 166 */
7e30abe3
MD
167#define _rcu_assign_pointer(p, v) _rcu_set_pointer(&(p), v)
168
67ecffc0 169#ifdef __cplusplus
36bc70a8
MD
170}
171#endif
172
7e30abe3 173#endif /* _URCU_POINTER_STATIC_H */
This page took 0.055619 seconds and 4 git commands to generate.