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);
58 * rcu_gp_lock ensures mutual exclusion between threads calling
61 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
63 * rcu_registry_lock ensures mutual exclusion between threads
64 * registering and unregistering themselves to/from the registry, and
65 * with threads reading that registry from synchronize_rcu(). However,
66 * this lock is not held all the way through the completion of awaiting
67 * for the grace period. It is sporadically released between iterations
69 * rcu_registry_lock may nest inside rcu_gp_lock.
71 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
72 struct urcu_gp urcu_qsbr_gp
= { .ctr
= URCU_QSBR_GP_ONLINE
};
73 URCU_ATTR_ALIAS("urcu_qsbr_gp") extern struct urcu_gp rcu_gp_qsbr
;
76 * Active attempts to check for reader Q.S. before calling futex().
78 #define RCU_QS_ACTIVE_ATTEMPTS 100
81 * Written to only by each individual reader. Read by both the reader and the
84 DEFINE_URCU_TLS(struct urcu_qsbr_reader
, urcu_qsbr_reader
);
85 DEFINE_URCU_TLS_ALIAS(struct urcu_qsbr_reader
, urcu_qsbr_reader
, rcu_reader_qsbr
);
87 static CDS_LIST_HEAD(registry
);
90 * Queue keeping threads awaiting to wait for a grace period. Contains
91 * struct gp_waiters_thread objects.
93 static DEFINE_URCU_WAIT_QUEUE(gp_waiters
);
95 static void mutex_lock(pthread_mutex_t
*mutex
)
99 #ifndef DISTRUST_SIGNALS_EXTREME
100 ret
= pthread_mutex_lock(mutex
);
103 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
104 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
105 if (ret
!= EBUSY
&& ret
!= EINTR
)
109 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
112 static void mutex_unlock(pthread_mutex_t
*mutex
)
116 ret
= pthread_mutex_unlock(mutex
);
122 * synchronize_rcu() waiting. Single thread.
124 static void wait_gp(void)
126 /* Read reader_gp before read futex */
128 while (uatomic_read(&urcu_qsbr_gp
.futex
) == -1) {
129 if (!futex_noasync(&urcu_qsbr_gp
.futex
, FUTEX_WAIT
, -1, NULL
, NULL
, 0)) {
131 * Prior queued wakeups queued by unrelated code
132 * using the same address can cause futex wait to
133 * return 0 even through the futex value is still
134 * -1 (spurious wakeups). Check the value again
135 * in user-space to validate whether it really
142 /* Value already changed. */
145 /* Retry if interrupted by signal. */
146 break; /* Get out of switch. Check again. */
148 /* Unexpected error. */
155 * Always called with rcu_registry lock held. Releases this lock between
156 * iterations and grabs it again. Holds the lock when it returns.
158 static void wait_for_readers(struct cds_list_head
*input_readers
,
159 struct cds_list_head
*cur_snap_readers
,
160 struct cds_list_head
*qsreaders
)
162 unsigned int wait_loops
= 0;
163 struct urcu_qsbr_reader
*index
, *tmp
;
166 * Wait for each thread URCU_TLS(urcu_qsbr_reader).ctr to either
167 * indicate quiescence (offline), or for them to observe the
168 * current urcu_qsbr_gp.ctr value.
171 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
173 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
174 uatomic_set(&urcu_qsbr_gp
.futex
, -1);
176 * Write futex before write waiting (the other side
177 * reads them in the opposite order).
180 cds_list_for_each_entry(index
, input_readers
, node
) {
181 _CMM_STORE_SHARED(index
->waiting
, 1);
183 /* Write futex before read reader_gp */
186 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
187 switch (urcu_qsbr_reader_state(&index
->ctr
)) {
188 case URCU_READER_ACTIVE_CURRENT
:
189 if (cur_snap_readers
) {
190 cds_list_move(&index
->node
,
195 case URCU_READER_INACTIVE
:
196 cds_list_move(&index
->node
, qsreaders
);
198 case URCU_READER_ACTIVE_OLD
:
200 * Old snapshot. Leaving node in
201 * input_readers will make us busy-loop
202 * until the snapshot becomes current or
203 * the reader becomes inactive.
209 if (cds_list_empty(input_readers
)) {
210 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
211 /* Read reader_gp before write futex */
213 uatomic_set(&urcu_qsbr_gp
.futex
, 0);
217 /* Temporarily unlock the registry lock. */
218 mutex_unlock(&rcu_registry_lock
);
219 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
222 #ifndef HAS_INCOHERENT_CACHES
224 #else /* #ifndef HAS_INCOHERENT_CACHES */
226 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
228 /* Re-lock the registry lock before the next loop. */
229 mutex_lock(&rcu_registry_lock
);
235 * Using a two-subphases algorithm for architectures with smaller than 64-bit
236 * long-size to ensure we do not encounter an overflow bug.
239 #if (CAA_BITS_PER_LONG < 64)
240 void urcu_qsbr_synchronize_rcu(void)
242 CDS_LIST_HEAD(cur_snap_readers
);
243 CDS_LIST_HEAD(qsreaders
);
244 unsigned long was_online
;
245 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
246 struct urcu_waiters waiters
;
248 was_online
= urcu_qsbr_read_ongoing();
250 /* All threads should read qparity before accessing data structure
251 * where new ptr points to. In the "then" case, rcu_thread_offline
252 * includes a memory barrier.
254 * Mark the writer thread offline to make sure we don't wait for
255 * our own quiescent state. This allows using synchronize_rcu()
256 * in threads registered as readers.
259 urcu_qsbr_thread_offline();
264 * Add ourself to gp_waiters queue of threads awaiting to wait
265 * for a grace period. Proceed to perform the grace period only
266 * if we are the first thread added into the queue.
268 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
269 /* Not first in queue: will be awakened by another thread. */
270 urcu_adaptative_busy_wait(&wait
);
273 /* We won't need to wake ourself up */
274 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
276 mutex_lock(&rcu_gp_lock
);
279 * Move all waiters into our local queue.
281 urcu_move_waiters(&waiters
, &gp_waiters
);
283 mutex_lock(&rcu_registry_lock
);
285 if (cds_list_empty(®istry
))
289 * Wait for readers to observe original parity or be quiescent.
290 * wait_for_readers() can release and grab again rcu_registry_lock
293 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
296 * Must finish waiting for quiescent state for original parity
297 * before committing next urcu_qsbr_gp.ctr update to memory. Failure
298 * to do so could result in the writer waiting forever while new
299 * readers are always accessing data (no progress). Enforce
300 * compiler-order of load URCU_TLS(urcu_qsbr_reader).ctr before store
301 * to urcu_qsbr_gp.ctr.
306 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
307 * model easier to understand. It does not have a big performance impact
308 * anyway, given this is the write-side.
312 /* Switch parity: 0 -> 1, 1 -> 0 */
313 CMM_STORE_SHARED(urcu_qsbr_gp
.ctr
, urcu_qsbr_gp
.ctr
^ URCU_QSBR_GP_CTR
);
316 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
317 * quiescent state. Failure to do so could result in the writer
318 * waiting forever while new readers are always accessing data
319 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
320 * before load URCU_TLS(urcu_qsbr_reader).ctr.
325 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
326 * model easier to understand. It does not have a big performance impact
327 * anyway, given this is the write-side.
332 * Wait for readers to observe new parity or be quiescent.
333 * wait_for_readers() can release and grab again rcu_registry_lock
336 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
339 * Put quiescent reader list back into registry.
341 cds_list_splice(&qsreaders
, ®istry
);
343 mutex_unlock(&rcu_registry_lock
);
344 mutex_unlock(&rcu_gp_lock
);
345 urcu_wake_all_waiters(&waiters
);
348 * Finish waiting for reader threads before letting the old ptr being
352 urcu_qsbr_thread_online();
356 #else /* !(CAA_BITS_PER_LONG < 64) */
357 void urcu_qsbr_synchronize_rcu(void)
359 CDS_LIST_HEAD(qsreaders
);
360 unsigned long was_online
;
361 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
362 struct urcu_waiters waiters
;
364 was_online
= urcu_qsbr_read_ongoing();
367 * Mark the writer thread offline to make sure we don't wait for
368 * our own quiescent state. This allows using synchronize_rcu()
369 * in threads registered as readers.
372 urcu_qsbr_thread_offline();
377 * Add ourself to gp_waiters queue of threads awaiting to wait
378 * for a grace period. Proceed to perform the grace period only
379 * if we are the first thread added into the queue.
381 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
382 /* Not first in queue: will be awakened by another thread. */
383 urcu_adaptative_busy_wait(&wait
);
386 /* We won't need to wake ourself up */
387 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
389 mutex_lock(&rcu_gp_lock
);
392 * Move all waiters into our local queue.
394 urcu_move_waiters(&waiters
, &gp_waiters
);
396 mutex_lock(&rcu_registry_lock
);
398 if (cds_list_empty(®istry
))
401 /* Increment current G.P. */
402 CMM_STORE_SHARED(urcu_qsbr_gp
.ctr
, urcu_qsbr_gp
.ctr
+ URCU_QSBR_GP_CTR
);
405 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
406 * quiescent state. Failure to do so could result in the writer
407 * waiting forever while new readers are always accessing data
408 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
409 * before load URCU_TLS(urcu_qsbr_reader).ctr.
414 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
415 * model easier to understand. It does not have a big performance impact
416 * anyway, given this is the write-side.
421 * Wait for readers to observe new count of be quiescent.
422 * wait_for_readers() can release and grab again rcu_registry_lock
425 wait_for_readers(®istry
, NULL
, &qsreaders
);
428 * Put quiescent reader list back into registry.
430 cds_list_splice(&qsreaders
, ®istry
);
432 mutex_unlock(&rcu_registry_lock
);
433 mutex_unlock(&rcu_gp_lock
);
434 urcu_wake_all_waiters(&waiters
);
437 urcu_qsbr_thread_online();
441 #endif /* !(CAA_BITS_PER_LONG < 64) */
442 URCU_ATTR_ALIAS("urcu_qsbr_synchronize_rcu")
443 void synchronize_rcu_qsbr();
446 * library wrappers to be used by non-LGPL compatible source code.
449 void urcu_qsbr_read_lock(void)
451 _urcu_qsbr_read_lock();
453 URCU_ATTR_ALIAS("urcu_qsbr_read_lock") void rcu_read_lock_qsbr();
455 void urcu_qsbr_read_unlock(void)
457 _urcu_qsbr_read_unlock();
459 URCU_ATTR_ALIAS("urcu_qsbr_read_unlock") void rcu_read_unlock_qsbr();
461 int urcu_qsbr_read_ongoing(void)
463 return _urcu_qsbr_read_ongoing();
465 URCU_ATTR_ALIAS("urcu_qsbr_read_ongoing")
466 void rcu_read_ongoing_qsbr();
468 void urcu_qsbr_quiescent_state(void)
470 _urcu_qsbr_quiescent_state();
472 URCU_ATTR_ALIAS("urcu_qsbr_quiescent_state")
473 void rcu_quiescent_state_qsbr();
475 void urcu_qsbr_thread_offline(void)
477 _urcu_qsbr_thread_offline();
479 URCU_ATTR_ALIAS("urcu_qsbr_thread_offline")
480 void rcu_thread_offline_qsbr();
482 void urcu_qsbr_thread_online(void)
484 _urcu_qsbr_thread_online();
486 URCU_ATTR_ALIAS("urcu_qsbr_thread_online")
487 void rcu_thread_online_qsbr();
489 void urcu_qsbr_register_thread(void)
491 URCU_TLS(urcu_qsbr_reader
).tid
= pthread_self();
492 assert(URCU_TLS(urcu_qsbr_reader
).ctr
== 0);
494 mutex_lock(&rcu_registry_lock
);
495 assert(!URCU_TLS(urcu_qsbr_reader
).registered
);
496 URCU_TLS(urcu_qsbr_reader
).registered
= 1;
497 cds_list_add(&URCU_TLS(urcu_qsbr_reader
).node
, ®istry
);
498 mutex_unlock(&rcu_registry_lock
);
499 _urcu_qsbr_thread_online();
501 URCU_ATTR_ALIAS("urcu_qsbr_register_thread")
502 void rcu_register_thread_qsbr();
504 void urcu_qsbr_unregister_thread(void)
507 * We have to make the thread offline otherwise we end up dealocking
508 * with a waiting writer.
510 _urcu_qsbr_thread_offline();
511 assert(URCU_TLS(urcu_qsbr_reader
).registered
);
512 URCU_TLS(urcu_qsbr_reader
).registered
= 0;
513 mutex_lock(&rcu_registry_lock
);
514 cds_list_del(&URCU_TLS(urcu_qsbr_reader
).node
);
515 mutex_unlock(&rcu_registry_lock
);
517 URCU_ATTR_ALIAS("urcu_qsbr_unregister_thread")
518 void rcu_unregister_thread_qsbr();
520 void urcu_qsbr_exit(void)
523 * Assertion disabled because call_rcu threads are now rcu
524 * readers, and left running at exit.
525 * assert(cds_list_empty(®istry));
528 URCU_ATTR_ALIAS("urcu_qsbr_exit") void rcu_exit_qsbr();
530 DEFINE_RCU_FLAVOR(rcu_flavor
);
531 DEFINE_RCU_FLAVOR_ALIAS(rcu_flavor
, alias_rcu_flavor
);
533 #include "urcu-call-rcu-impl.h"
534 #include "urcu-defer-impl.h"