Fix: add missing destroy functions to queues/stack APIs
[urcu.git] / urcu / static / urcu-pointer.h
CommitLineData
7e30abe3
MD
1#ifndef _URCU_POINTER_STATIC_H
2#define _URCU_POINTER_STATIC_H
3
4/*
5 * urcu-pointer-static.h
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
38extern "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 *
6cf3827c 52 * The compiler memory barrier in CMM_LOAD_SHARED() ensures that value-speculative
7e30abe3
MD
53 * optimizations (e.g. VSS: Value Speculation Scheduling) does not perform the
54 * data read before the pointer read by speculating the value of the pointer.
55 * Correct ordering is ensured because the pointer is read as a volatile access.
56 * This acts as a global side-effect operation, which forbids reordering of
57 * dependent memory operations. Note that such concern about dependency-breaking
58 * optimizations will eventually be taken care of by the "memory_order_consume"
59 * addition to forthcoming C++ standard.
60 *
61 * Should match rcu_assign_pointer() or rcu_xchg_pointer().
a5a9f428
PM
62 *
63 * This macro is less than 10 lines long. The intent is that this macro
64 * meets the 10-line criterion in LGPL, allowing this function to be
65 * expanded directly in non-LGPL code.
7e30abe3 66 */
1b85da85
LB
67#define _rcu_dereference(p) \
68 __extension__ \
69 ({ \
bdffa73a 70 __typeof__(p) _________p1 = CMM_LOAD_SHARED(p); \
5481ddb3 71 cmm_smp_read_barrier_depends(); \
7e30abe3
MD
72 (_________p1); \
73 })
74
75/**
76 * _rcu_cmpxchg_pointer - same as rcu_assign_pointer, but tests if the pointer
77 * is as expected by "old". If succeeds, returns the previous pointer to the
78 * data structure, which can be safely freed after waiting for a quiescent state
79 * using synchronize_rcu(). If fails (unexpected value), returns old (which
80 * should not be freed !).
a5a9f428
PM
81 *
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.
7e30abe3 85 */
2b5554c9 86#define _rcu_cmpxchg_pointer(p, old, _new) \
1b85da85 87 __extension__ \
2b5554c9 88 ({ \
bdffa73a
MD
89 __typeof__(*p) _________pold = (old); \
90 __typeof__(*p) _________pnew = (_new); \
2b5554c9
MD
91 if (!__builtin_constant_p(_new) || \
92 ((_new) != NULL)) \
5481ddb3 93 cmm_wmb(); \
bf9de1b7 94 uatomic_cmpxchg(p, _________pold, _________pnew); \
7e30abe3
MD
95 })
96
97/**
98 * _rcu_xchg_pointer - same as rcu_assign_pointer, but returns the previous
99 * pointer to the data structure, which can be safely freed after waiting for a
100 * quiescent state using synchronize_rcu().
a5a9f428
PM
101 *
102 * This macro is less than 10 lines long. The intent is that this macro
103 * meets the 10-line criterion in LGPL, allowing this function to be
104 * expanded directly in non-LGPL code.
7e30abe3 105 */
7e30abe3 106#define _rcu_xchg_pointer(p, v) \
1b85da85 107 __extension__ \
7e30abe3 108 ({ \
bdffa73a 109 __typeof__(*p) _________pv = (v); \
7e30abe3
MD
110 if (!__builtin_constant_p(v) || \
111 ((v) != NULL)) \
5481ddb3 112 cmm_wmb(); \
2b5554c9 113 uatomic_xchg(p, _________pv); \
7e30abe3
MD
114 })
115
116
117#define _rcu_set_pointer(p, v) \
3daae22a 118 do { \
bdffa73a 119 __typeof__(*p) _________pv = (v); \
7e30abe3
MD
120 if (!__builtin_constant_p(v) || \
121 ((v) != NULL)) \
5481ddb3 122 cmm_wmb(); \
bf9de1b7 123 uatomic_set(p, _________pv); \
3daae22a 124 } while (0)
7e30abe3 125
7e30abe3
MD
126/**
127 * _rcu_assign_pointer - assign (publicize) a pointer to a new data structure
128 * meant to be read by RCU read-side critical sections. Returns the assigned
129 * value.
130 *
131 * Documents which pointers will be dereferenced by RCU read-side critical
132 * sections and adds the required memory barriers on architectures requiring
133 * them. It also makes sure the compiler does not reorder code initializing the
134 * data structure before its publication.
135 *
edf8de69 136 * Should match rcu_dereference().
a5a9f428
PM
137 *
138 * This macro is less than 10 lines long. The intent is that this macro
139 * meets the 10-line criterion in LGPL, allowing this function to be
140 * expanded directly in non-LGPL code.
7e30abe3 141 */
7e30abe3
MD
142#define _rcu_assign_pointer(p, v) _rcu_set_pointer(&(p), v)
143
67ecffc0 144#ifdef __cplusplus
36bc70a8
MD
145}
146#endif
147
7e30abe3 148#endif /* _URCU_POINTER_STATIC_H */
This page took 0.049816 seconds and 4 git commands to generate.