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
37 #include <urcu/assert.h>
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
};
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
);
86 static CDS_LIST_HEAD(registry
);
89 * Queue keeping threads awaiting to wait for a grace period. Contains
90 * struct gp_waiters_thread objects.
92 static DEFINE_URCU_WAIT_QUEUE(gp_waiters
);
94 static void mutex_lock(pthread_mutex_t
*mutex
)
98 #ifndef DISTRUST_SIGNALS_EXTREME
99 ret
= pthread_mutex_lock(mutex
);
102 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
103 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
104 if (ret
!= EBUSY
&& ret
!= EINTR
)
108 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
111 static void mutex_unlock(pthread_mutex_t
*mutex
)
115 ret
= pthread_mutex_unlock(mutex
);
121 * synchronize_rcu() waiting. Single thread.
123 static void wait_gp(void)
125 /* Read reader_gp before read futex */
127 while (uatomic_read(&urcu_qsbr_gp
.futex
) == -1) {
128 if (!futex_noasync(&urcu_qsbr_gp
.futex
, FUTEX_WAIT
, -1, NULL
, NULL
, 0)) {
130 * Prior queued wakeups queued by unrelated code
131 * using the same address can cause futex wait to
132 * return 0 even through the futex value is still
133 * -1 (spurious wakeups). Check the value again
134 * in user-space to validate whether it really
141 /* Value already changed. */
144 /* Retry if interrupted by signal. */
145 break; /* Get out of switch. Check again. */
147 /* Unexpected error. */
154 * Always called with rcu_registry lock held. Releases this lock between
155 * iterations and grabs it again. Holds the lock when it returns.
157 static void wait_for_readers(struct cds_list_head
*input_readers
,
158 struct cds_list_head
*cur_snap_readers
,
159 struct cds_list_head
*qsreaders
)
161 unsigned int wait_loops
= 0;
162 struct urcu_qsbr_reader
*index
, *tmp
;
165 * Wait for each thread URCU_TLS(urcu_qsbr_reader).ctr to either
166 * indicate quiescence (offline), or for them to observe the
167 * current urcu_qsbr_gp.ctr value.
170 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
172 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
173 uatomic_set(&urcu_qsbr_gp
.futex
, -1);
175 * Write futex before write waiting (the other side
176 * reads them in the opposite order).
179 cds_list_for_each_entry(index
, input_readers
, node
) {
180 _CMM_STORE_SHARED(index
->waiting
, 1);
182 /* Write futex before read reader_gp */
185 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
186 switch (urcu_qsbr_reader_state(&index
->ctr
)) {
187 case URCU_READER_ACTIVE_CURRENT
:
188 if (cur_snap_readers
) {
189 cds_list_move(&index
->node
,
194 case URCU_READER_INACTIVE
:
195 cds_list_move(&index
->node
, qsreaders
);
197 case URCU_READER_ACTIVE_OLD
:
199 * Old snapshot. Leaving node in
200 * input_readers will make us busy-loop
201 * until the snapshot becomes current or
202 * the reader becomes inactive.
208 if (cds_list_empty(input_readers
)) {
209 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
210 /* Read reader_gp before write futex */
212 uatomic_set(&urcu_qsbr_gp
.futex
, 0);
216 /* Temporarily unlock the registry lock. */
217 mutex_unlock(&rcu_registry_lock
);
218 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
221 #ifndef HAS_INCOHERENT_CACHES
223 #else /* #ifndef HAS_INCOHERENT_CACHES */
225 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
227 /* Re-lock the registry lock before the next loop. */
228 mutex_lock(&rcu_registry_lock
);
234 * Using a two-subphases algorithm for architectures with smaller than 64-bit
235 * long-size to ensure we do not encounter an overflow bug.
238 #if (CAA_BITS_PER_LONG < 64)
239 void urcu_qsbr_synchronize_rcu(void)
241 CDS_LIST_HEAD(cur_snap_readers
);
242 CDS_LIST_HEAD(qsreaders
);
243 unsigned long was_online
;
244 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
245 struct urcu_waiters waiters
;
247 was_online
= urcu_qsbr_read_ongoing();
249 /* All threads should read qparity before accessing data structure
250 * where new ptr points to. In the "then" case, rcu_thread_offline
251 * includes a memory barrier.
253 * Mark the writer thread offline to make sure we don't wait for
254 * our own quiescent state. This allows using synchronize_rcu()
255 * in threads registered as readers.
258 urcu_qsbr_thread_offline();
263 * Add ourself to gp_waiters queue of threads awaiting to wait
264 * for a grace period. Proceed to perform the grace period only
265 * if we are the first thread added into the queue.
267 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
268 /* Not first in queue: will be awakened by another thread. */
269 urcu_adaptative_busy_wait(&wait
);
272 /* We won't need to wake ourself up */
273 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
275 mutex_lock(&rcu_gp_lock
);
278 * Move all waiters into our local queue.
280 urcu_move_waiters(&waiters
, &gp_waiters
);
282 mutex_lock(&rcu_registry_lock
);
284 if (cds_list_empty(®istry
))
288 * Wait for readers to observe original parity or be quiescent.
289 * wait_for_readers() can release and grab again rcu_registry_lock
292 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
295 * Must finish waiting for quiescent state for original parity
296 * before committing next urcu_qsbr_gp.ctr update to memory. Failure
297 * to do so could result in the writer waiting forever while new
298 * readers are always accessing data (no progress). Enforce
299 * compiler-order of load URCU_TLS(urcu_qsbr_reader).ctr before store
300 * to urcu_qsbr_gp.ctr.
305 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
306 * model easier to understand. It does not have a big performance impact
307 * anyway, given this is the write-side.
311 /* Switch parity: 0 -> 1, 1 -> 0 */
312 CMM_STORE_SHARED(urcu_qsbr_gp
.ctr
, urcu_qsbr_gp
.ctr
^ URCU_QSBR_GP_CTR
);
315 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
316 * quiescent state. Failure to do so could result in the writer
317 * waiting forever while new readers are always accessing data
318 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
319 * before load URCU_TLS(urcu_qsbr_reader).ctr.
324 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
325 * model easier to understand. It does not have a big performance impact
326 * anyway, given this is the write-side.
331 * Wait for readers to observe new parity or be quiescent.
332 * wait_for_readers() can release and grab again rcu_registry_lock
335 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
338 * Put quiescent reader list back into registry.
340 cds_list_splice(&qsreaders
, ®istry
);
342 mutex_unlock(&rcu_registry_lock
);
343 mutex_unlock(&rcu_gp_lock
);
344 urcu_wake_all_waiters(&waiters
);
347 * Finish waiting for reader threads before letting the old ptr being
351 urcu_qsbr_thread_online();
355 #else /* !(CAA_BITS_PER_LONG < 64) */
356 void urcu_qsbr_synchronize_rcu(void)
358 CDS_LIST_HEAD(qsreaders
);
359 unsigned long was_online
;
360 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
361 struct urcu_waiters waiters
;
363 was_online
= urcu_qsbr_read_ongoing();
366 * Mark the writer thread offline to make sure we don't wait for
367 * our own quiescent state. This allows using synchronize_rcu()
368 * in threads registered as readers.
371 urcu_qsbr_thread_offline();
376 * Add ourself to gp_waiters queue of threads awaiting to wait
377 * for a grace period. Proceed to perform the grace period only
378 * if we are the first thread added into the queue.
380 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
381 /* Not first in queue: will be awakened by another thread. */
382 urcu_adaptative_busy_wait(&wait
);
385 /* We won't need to wake ourself up */
386 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
388 mutex_lock(&rcu_gp_lock
);
391 * Move all waiters into our local queue.
393 urcu_move_waiters(&waiters
, &gp_waiters
);
395 mutex_lock(&rcu_registry_lock
);
397 if (cds_list_empty(®istry
))
400 /* Increment current G.P. */
401 CMM_STORE_SHARED(urcu_qsbr_gp
.ctr
, urcu_qsbr_gp
.ctr
+ URCU_QSBR_GP_CTR
);
404 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
405 * quiescent state. Failure to do so could result in the writer
406 * waiting forever while new readers are always accessing data
407 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
408 * before load URCU_TLS(urcu_qsbr_reader).ctr.
413 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
414 * model easier to understand. It does not have a big performance impact
415 * anyway, given this is the write-side.
420 * Wait for readers to observe new count of be quiescent.
421 * wait_for_readers() can release and grab again rcu_registry_lock
424 wait_for_readers(®istry
, NULL
, &qsreaders
);
427 * Put quiescent reader list back into registry.
429 cds_list_splice(&qsreaders
, ®istry
);
431 mutex_unlock(&rcu_registry_lock
);
432 mutex_unlock(&rcu_gp_lock
);
433 urcu_wake_all_waiters(&waiters
);
436 urcu_qsbr_thread_online();
440 #endif /* !(CAA_BITS_PER_LONG < 64) */
443 * library wrappers to be used by non-LGPL compatible source code.
446 void urcu_qsbr_read_lock(void)
448 _urcu_qsbr_read_lock();
451 void urcu_qsbr_read_unlock(void)
453 _urcu_qsbr_read_unlock();
456 int urcu_qsbr_read_ongoing(void)
458 return _urcu_qsbr_read_ongoing();
460 void rcu_read_ongoing_qsbr();
462 void urcu_qsbr_quiescent_state(void)
464 _urcu_qsbr_quiescent_state();
466 void rcu_quiescent_state_qsbr();
468 void urcu_qsbr_thread_offline(void)
470 _urcu_qsbr_thread_offline();
472 void rcu_thread_offline_qsbr();
474 void urcu_qsbr_thread_online(void)
476 _urcu_qsbr_thread_online();
479 void urcu_qsbr_register_thread(void)
481 URCU_TLS(urcu_qsbr_reader
).tid
= pthread_self();
482 urcu_posix_assert(URCU_TLS(urcu_qsbr_reader
).ctr
== 0);
484 mutex_lock(&rcu_registry_lock
);
485 urcu_posix_assert(!URCU_TLS(urcu_qsbr_reader
).registered
);
486 URCU_TLS(urcu_qsbr_reader
).registered
= 1;
487 cds_list_add(&URCU_TLS(urcu_qsbr_reader
).node
, ®istry
);
488 mutex_unlock(&rcu_registry_lock
);
489 _urcu_qsbr_thread_online();
492 void urcu_qsbr_unregister_thread(void)
495 * We have to make the thread offline otherwise we end up dealocking
496 * with a waiting writer.
498 _urcu_qsbr_thread_offline();
499 urcu_posix_assert(URCU_TLS(urcu_qsbr_reader
).registered
);
500 URCU_TLS(urcu_qsbr_reader
).registered
= 0;
501 mutex_lock(&rcu_registry_lock
);
502 cds_list_del(&URCU_TLS(urcu_qsbr_reader
).node
);
503 mutex_unlock(&rcu_registry_lock
);
506 void urcu_qsbr_exit(void)
509 * Assertion disabled because call_rcu threads are now rcu
510 * readers, and left running at exit.
511 * urcu_posix_assert(cds_list_empty(®istry));
513 urcu_call_rcu_exit();
516 DEFINE_RCU_FLAVOR(rcu_flavor
);
518 #include "urcu-call-rcu-impl.h"
519 #include "urcu-defer-impl.h"
520 #include "urcu-poll-impl.h"