Update 386 cmpxchg support
[urcu.git] / urcu-pointer-static.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 *
9 * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See urcu-pointer.h for
10 * linking dynamically with the userspace rcu library.
11 *
12 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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>
48d848c7 35#include <urcu/uatomic_arch.h>
7e30abe3
MD
36
37/**
38 * _rcu_dereference - reads (copy) a RCU-protected pointer to a local variable
39 * into a RCU read-side critical section. The pointer can later be safely
40 * dereferenced within the critical section.
41 *
42 * This ensures that the pointer copy is invariant thorough the whole critical
43 * section.
44 *
45 * Inserts memory barriers on architectures that require them (currently only
46 * Alpha) and documents which pointers are protected by RCU.
47 *
48 * The compiler memory barrier in LOAD_SHARED() ensures that value-speculative
49 * optimizations (e.g. VSS: Value Speculation Scheduling) does not perform the
50 * data read before the pointer read by speculating the value of the pointer.
51 * Correct ordering is ensured because the pointer is read as a volatile access.
52 * This acts as a global side-effect operation, which forbids reordering of
53 * dependent memory operations. Note that such concern about dependency-breaking
54 * optimizations will eventually be taken care of by the "memory_order_consume"
55 * addition to forthcoming C++ standard.
56 *
57 * Should match rcu_assign_pointer() or rcu_xchg_pointer().
58 */
59
60#define _rcu_dereference(p) ({ \
61 typeof(p) _________p1 = LOAD_SHARED(p); \
62 smp_read_barrier_depends(); \
63 (_________p1); \
64 })
65
66/**
67 * _rcu_cmpxchg_pointer - same as rcu_assign_pointer, but tests if the pointer
68 * is as expected by "old". If succeeds, returns the previous pointer to the
69 * data structure, which can be safely freed after waiting for a quiescent state
70 * using synchronize_rcu(). If fails (unexpected value), returns old (which
71 * should not be freed !).
72 */
73
2b5554c9
MD
74#define _rcu_cmpxchg_pointer(p, old, _new) \
75 ({ \
76 typeof(*p) _________pold = (old); \
77 typeof(*p) _________pnew = (_new); \
78 if (!__builtin_constant_p(_new) || \
79 ((_new) != NULL)) \
80 wmb(); \
e1562850
MD
81 (likely(URCU_CAS_AVAIL()) ? \
82 (uatomic_cmpxchg(p, _________pold, _________pnew)) : \
83 (compat_uatomic_cmpxchg(p, _________pold, \
84 _________pnew))) \
7e30abe3
MD
85 })
86
87/**
88 * _rcu_xchg_pointer - same as rcu_assign_pointer, but returns the previous
89 * pointer to the data structure, which can be safely freed after waiting for a
90 * quiescent state using synchronize_rcu().
91 */
92
93#define _rcu_xchg_pointer(p, v) \
94 ({ \
2b5554c9 95 typeof(*p) _________pv = (v); \
7e30abe3
MD
96 if (!__builtin_constant_p(v) || \
97 ((v) != NULL)) \
98 wmb(); \
2b5554c9 99 uatomic_xchg(p, _________pv); \
7e30abe3
MD
100 })
101
102
103#define _rcu_set_pointer(p, v) \
104 ({ \
2b5554c9 105 typeof(*p) _________pv = (v); \
7e30abe3
MD
106 if (!__builtin_constant_p(v) || \
107 ((v) != NULL)) \
108 wmb(); \
2b5554c9 109 STORE_SHARED(*(p), _________pv); \
7e30abe3
MD
110 })
111
7e30abe3
MD
112/**
113 * _rcu_assign_pointer - assign (publicize) a pointer to a new data structure
114 * meant to be read by RCU read-side critical sections. Returns the assigned
115 * value.
116 *
117 * Documents which pointers will be dereferenced by RCU read-side critical
118 * sections and adds the required memory barriers on architectures requiring
119 * them. It also makes sure the compiler does not reorder code initializing the
120 * data structure before its publication.
121 *
122 * Should match rcu_dereference_pointer().
123 */
124
125#define _rcu_assign_pointer(p, v) _rcu_set_pointer(&(p), v)
126
127#endif /* _URCU_POINTER_STATIC_H */
This page took 0.026655 seconds and 4 git commands to generate.