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.
26 #define URCU_NO_COMPAT_IDENTIFIERS
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"
47 #include "urcu-utils.h"
50 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
52 #include <urcu/urcu-qsbr.h>
55 void __attribute__((destructor
)) urcu_qsbr_exit(void);
56 static void urcu_call_rcu_exit(void);
59 * rcu_gp_lock ensures mutual exclusion between threads calling
62 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
64 * rcu_registry_lock ensures mutual exclusion between threads
65 * registering and unregistering themselves to/from the registry, and
66 * with threads reading that registry from synchronize_rcu(). However,
67 * this lock is not held all the way through the completion of awaiting
68 * for the grace period. It is sporadically released between iterations
70 * rcu_registry_lock may nest inside rcu_gp_lock.
72 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
73 struct urcu_gp urcu_qsbr_gp
= { .ctr
= URCU_QSBR_GP_ONLINE
};
74 URCU_ATTR_ALIAS("urcu_qsbr_gp") extern struct urcu_gp rcu_gp_qsbr
;
77 * Active attempts to check for reader Q.S. before calling futex().
79 #define RCU_QS_ACTIVE_ATTEMPTS 100
82 * Written to only by each individual reader. Read by both the reader and the
85 DEFINE_URCU_TLS(struct urcu_qsbr_reader
, urcu_qsbr_reader
);
86 DEFINE_URCU_TLS_ALIAS(struct urcu_qsbr_reader
, urcu_qsbr_reader
, rcu_reader_qsbr
);
88 static CDS_LIST_HEAD(registry
);
91 * Queue keeping threads awaiting to wait for a grace period. Contains
92 * struct gp_waiters_thread objects.
94 static DEFINE_URCU_WAIT_QUEUE(gp_waiters
);
96 static void mutex_lock(pthread_mutex_t
*mutex
)
100 #ifndef DISTRUST_SIGNALS_EXTREME
101 ret
= pthread_mutex_lock(mutex
);
104 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
105 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
106 if (ret
!= EBUSY
&& ret
!= EINTR
)
110 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
113 static void mutex_unlock(pthread_mutex_t
*mutex
)
117 ret
= pthread_mutex_unlock(mutex
);
123 * synchronize_rcu() waiting. Single thread.
125 static void wait_gp(void)
127 /* Read reader_gp before read futex */
129 while (uatomic_read(&urcu_qsbr_gp
.futex
) == -1) {
130 if (!futex_noasync(&urcu_qsbr_gp
.futex
, FUTEX_WAIT
, -1, NULL
, NULL
, 0)) {
132 * Prior queued wakeups queued by unrelated code
133 * using the same address can cause futex wait to
134 * return 0 even through the futex value is still
135 * -1 (spurious wakeups). Check the value again
136 * in user-space to validate whether it really
143 /* Value already changed. */
146 /* Retry if interrupted by signal. */
147 break; /* Get out of switch. Check again. */
149 /* Unexpected error. */
156 * Always called with rcu_registry lock held. Releases this lock between
157 * iterations and grabs it again. Holds the lock when it returns.
159 static void wait_for_readers(struct cds_list_head
*input_readers
,
160 struct cds_list_head
*cur_snap_readers
,
161 struct cds_list_head
*qsreaders
)
163 unsigned int wait_loops
= 0;
164 struct urcu_qsbr_reader
*index
, *tmp
;
167 * Wait for each thread URCU_TLS(urcu_qsbr_reader).ctr to either
168 * indicate quiescence (offline), or for them to observe the
169 * current urcu_qsbr_gp.ctr value.
172 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
174 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
175 uatomic_set(&urcu_qsbr_gp
.futex
, -1);
177 * Write futex before write waiting (the other side
178 * reads them in the opposite order).
181 cds_list_for_each_entry(index
, input_readers
, node
) {
182 _CMM_STORE_SHARED(index
->waiting
, 1);
184 /* Write futex before read reader_gp */
187 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
188 switch (urcu_qsbr_reader_state(&index
->ctr
)) {
189 case URCU_READER_ACTIVE_CURRENT
:
190 if (cur_snap_readers
) {
191 cds_list_move(&index
->node
,
196 case URCU_READER_INACTIVE
:
197 cds_list_move(&index
->node
, qsreaders
);
199 case URCU_READER_ACTIVE_OLD
:
201 * Old snapshot. Leaving node in
202 * input_readers will make us busy-loop
203 * until the snapshot becomes current or
204 * the reader becomes inactive.
210 if (cds_list_empty(input_readers
)) {
211 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
212 /* Read reader_gp before write futex */
214 uatomic_set(&urcu_qsbr_gp
.futex
, 0);
218 /* Temporarily unlock the registry lock. */
219 mutex_unlock(&rcu_registry_lock
);
220 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
223 #ifndef HAS_INCOHERENT_CACHES
225 #else /* #ifndef HAS_INCOHERENT_CACHES */
227 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
229 /* Re-lock the registry lock before the next loop. */
230 mutex_lock(&rcu_registry_lock
);
236 * Using a two-subphases algorithm for architectures with smaller than 64-bit
237 * long-size to ensure we do not encounter an overflow bug.
240 #if (CAA_BITS_PER_LONG < 64)
241 void urcu_qsbr_synchronize_rcu(void)
243 CDS_LIST_HEAD(cur_snap_readers
);
244 CDS_LIST_HEAD(qsreaders
);
245 unsigned long was_online
;
246 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
247 struct urcu_waiters waiters
;
249 was_online
= urcu_qsbr_read_ongoing();
251 /* All threads should read qparity before accessing data structure
252 * where new ptr points to. In the "then" case, rcu_thread_offline
253 * includes a memory barrier.
255 * Mark the writer thread offline to make sure we don't wait for
256 * our own quiescent state. This allows using synchronize_rcu()
257 * in threads registered as readers.
260 urcu_qsbr_thread_offline();
265 * Add ourself to gp_waiters queue of threads awaiting to wait
266 * for a grace period. Proceed to perform the grace period only
267 * if we are the first thread added into the queue.
269 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
270 /* Not first in queue: will be awakened by another thread. */
271 urcu_adaptative_busy_wait(&wait
);
274 /* We won't need to wake ourself up */
275 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
277 mutex_lock(&rcu_gp_lock
);
280 * Move all waiters into our local queue.
282 urcu_move_waiters(&waiters
, &gp_waiters
);
284 mutex_lock(&rcu_registry_lock
);
286 if (cds_list_empty(®istry
))
290 * Wait for readers to observe original parity or be quiescent.
291 * wait_for_readers() can release and grab again rcu_registry_lock
294 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
297 * Must finish waiting for quiescent state for original parity
298 * before committing next urcu_qsbr_gp.ctr update to memory. Failure
299 * to do so could result in the writer waiting forever while new
300 * readers are always accessing data (no progress). Enforce
301 * compiler-order of load URCU_TLS(urcu_qsbr_reader).ctr before store
302 * to urcu_qsbr_gp.ctr.
307 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
308 * model easier to understand. It does not have a big performance impact
309 * anyway, given this is the write-side.
313 /* Switch parity: 0 -> 1, 1 -> 0 */
314 CMM_STORE_SHARED(urcu_qsbr_gp
.ctr
, urcu_qsbr_gp
.ctr
^ URCU_QSBR_GP_CTR
);
317 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
318 * quiescent state. Failure to do so could result in the writer
319 * waiting forever while new readers are always accessing data
320 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
321 * before load URCU_TLS(urcu_qsbr_reader).ctr.
326 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
327 * model easier to understand. It does not have a big performance impact
328 * anyway, given this is the write-side.
333 * Wait for readers to observe new parity or be quiescent.
334 * wait_for_readers() can release and grab again rcu_registry_lock
337 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
340 * Put quiescent reader list back into registry.
342 cds_list_splice(&qsreaders
, ®istry
);
344 mutex_unlock(&rcu_registry_lock
);
345 mutex_unlock(&rcu_gp_lock
);
346 urcu_wake_all_waiters(&waiters
);
349 * Finish waiting for reader threads before letting the old ptr being
353 urcu_qsbr_thread_online();
357 #else /* !(CAA_BITS_PER_LONG < 64) */
358 void urcu_qsbr_synchronize_rcu(void)
360 CDS_LIST_HEAD(qsreaders
);
361 unsigned long was_online
;
362 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
363 struct urcu_waiters waiters
;
365 was_online
= urcu_qsbr_read_ongoing();
368 * Mark the writer thread offline to make sure we don't wait for
369 * our own quiescent state. This allows using synchronize_rcu()
370 * in threads registered as readers.
373 urcu_qsbr_thread_offline();
378 * Add ourself to gp_waiters queue of threads awaiting to wait
379 * for a grace period. Proceed to perform the grace period only
380 * if we are the first thread added into the queue.
382 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
383 /* Not first in queue: will be awakened by another thread. */
384 urcu_adaptative_busy_wait(&wait
);
387 /* We won't need to wake ourself up */
388 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
390 mutex_lock(&rcu_gp_lock
);
393 * Move all waiters into our local queue.
395 urcu_move_waiters(&waiters
, &gp_waiters
);
397 mutex_lock(&rcu_registry_lock
);
399 if (cds_list_empty(®istry
))
402 /* Increment current G.P. */
403 CMM_STORE_SHARED(urcu_qsbr_gp
.ctr
, urcu_qsbr_gp
.ctr
+ URCU_QSBR_GP_CTR
);
406 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
407 * quiescent state. Failure to do so could result in the writer
408 * waiting forever while new readers are always accessing data
409 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
410 * before load URCU_TLS(urcu_qsbr_reader).ctr.
415 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
416 * model easier to understand. It does not have a big performance impact
417 * anyway, given this is the write-side.
422 * Wait for readers to observe new count of be quiescent.
423 * wait_for_readers() can release and grab again rcu_registry_lock
426 wait_for_readers(®istry
, NULL
, &qsreaders
);
429 * Put quiescent reader list back into registry.
431 cds_list_splice(&qsreaders
, ®istry
);
433 mutex_unlock(&rcu_registry_lock
);
434 mutex_unlock(&rcu_gp_lock
);
435 urcu_wake_all_waiters(&waiters
);
438 urcu_qsbr_thread_online();
442 #endif /* !(CAA_BITS_PER_LONG < 64) */
443 URCU_ATTR_ALIAS("urcu_qsbr_synchronize_rcu")
444 void synchronize_rcu_qsbr();
447 * library wrappers to be used by non-LGPL compatible source code.
450 void urcu_qsbr_read_lock(void)
452 _urcu_qsbr_read_lock();
454 URCU_ATTR_ALIAS("urcu_qsbr_read_lock") void rcu_read_lock_qsbr();
456 void urcu_qsbr_read_unlock(void)
458 _urcu_qsbr_read_unlock();
460 URCU_ATTR_ALIAS("urcu_qsbr_read_unlock") void rcu_read_unlock_qsbr();
462 int urcu_qsbr_read_ongoing(void)
464 return _urcu_qsbr_read_ongoing();
466 URCU_ATTR_ALIAS("urcu_qsbr_read_ongoing")
467 void rcu_read_ongoing_qsbr();
469 void urcu_qsbr_quiescent_state(void)
471 _urcu_qsbr_quiescent_state();
473 URCU_ATTR_ALIAS("urcu_qsbr_quiescent_state")
474 void rcu_quiescent_state_qsbr();
476 void urcu_qsbr_thread_offline(void)
478 _urcu_qsbr_thread_offline();
480 URCU_ATTR_ALIAS("urcu_qsbr_thread_offline")
481 void rcu_thread_offline_qsbr();
483 void urcu_qsbr_thread_online(void)
485 _urcu_qsbr_thread_online();
487 URCU_ATTR_ALIAS("urcu_qsbr_thread_online")
488 void rcu_thread_online_qsbr();
490 void urcu_qsbr_register_thread(void)
492 URCU_TLS(urcu_qsbr_reader
).tid
= pthread_self();
493 assert(URCU_TLS(urcu_qsbr_reader
).ctr
== 0);
495 mutex_lock(&rcu_registry_lock
);
496 assert(!URCU_TLS(urcu_qsbr_reader
).registered
);
497 URCU_TLS(urcu_qsbr_reader
).registered
= 1;
498 cds_list_add(&URCU_TLS(urcu_qsbr_reader
).node
, ®istry
);
499 mutex_unlock(&rcu_registry_lock
);
500 _urcu_qsbr_thread_online();
502 URCU_ATTR_ALIAS("urcu_qsbr_register_thread")
503 void rcu_register_thread_qsbr();
505 void urcu_qsbr_unregister_thread(void)
508 * We have to make the thread offline otherwise we end up dealocking
509 * with a waiting writer.
511 _urcu_qsbr_thread_offline();
512 assert(URCU_TLS(urcu_qsbr_reader
).registered
);
513 URCU_TLS(urcu_qsbr_reader
).registered
= 0;
514 mutex_lock(&rcu_registry_lock
);
515 cds_list_del(&URCU_TLS(urcu_qsbr_reader
).node
);
516 mutex_unlock(&rcu_registry_lock
);
518 URCU_ATTR_ALIAS("urcu_qsbr_unregister_thread")
519 void rcu_unregister_thread_qsbr();
521 void urcu_qsbr_exit(void)
524 * Assertion disabled because call_rcu threads are now rcu
525 * readers, and left running at exit.
526 * assert(cds_list_empty(®istry));
528 urcu_call_rcu_exit();
530 URCU_ATTR_ALIAS("urcu_qsbr_exit") void rcu_exit_qsbr();
532 DEFINE_RCU_FLAVOR(rcu_flavor
);
533 DEFINE_RCU_FLAVOR_ALIAS(rcu_flavor
, alias_rcu_flavor
);
535 #include "urcu-call-rcu-impl.h"
536 #include "urcu-defer-impl.h"