1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 // SPDX-FileCopyrightText: 2009 Paul E. McKenney, IBM Corporation.
4 // SPDX-License-Identifier: LGPL-2.1-or-later
7 * Userspace RCU QSBR library
9 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
12 #define URCU_NO_COMPAT_IDENTIFIERS
23 #include <urcu/assert.h>
24 #include <urcu/wfcqueue.h>
25 #include <urcu/map/urcu-qsbr.h>
26 #define BUILD_QSBR_LIB
27 #include <urcu/static/urcu-qsbr.h>
28 #include <urcu/pointer.h>
29 #include <urcu/tls-compat.h>
32 #include "urcu-wait.h"
33 #include "urcu-utils.h"
36 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
38 #include <urcu/urcu-qsbr.h>
41 void __attribute__((destructor
)) urcu_qsbr_exit(void);
42 static void urcu_call_rcu_exit(void);
45 * rcu_gp_lock ensures mutual exclusion between threads calling
48 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
50 * rcu_registry_lock ensures mutual exclusion between threads
51 * registering and unregistering themselves to/from the registry, and
52 * with threads reading that registry from synchronize_rcu(). However,
53 * this lock is not held all the way through the completion of awaiting
54 * for the grace period. It is sporadically released between iterations
56 * rcu_registry_lock may nest inside rcu_gp_lock.
58 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
59 struct urcu_gp urcu_qsbr_gp
= { .ctr
= URCU_QSBR_GP_ONLINE
};
62 * Active attempts to check for reader Q.S. before calling futex().
64 #define RCU_QS_ACTIVE_ATTEMPTS 100
67 * Written to only by each individual reader. Read by both the reader and the
70 DEFINE_URCU_TLS(struct urcu_qsbr_reader
, urcu_qsbr_reader
);
72 static CDS_LIST_HEAD(registry
);
75 * Queue keeping threads awaiting to wait for a grace period. Contains
76 * struct gp_waiters_thread objects.
78 static DEFINE_URCU_WAIT_QUEUE(gp_waiters
);
80 static void mutex_lock(pthread_mutex_t
*mutex
)
84 #ifndef DISTRUST_SIGNALS_EXTREME
85 ret
= pthread_mutex_lock(mutex
);
88 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
89 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
90 if (ret
!= EBUSY
&& ret
!= EINTR
)
94 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
97 static void mutex_unlock(pthread_mutex_t
*mutex
)
101 ret
= pthread_mutex_unlock(mutex
);
107 * synchronize_rcu() waiting. Single thread.
109 static void wait_gp(void)
111 /* Read reader_gp before read futex */
113 while (uatomic_read(&urcu_qsbr_gp
.futex
) == -1) {
114 if (!futex_noasync(&urcu_qsbr_gp
.futex
, FUTEX_WAIT
, -1, NULL
, NULL
, 0)) {
116 * Prior queued wakeups queued by unrelated code
117 * using the same address can cause futex wait to
118 * return 0 even through the futex value is still
119 * -1 (spurious wakeups). Check the value again
120 * in user-space to validate whether it really
127 /* Value already changed. */
130 /* Retry if interrupted by signal. */
131 break; /* Get out of switch. Check again. */
133 /* Unexpected error. */
140 * Always called with rcu_registry lock held. Releases this lock between
141 * iterations and grabs it again. Holds the lock when it returns.
143 static void wait_for_readers(struct cds_list_head
*input_readers
,
144 struct cds_list_head
*cur_snap_readers
,
145 struct cds_list_head
*qsreaders
)
147 unsigned int wait_loops
= 0;
148 struct urcu_qsbr_reader
*index
, *tmp
;
151 * Wait for each thread URCU_TLS(urcu_qsbr_reader).ctr to either
152 * indicate quiescence (offline), or for them to observe the
153 * current urcu_qsbr_gp.ctr value.
156 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
158 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
159 uatomic_set(&urcu_qsbr_gp
.futex
, -1);
161 * Write futex before write waiting (the other side
162 * reads them in the opposite order).
165 cds_list_for_each_entry(index
, input_readers
, node
) {
166 _CMM_STORE_SHARED(index
->waiting
, 1);
168 /* Write futex before read reader_gp */
171 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
172 switch (urcu_qsbr_reader_state(&index
->ctr
)) {
173 case URCU_READER_ACTIVE_CURRENT
:
174 if (cur_snap_readers
) {
175 cds_list_move(&index
->node
,
180 case URCU_READER_INACTIVE
:
181 cds_list_move(&index
->node
, qsreaders
);
183 case URCU_READER_ACTIVE_OLD
:
185 * Old snapshot. Leaving node in
186 * input_readers will make us busy-loop
187 * until the snapshot becomes current or
188 * the reader becomes inactive.
194 if (cds_list_empty(input_readers
)) {
195 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
196 /* Read reader_gp before write futex */
198 uatomic_set(&urcu_qsbr_gp
.futex
, 0);
202 /* Temporarily unlock the registry lock. */
203 mutex_unlock(&rcu_registry_lock
);
204 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
207 #ifndef HAS_INCOHERENT_CACHES
209 #else /* #ifndef HAS_INCOHERENT_CACHES */
211 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
213 /* Re-lock the registry lock before the next loop. */
214 mutex_lock(&rcu_registry_lock
);
220 * Using a two-subphases algorithm for architectures with smaller than 64-bit
221 * long-size to ensure we do not encounter an overflow bug.
224 #if (CAA_BITS_PER_LONG < 64)
225 void urcu_qsbr_synchronize_rcu(void)
227 CDS_LIST_HEAD(cur_snap_readers
);
228 CDS_LIST_HEAD(qsreaders
);
229 unsigned long was_online
;
230 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
231 struct urcu_waiters waiters
;
233 was_online
= urcu_qsbr_read_ongoing();
235 /* All threads should read qparity before accessing data structure
236 * where new ptr points to. In the "then" case, rcu_thread_offline
237 * includes a memory barrier.
239 * Mark the writer thread offline to make sure we don't wait for
240 * our own quiescent state. This allows using synchronize_rcu()
241 * in threads registered as readers.
244 urcu_qsbr_thread_offline();
249 * Add ourself to gp_waiters queue of threads awaiting to wait
250 * for a grace period. Proceed to perform the grace period only
251 * if we are the first thread added into the queue.
253 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
254 /* Not first in queue: will be awakened by another thread. */
255 urcu_adaptative_busy_wait(&wait
);
258 /* We won't need to wake ourself up */
259 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
261 mutex_lock(&rcu_gp_lock
);
264 * Move all waiters into our local queue.
266 urcu_move_waiters(&waiters
, &gp_waiters
);
268 mutex_lock(&rcu_registry_lock
);
270 if (cds_list_empty(®istry
))
274 * Wait for readers to observe original parity or be quiescent.
275 * wait_for_readers() can release and grab again rcu_registry_lock
278 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
281 * Must finish waiting for quiescent state for original parity
282 * before committing next urcu_qsbr_gp.ctr update to memory. Failure
283 * to do so could result in the writer waiting forever while new
284 * readers are always accessing data (no progress). Enforce
285 * compiler-order of load URCU_TLS(urcu_qsbr_reader).ctr before store
286 * to urcu_qsbr_gp.ctr.
291 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
292 * model easier to understand. It does not have a big performance impact
293 * anyway, given this is the write-side.
297 /* Switch parity: 0 -> 1, 1 -> 0 */
298 CMM_STORE_SHARED(urcu_qsbr_gp
.ctr
, urcu_qsbr_gp
.ctr
^ URCU_QSBR_GP_CTR
);
301 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
302 * quiescent state. Failure to do so could result in the writer
303 * waiting forever while new readers are always accessing data
304 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
305 * before load URCU_TLS(urcu_qsbr_reader).ctr.
310 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
311 * model easier to understand. It does not have a big performance impact
312 * anyway, given this is the write-side.
317 * Wait for readers to observe new parity or be quiescent.
318 * wait_for_readers() can release and grab again rcu_registry_lock
321 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
324 * Put quiescent reader list back into registry.
326 cds_list_splice(&qsreaders
, ®istry
);
328 mutex_unlock(&rcu_registry_lock
);
329 mutex_unlock(&rcu_gp_lock
);
330 urcu_wake_all_waiters(&waiters
);
333 * Finish waiting for reader threads before letting the old ptr being
337 urcu_qsbr_thread_online();
341 #else /* !(CAA_BITS_PER_LONG < 64) */
342 void urcu_qsbr_synchronize_rcu(void)
344 CDS_LIST_HEAD(qsreaders
);
345 unsigned long was_online
;
346 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
347 struct urcu_waiters waiters
;
349 was_online
= urcu_qsbr_read_ongoing();
352 * Mark the writer thread offline to make sure we don't wait for
353 * our own quiescent state. This allows using synchronize_rcu()
354 * in threads registered as readers.
357 urcu_qsbr_thread_offline();
362 * Add ourself to gp_waiters queue of threads awaiting to wait
363 * for a grace period. Proceed to perform the grace period only
364 * if we are the first thread added into the queue.
366 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
367 /* Not first in queue: will be awakened by another thread. */
368 urcu_adaptative_busy_wait(&wait
);
371 /* We won't need to wake ourself up */
372 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
374 mutex_lock(&rcu_gp_lock
);
377 * Move all waiters into our local queue.
379 urcu_move_waiters(&waiters
, &gp_waiters
);
381 mutex_lock(&rcu_registry_lock
);
383 if (cds_list_empty(®istry
))
386 /* Increment current G.P. */
387 CMM_STORE_SHARED(urcu_qsbr_gp
.ctr
, urcu_qsbr_gp
.ctr
+ URCU_QSBR_GP_CTR
);
390 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
391 * quiescent state. Failure to do so could result in the writer
392 * waiting forever while new readers are always accessing data
393 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
394 * before load URCU_TLS(urcu_qsbr_reader).ctr.
399 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
400 * model easier to understand. It does not have a big performance impact
401 * anyway, given this is the write-side.
406 * Wait for readers to observe new count of be quiescent.
407 * wait_for_readers() can release and grab again rcu_registry_lock
410 wait_for_readers(®istry
, NULL
, &qsreaders
);
413 * Put quiescent reader list back into registry.
415 cds_list_splice(&qsreaders
, ®istry
);
417 mutex_unlock(&rcu_registry_lock
);
418 mutex_unlock(&rcu_gp_lock
);
419 urcu_wake_all_waiters(&waiters
);
422 urcu_qsbr_thread_online();
426 #endif /* !(CAA_BITS_PER_LONG < 64) */
429 * library wrappers to be used by non-LGPL compatible source code.
432 void urcu_qsbr_read_lock(void)
434 _urcu_qsbr_read_lock();
437 void urcu_qsbr_read_unlock(void)
439 _urcu_qsbr_read_unlock();
442 int urcu_qsbr_read_ongoing(void)
444 return _urcu_qsbr_read_ongoing();
446 void rcu_read_ongoing_qsbr();
448 void urcu_qsbr_quiescent_state(void)
450 _urcu_qsbr_quiescent_state();
452 void rcu_quiescent_state_qsbr();
454 void urcu_qsbr_thread_offline(void)
456 _urcu_qsbr_thread_offline();
458 void rcu_thread_offline_qsbr();
460 void urcu_qsbr_thread_online(void)
462 _urcu_qsbr_thread_online();
465 void urcu_qsbr_register_thread(void)
467 URCU_TLS(urcu_qsbr_reader
).tid
= pthread_self();
468 urcu_posix_assert(URCU_TLS(urcu_qsbr_reader
).ctr
== 0);
470 mutex_lock(&rcu_registry_lock
);
471 urcu_posix_assert(!URCU_TLS(urcu_qsbr_reader
).registered
);
472 URCU_TLS(urcu_qsbr_reader
).registered
= 1;
473 cds_list_add(&URCU_TLS(urcu_qsbr_reader
).node
, ®istry
);
474 mutex_unlock(&rcu_registry_lock
);
475 _urcu_qsbr_thread_online();
478 void urcu_qsbr_unregister_thread(void)
481 * We have to make the thread offline otherwise we end up dealocking
482 * with a waiting writer.
484 _urcu_qsbr_thread_offline();
485 urcu_posix_assert(URCU_TLS(urcu_qsbr_reader
).registered
);
486 URCU_TLS(urcu_qsbr_reader
).registered
= 0;
487 mutex_lock(&rcu_registry_lock
);
488 cds_list_del(&URCU_TLS(urcu_qsbr_reader
).node
);
489 mutex_unlock(&rcu_registry_lock
);
492 void urcu_qsbr_exit(void)
495 * Assertion disabled because call_rcu threads are now rcu
496 * readers, and left running at exit.
497 * urcu_posix_assert(cds_list_empty(®istry));
499 urcu_call_rcu_exit();
502 DEFINE_RCU_FLAVOR(rcu_flavor
);
504 #include "urcu-call-rcu-impl.h"
505 #include "urcu-defer-impl.h"
506 #include "urcu-poll-impl.h"