source: mainline/kernel/generic/include/synch/rcu.h@ 089c23d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 089c23d was 089c23d, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

rcu: Added forgotten compiler barriers to rcu_read_lock/unlock for A-RCU.

  • Property mode set to 100644
File size: 6.9 KB
RevLine 
[79d74fe]1/*
2 * Copyright (c) 2012 Adam Hraska
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup sync
30 * @{
31 */
32/** @file
33 */
34
35#ifndef KERN_RCU_H_
36#define KERN_RCU_H_
37
[8e3ed06]38#include <synch/rcu_types.h>
[181a746]39#include <compiler/barrier.h>
40
41
42
43
44/** Use to assign a pointer to newly initialized data to a rcu reader
45 * accessible pointer.
46 *
47 * Example:
48 * @code
49 * typedef struct exam {
50 * struct exam *next;
51 * int grade;
52 * } exam_t;
53 *
54 * exam_t *exam_list;
55 * // ..
56 *
57 * // Insert at the beginning of the list.
58 * exam_t *my_exam = malloc(sizeof(exam_t), 0);
59 * my_exam->grade = 5;
60 * my_exam->next = exam_list;
61 * rcu_assign(exam_list, my_exam);
62 *
63 * // Changes properly propagate. Every reader either sees
64 * // the old version of exam_list or the new version with
65 * // the fully initialized my_exam.
66 * rcu_synchronize();
67 * // Now we can be sure every reader sees my_exam.
68 *
69 * @endcode
70 */
71#define rcu_assign(ptr, value) \
72 do { \
73 memory_barrier(); \
74 (ptr) = (value); \
75 } while (0)
76
77/** Use to access RCU protected data in a reader section.
78 *
79 * Example:
80 * @code
81 * exam_t *exam_list;
82 * // ...
83 *
84 * rcu_read_lock();
85 * exam_t *first_exam = rcu_access(exam_list);
86 * // We can now safely use first_exam, it won't change
87 * // under us while we're using it.
88 *
89 * // ..
90 * rcu_read_unlock();
91 * @endcode
92 */
93#define rcu_access(ptr) ACCESS_ONCE(ptr)
[79d74fe]94
[8e3ed06]95
96
97
98#include <debug.h>
99#include <preemption.h>
100#include <cpu.h>
101#include <proc/thread.h>
102
103
[4a6da62]104extern bool rcu_read_locked(void);
[181a746]105extern void rcu_synchronize(void);
[4ec9ea41]106extern void rcu_synchronize_expedite(void);
[181a746]107extern void rcu_call(rcu_item_t *rcu_item, rcu_func_t func);
[4ec9ea41]108extern void rcu_barrier(void);
[79d74fe]109
[181a746]110extern void rcu_print_stat(void);
[79d74fe]111
[181a746]112extern void rcu_init(void);
113extern void rcu_stop(void);
114extern void rcu_cpu_init(void);
115extern void rcu_kinit_init(void);
116extern void rcu_thread_init(struct thread*);
117extern void rcu_thread_exiting(void);
118extern void rcu_after_thread_ran(void);
119extern void rcu_before_thread_runs(void);
[79d74fe]120
[181a746]121extern uint64_t rcu_completed_gps(void);
122extern void _rcu_call(bool expedite, rcu_item_t *rcu_item, rcu_func_t func);
[4ec9ea41]123extern void _rcu_synchronize(bool expedite);
[79d74fe]124
[8e3ed06]125
[d4d36f9]126#ifdef RCU_PREEMPT_A
127
128#define RCU_CNT_INC (1 << 1)
129#define RCU_WAS_PREEMPTED (1 << 0)
130
131/* Fwd. decl. because of inlining. */
132void _rcu_preempted_unlock(void);
133
134/** Delimits the start of an RCU reader critical section.
135 *
136 * Reader sections may be nested and are preemptable. You must not
137 * however block/sleep within reader sections.
138 */
139static inline void rcu_read_lock(void)
140{
141 THE->rcu_nesting += RCU_CNT_INC;
[089c23d]142 compiler_barrier();
[d4d36f9]143}
144
145/** Delimits the end of an RCU reader critical section. */
146static inline void rcu_read_unlock(void)
147{
[089c23d]148 compiler_barrier();
[d4d36f9]149 THE->rcu_nesting -= RCU_CNT_INC;
150
151 if (RCU_WAS_PREEMPTED == THE->rcu_nesting) {
152 _rcu_preempted_unlock();
153 }
154}
155
156#elif defined(RCU_PREEMPT_PODZIMEK)
157
[8e3ed06]158/* Fwd decl. required by the inlined implementation. Not part of public API. */
159extern rcu_gp_t _rcu_cur_gp;
160extern void _rcu_signal_read_unlock(void);
161
162
163/** Unconditionally records a quiescent state for the local cpu. */
164static inline void _rcu_record_qs(void)
165{
166 ASSERT(PREEMPTION_DISABLED || interrupts_disabled());
167
168 /*
169 * A new GP was started since the last time we passed a QS.
170 * Notify the detector we have reached a new QS.
171 */
172 if (CPU->rcu.last_seen_gp != _rcu_cur_gp) {
173 rcu_gp_t cur_gp = ACCESS_ONCE(_rcu_cur_gp);
174 /*
175 * Contain memory accesses within a reader critical section.
176 * If we are in rcu_lock() it also makes changes prior to the
177 * start of the GP visible in the reader section.
178 */
179 memory_barrier();
180 /*
181 * Acknowledge we passed a QS since the beginning of rcu.cur_gp.
182 * Cache coherency will lazily transport the value to the
183 * detector while it sleeps in gp_sleep().
184 *
185 * Note that there is a theoretical possibility that we
186 * overwrite a more recent/greater last_seen_gp here with
187 * an older/smaller value. If this cpu is interrupted here
188 * while in rcu_lock() reader sections in the interrupt handler
189 * will update last_seen_gp to the same value as is currently
190 * in local cur_gp. However, if the cpu continues processing
191 * interrupts and the detector starts a new GP immediately,
192 * local interrupt handlers may update last_seen_gp again (ie
193 * properly ack the new GP) with a value greater than local cur_gp.
194 * Resetting last_seen_gp to a previous value here is however
195 * benign and we only have to remember that this reader may end up
196 * in cur_preempted even after the GP ends. That is why we
197 * append next_preempted to cur_preempted rather than overwriting
198 * it as if cur_preempted were empty.
199 */
200 CPU->rcu.last_seen_gp = cur_gp;
201 }
202}
203
204/** Delimits the start of an RCU reader critical section.
205 *
206 * Reader sections may be nested and are preemptable. You must not
207 * however block/sleep within reader sections.
208 */
209static inline void rcu_read_lock(void)
210{
211 ASSERT(CPU);
212 preemption_disable();
213
214 /* Record a QS if not in a reader critical section. */
[5b03a72]215 if (0 == CPU->rcu.nesting_cnt)
[8e3ed06]216 _rcu_record_qs();
217
[5b03a72]218 ++CPU->rcu.nesting_cnt;
[8e3ed06]219
220 preemption_enable();
221}
222
223/** Delimits the end of an RCU reader critical section. */
224static inline void rcu_read_unlock(void)
225{
226 ASSERT(CPU);
227 preemption_disable();
228
[5b03a72]229 if (0 == --CPU->rcu.nesting_cnt) {
[8e3ed06]230 _rcu_record_qs();
231
232 /*
233 * The thread was preempted while in a critical section or
[f0fcb04]234 * the detector is eagerly waiting for this cpu's reader to finish.
[8e3ed06]235 */
[f0fcb04]236 if (CPU->rcu.signal_unlock) {
[8e3ed06]237 /* Rechecks with disabled interrupts. */
238 _rcu_signal_read_unlock();
239 }
240 }
241
242 preemption_enable();
243}
[d4d36f9]244#endif
[8e3ed06]245
[79d74fe]246#endif
247
248/** @}
249 */
Note: See TracBrowser for help on using the repository browser.