uatomic/x86: Remove redundant memory barriers
[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 */
d1854484
OD
85# define _rcu_dereference(p) \
86 uatomic_load(&(p), CMM_CONSUME)
7e30abe3
MD
87/**
88 * _rcu_cmpxchg_pointer - same as rcu_assign_pointer, but tests if the pointer
89 * is as expected by "old". If succeeds, returns the previous pointer to the
90 * data structure, which can be safely freed after waiting for a quiescent state
91 * using synchronize_rcu(). If fails (unexpected value), returns old (which
92 * should not be freed !).
a5a9f428 93 *
d1854484 94 * uatomic_cmpxchg() acts as both release and acquire barriers on success.
2f10494a 95 *
a5a9f428
PM
96 * This macro is less than 10 lines long. The intent is that this macro
97 * meets the 10-line criterion in LGPL, allowing this function to be
98 * expanded directly in non-LGPL code.
7e30abe3 99 */
2b5554c9 100#define _rcu_cmpxchg_pointer(p, old, _new) \
1b85da85 101 __extension__ \
2b5554c9 102 ({ \
bdffa73a
MD
103 __typeof__(*p) _________pold = (old); \
104 __typeof__(*p) _________pnew = (_new); \
d1854484
OD
105 uatomic_cmpxchg_mo(p, _________pold, _________pnew, \
106 CMM_SEQ_CST, CMM_RELAXED); \
107 });
7e30abe3
MD
108
109/**
110 * _rcu_xchg_pointer - same as rcu_assign_pointer, but returns the previous
111 * pointer to the data structure, which can be safely freed after waiting for a
112 * quiescent state using synchronize_rcu().
a5a9f428 113 *
2f10494a
MD
114 * uatomic_xchg() acts as both release and acquire barriers.
115 *
a5a9f428
PM
116 * This macro is less than 10 lines long. The intent is that this macro
117 * meets the 10-line criterion in LGPL, allowing this function to be
118 * expanded directly in non-LGPL code.
7e30abe3 119 */
7e30abe3 120#define _rcu_xchg_pointer(p, v) \
1b85da85 121 __extension__ \
7e30abe3 122 ({ \
bdffa73a 123 __typeof__(*p) _________pv = (v); \
d1854484
OD
124 uatomic_xchg_mo(p, _________pv, \
125 CMM_SEQ_CST); \
7e30abe3
MD
126 })
127
128
d1854484
OD
129#define _rcu_set_pointer(p, v) \
130 do { \
131 __typeof__(*p) _________pv = (v); \
132 uatomic_store(p, _________pv, \
133 __builtin_constant_p(v) && (v) == NULL ? \
134 CMM_RELAXED : CMM_RELEASE); \
3daae22a 135 } while (0)
7e30abe3 136
7e30abe3
MD
137/**
138 * _rcu_assign_pointer - assign (publicize) a pointer to a new data structure
139 * meant to be read by RCU read-side critical sections. Returns the assigned
140 * value.
141 *
142 * Documents which pointers will be dereferenced by RCU read-side critical
143 * sections and adds the required memory barriers on architectures requiring
144 * them. It also makes sure the compiler does not reorder code initializing the
145 * data structure before its publication.
146 *
edf8de69 147 * Should match rcu_dereference().
a5a9f428
PM
148 *
149 * This macro is less than 10 lines long. The intent is that this macro
150 * meets the 10-line criterion in LGPL, allowing this function to be
151 * expanded directly in non-LGPL code.
7e30abe3 152 */
7e30abe3
MD
153#define _rcu_assign_pointer(p, v) _rcu_set_pointer(&(p), v)
154
67ecffc0 155#ifdef __cplusplus
36bc70a8
MD
156}
157#endif
158
7e30abe3 159#endif /* _URCU_POINTER_STATIC_H */
This page took 0.058146 seconds and 5 git commands to generate.