4 * Userspace RCU QSBR library
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
38 #include "urcu/wfcqueue.h"
39 #include "urcu/map/urcu-qsbr.h"
40 #define BUILD_QSBR_LIB
41 #include "urcu/static/urcu-qsbr.h"
42 #include "urcu-pointer.h"
43 #include "urcu/tls-compat.h"
46 #include "urcu-wait.h"
48 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
50 #include "urcu-qsbr.h"
53 void __attribute__((destructor
)) rcu_exit(void);
55 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
56 struct urcu_gp rcu_gp
= { .ctr
= RCU_GP_ONLINE
};
59 * Active attempts to check for reader Q.S. before calling futex().
61 #define RCU_QS_ACTIVE_ATTEMPTS 100
64 * Written to only by each individual reader. Read by both the reader and the
67 DEFINE_URCU_TLS(struct rcu_reader
, rcu_reader
);
70 unsigned int rcu_yield_active
;
71 DEFINE_URCU_TLS(unsigned int, rcu_rand_yield
);
74 static CDS_LIST_HEAD(registry
);
77 * Queue keeping threads awaiting to wait for a grace period. Contains
78 * struct gp_waiters_thread objects.
80 static DEFINE_URCU_WAIT_QUEUE(gp_waiters
);
82 static void mutex_lock(pthread_mutex_t
*mutex
)
86 #ifndef DISTRUST_SIGNALS_EXTREME
87 ret
= pthread_mutex_lock(mutex
);
90 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
91 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
92 if (ret
!= EBUSY
&& ret
!= EINTR
)
96 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
99 static void mutex_unlock(pthread_mutex_t
*mutex
)
103 ret
= pthread_mutex_unlock(mutex
);
109 * synchronize_rcu() waiting. Single thread.
111 static void wait_gp(void)
113 /* Read reader_gp before read futex */
115 if (uatomic_read(&rcu_gp
.futex
) == -1)
116 futex_noasync(&rcu_gp
.futex
, FUTEX_WAIT
, -1,
120 static void wait_for_readers(struct cds_list_head
*input_readers
,
121 struct cds_list_head
*cur_snap_readers
,
122 struct cds_list_head
*qsreaders
)
125 struct rcu_reader
*index
, *tmp
;
128 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
129 * indicate quiescence (offline), or for them to observe the
130 * current rcu_gp.ctr value.
134 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
135 uatomic_set(&rcu_gp
.futex
, -1);
137 * Write futex before write waiting (the other side
138 * reads them in the opposite order).
141 cds_list_for_each_entry(index
, input_readers
, node
) {
142 _CMM_STORE_SHARED(index
->waiting
, 1);
144 /* Write futex before read reader_gp */
147 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
148 switch (rcu_reader_state(&index
->ctr
)) {
149 case RCU_READER_ACTIVE_CURRENT
:
150 if (cur_snap_readers
) {
151 cds_list_move(&index
->node
,
156 case RCU_READER_INACTIVE
:
157 cds_list_move(&index
->node
, qsreaders
);
159 case RCU_READER_ACTIVE_OLD
:
161 * Old snapshot. Leaving node in
162 * input_readers will make us busy-loop
163 * until the snapshot becomes current or
164 * the reader becomes inactive.
170 if (cds_list_empty(input_readers
)) {
171 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
172 /* Read reader_gp before write futex */
174 uatomic_set(&rcu_gp
.futex
, 0);
178 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
181 #ifndef HAS_INCOHERENT_CACHES
183 #else /* #ifndef HAS_INCOHERENT_CACHES */
185 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
192 * Using a two-subphases algorithm for architectures with smaller than 64-bit
193 * long-size to ensure we do not encounter an overflow bug.
196 #if (CAA_BITS_PER_LONG < 64)
197 void synchronize_rcu(void)
199 CDS_LIST_HEAD(cur_snap_readers
);
200 CDS_LIST_HEAD(qsreaders
);
201 unsigned long was_online
;
202 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
203 struct urcu_waiters waiters
;
205 was_online
= rcu_read_ongoing();
207 /* All threads should read qparity before accessing data structure
208 * where new ptr points to. In the "then" case, rcu_thread_offline
209 * includes a memory barrier.
211 * Mark the writer thread offline to make sure we don't wait for
212 * our own quiescent state. This allows using synchronize_rcu()
213 * in threads registered as readers.
216 rcu_thread_offline();
221 * Add ourself to gp_waiters queue of threads awaiting to wait
222 * for a grace period. Proceed to perform the grace period only
223 * if we are the first thread added into the queue.
225 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
226 /* Not first in queue: will be awakened by another thread. */
227 urcu_adaptative_busy_wait(&wait
);
230 /* We won't need to wake ourself up */
231 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
233 mutex_lock(&rcu_gp_lock
);
236 * Move all waiters into our local queue.
238 urcu_move_waiters(&waiters
, &gp_waiters
);
240 if (cds_list_empty(®istry
))
244 * Wait for readers to observe original parity or be quiescent.
246 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
249 * Must finish waiting for quiescent state for original parity
250 * before committing next rcu_gp.ctr update to memory. Failure
251 * to do so could result in the writer waiting forever while new
252 * readers are always accessing data (no progress). Enforce
253 * compiler-order of load URCU_TLS(rcu_reader).ctr before store
259 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
260 * model easier to understand. It does not have a big performance impact
261 * anyway, given this is the write-side.
265 /* Switch parity: 0 -> 1, 1 -> 0 */
266 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ RCU_GP_CTR
);
269 * Must commit rcu_gp.ctr update to memory before waiting for
270 * quiescent state. Failure to do so could result in the writer
271 * waiting forever while new readers are always accessing data
272 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
273 * before load URCU_TLS(rcu_reader).ctr.
278 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
279 * model easier to understand. It does not have a big performance impact
280 * anyway, given this is the write-side.
285 * Wait for readers to observe new parity or be quiescent.
287 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
290 * Put quiescent reader list back into registry.
292 cds_list_splice(&qsreaders
, ®istry
);
294 mutex_unlock(&rcu_gp_lock
);
295 urcu_wake_all_waiters(&waiters
);
298 * Finish waiting for reader threads before letting the old ptr being
306 #else /* !(CAA_BITS_PER_LONG < 64) */
307 void synchronize_rcu(void)
309 CDS_LIST_HEAD(qsreaders
);
310 unsigned long was_online
;
311 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
312 struct urcu_waiters waiters
;
314 was_online
= rcu_read_ongoing();
317 * Mark the writer thread offline to make sure we don't wait for
318 * our own quiescent state. This allows using synchronize_rcu()
319 * in threads registered as readers.
322 rcu_thread_offline();
327 * Add ourself to gp_waiters queue of threads awaiting to wait
328 * for a grace period. Proceed to perform the grace period only
329 * if we are the first thread added into the queue.
331 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
332 /* Not first in queue: will be awakened by another thread. */
333 urcu_adaptative_busy_wait(&wait
);
336 /* We won't need to wake ourself up */
337 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
339 mutex_lock(&rcu_gp_lock
);
342 * Move all waiters into our local queue.
344 urcu_move_waiters(&waiters
, &gp_waiters
);
346 if (cds_list_empty(®istry
))
349 /* Increment current G.P. */
350 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
+ RCU_GP_CTR
);
353 * Must commit rcu_gp.ctr update to memory before waiting for
354 * quiescent state. Failure to do so could result in the writer
355 * waiting forever while new readers are always accessing data
356 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
357 * before load URCU_TLS(rcu_reader).ctr.
362 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
363 * model easier to understand. It does not have a big performance impact
364 * anyway, given this is the write-side.
369 * Wait for readers to observe new count of be quiescent.
371 wait_for_readers(®istry
, NULL
, &qsreaders
);
374 * Put quiescent reader list back into registry.
376 cds_list_splice(&qsreaders
, ®istry
);
378 mutex_unlock(&rcu_gp_lock
);
379 urcu_wake_all_waiters(&waiters
);
386 #endif /* !(CAA_BITS_PER_LONG < 64) */
389 * library wrappers to be used by non-LGPL compatible source code.
392 void rcu_read_lock(void)
397 void rcu_read_unlock(void)
402 int rcu_read_ongoing(void)
404 return _rcu_read_ongoing();
407 void rcu_quiescent_state(void)
409 _rcu_quiescent_state();
412 void rcu_thread_offline(void)
414 _rcu_thread_offline();
417 void rcu_thread_online(void)
419 _rcu_thread_online();
422 void rcu_register_thread(void)
424 URCU_TLS(rcu_reader
).tid
= pthread_self();
425 assert(URCU_TLS(rcu_reader
).ctr
== 0);
427 mutex_lock(&rcu_gp_lock
);
428 cds_list_add(&URCU_TLS(rcu_reader
).node
, ®istry
);
429 mutex_unlock(&rcu_gp_lock
);
430 _rcu_thread_online();
433 void rcu_unregister_thread(void)
436 * We have to make the thread offline otherwise we end up dealocking
437 * with a waiting writer.
439 _rcu_thread_offline();
440 mutex_lock(&rcu_gp_lock
);
441 cds_list_del(&URCU_TLS(rcu_reader
).node
);
442 mutex_unlock(&rcu_gp_lock
);
448 * Assertion disabled because call_rcu threads are now rcu
449 * readers, and left running at exit.
450 * assert(cds_list_empty(®istry));
454 DEFINE_RCU_FLAVOR(rcu_flavor
);
456 #include "urcu-call-rcu-impl.h"
457 #include "urcu-defer-impl.h"