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);
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
};
75 * Active attempts to check for reader Q.S. before calling futex().
77 #define RCU_QS_ACTIVE_ATTEMPTS 100
80 * Written to only by each individual reader. Read by both the reader and the
83 DEFINE_URCU_TLS(struct urcu_qsbr_reader
, urcu_qsbr_reader
);
85 static CDS_LIST_HEAD(registry
);
88 * Queue keeping threads awaiting to wait for a grace period. Contains
89 * struct gp_waiters_thread objects.
91 static DEFINE_URCU_WAIT_QUEUE(gp_waiters
);
93 static void mutex_lock(pthread_mutex_t
*mutex
)
97 #ifndef DISTRUST_SIGNALS_EXTREME
98 ret
= pthread_mutex_lock(mutex
);
101 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
102 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
103 if (ret
!= EBUSY
&& ret
!= EINTR
)
107 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
110 static void mutex_unlock(pthread_mutex_t
*mutex
)
114 ret
= pthread_mutex_unlock(mutex
);
120 * synchronize_rcu() waiting. Single thread.
122 static void wait_gp(void)
124 /* Read reader_gp before read futex */
126 while (uatomic_read(&urcu_qsbr_gp
.futex
) == -1) {
127 if (!futex_noasync(&urcu_qsbr_gp
.futex
, FUTEX_WAIT
, -1, NULL
, NULL
, 0)) {
129 * Prior queued wakeups queued by unrelated code
130 * using the same address can cause futex wait to
131 * return 0 even through the futex value is still
132 * -1 (spurious wakeups). Check the value again
133 * in user-space to validate whether it really
140 /* Value already changed. */
143 /* Retry if interrupted by signal. */
144 break; /* Get out of switch. Check again. */
146 /* Unexpected error. */
153 * Always called with rcu_registry lock held. Releases this lock between
154 * iterations and grabs it again. Holds the lock when it returns.
156 static void wait_for_readers(struct cds_list_head
*input_readers
,
157 struct cds_list_head
*cur_snap_readers
,
158 struct cds_list_head
*qsreaders
)
160 unsigned int wait_loops
= 0;
161 struct urcu_qsbr_reader
*index
, *tmp
;
164 * Wait for each thread URCU_TLS(urcu_qsbr_reader).ctr to either
165 * indicate quiescence (offline), or for them to observe the
166 * current urcu_qsbr_gp.ctr value.
169 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
171 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
172 uatomic_set(&urcu_qsbr_gp
.futex
, -1);
174 * Write futex before write waiting (the other side
175 * reads them in the opposite order).
178 cds_list_for_each_entry(index
, input_readers
, node
) {
179 _CMM_STORE_SHARED(index
->waiting
, 1);
181 /* Write futex before read reader_gp */
184 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
185 switch (urcu_qsbr_reader_state(&index
->ctr
)) {
186 case URCU_READER_ACTIVE_CURRENT
:
187 if (cur_snap_readers
) {
188 cds_list_move(&index
->node
,
193 case URCU_READER_INACTIVE
:
194 cds_list_move(&index
->node
, qsreaders
);
196 case URCU_READER_ACTIVE_OLD
:
198 * Old snapshot. Leaving node in
199 * input_readers will make us busy-loop
200 * until the snapshot becomes current or
201 * the reader becomes inactive.
207 if (cds_list_empty(input_readers
)) {
208 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
209 /* Read reader_gp before write futex */
211 uatomic_set(&urcu_qsbr_gp
.futex
, 0);
215 /* Temporarily unlock the registry lock. */
216 mutex_unlock(&rcu_registry_lock
);
217 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
220 #ifndef HAS_INCOHERENT_CACHES
222 #else /* #ifndef HAS_INCOHERENT_CACHES */
224 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
226 /* Re-lock the registry lock before the next loop. */
227 mutex_lock(&rcu_registry_lock
);
233 * Using a two-subphases algorithm for architectures with smaller than 64-bit
234 * long-size to ensure we do not encounter an overflow bug.
237 #if (CAA_BITS_PER_LONG < 64)
238 void urcu_qsbr_synchronize_rcu(void)
240 CDS_LIST_HEAD(cur_snap_readers
);
241 CDS_LIST_HEAD(qsreaders
);
242 unsigned long was_online
;
243 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
244 struct urcu_waiters waiters
;
246 was_online
= urcu_qsbr_read_ongoing();
248 /* All threads should read qparity before accessing data structure
249 * where new ptr points to. In the "then" case, rcu_thread_offline
250 * includes a memory barrier.
252 * Mark the writer thread offline to make sure we don't wait for
253 * our own quiescent state. This allows using synchronize_rcu()
254 * in threads registered as readers.
257 urcu_qsbr_thread_offline();
262 * Add ourself to gp_waiters queue of threads awaiting to wait
263 * for a grace period. Proceed to perform the grace period only
264 * if we are the first thread added into the queue.
266 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
267 /* Not first in queue: will be awakened by another thread. */
268 urcu_adaptative_busy_wait(&wait
);
271 /* We won't need to wake ourself up */
272 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
274 mutex_lock(&rcu_gp_lock
);
277 * Move all waiters into our local queue.
279 urcu_move_waiters(&waiters
, &gp_waiters
);
281 mutex_lock(&rcu_registry_lock
);
283 if (cds_list_empty(®istry
))
287 * Wait for readers to observe original parity or be quiescent.
288 * wait_for_readers() can release and grab again rcu_registry_lock
291 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
294 * Must finish waiting for quiescent state for original parity
295 * before committing next urcu_qsbr_gp.ctr update to memory. Failure
296 * to do so could result in the writer waiting forever while new
297 * readers are always accessing data (no progress). Enforce
298 * compiler-order of load URCU_TLS(urcu_qsbr_reader).ctr before store
299 * to urcu_qsbr_gp.ctr.
304 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
305 * model easier to understand. It does not have a big performance impact
306 * anyway, given this is the write-side.
310 /* Switch parity: 0 -> 1, 1 -> 0 */
311 CMM_STORE_SHARED(urcu_qsbr_gp
.ctr
, urcu_qsbr_gp
.ctr
^ URCU_QSBR_GP_CTR
);
314 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
315 * quiescent state. Failure to do so could result in the writer
316 * waiting forever while new readers are always accessing data
317 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
318 * before load URCU_TLS(urcu_qsbr_reader).ctr.
323 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
324 * model easier to understand. It does not have a big performance impact
325 * anyway, given this is the write-side.
330 * Wait for readers to observe new parity or be quiescent.
331 * wait_for_readers() can release and grab again rcu_registry_lock
334 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
337 * Put quiescent reader list back into registry.
339 cds_list_splice(&qsreaders
, ®istry
);
341 mutex_unlock(&rcu_registry_lock
);
342 mutex_unlock(&rcu_gp_lock
);
343 urcu_wake_all_waiters(&waiters
);
346 * Finish waiting for reader threads before letting the old ptr being
350 urcu_qsbr_thread_online();
354 #else /* !(CAA_BITS_PER_LONG < 64) */
355 void urcu_qsbr_synchronize_rcu(void)
357 CDS_LIST_HEAD(qsreaders
);
358 unsigned long was_online
;
359 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
360 struct urcu_waiters waiters
;
362 was_online
= urcu_qsbr_read_ongoing();
365 * Mark the writer thread offline to make sure we don't wait for
366 * our own quiescent state. This allows using synchronize_rcu()
367 * in threads registered as readers.
370 urcu_qsbr_thread_offline();
375 * Add ourself to gp_waiters queue of threads awaiting to wait
376 * for a grace period. Proceed to perform the grace period only
377 * if we are the first thread added into the queue.
379 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
380 /* Not first in queue: will be awakened by another thread. */
381 urcu_adaptative_busy_wait(&wait
);
384 /* We won't need to wake ourself up */
385 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
387 mutex_lock(&rcu_gp_lock
);
390 * Move all waiters into our local queue.
392 urcu_move_waiters(&waiters
, &gp_waiters
);
394 mutex_lock(&rcu_registry_lock
);
396 if (cds_list_empty(®istry
))
399 /* Increment current G.P. */
400 CMM_STORE_SHARED(urcu_qsbr_gp
.ctr
, urcu_qsbr_gp
.ctr
+ URCU_QSBR_GP_CTR
);
403 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
404 * quiescent state. Failure to do so could result in the writer
405 * waiting forever while new readers are always accessing data
406 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
407 * before load URCU_TLS(urcu_qsbr_reader).ctr.
412 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
413 * model easier to understand. It does not have a big performance impact
414 * anyway, given this is the write-side.
419 * Wait for readers to observe new count of be quiescent.
420 * wait_for_readers() can release and grab again rcu_registry_lock
423 wait_for_readers(®istry
, NULL
, &qsreaders
);
426 * Put quiescent reader list back into registry.
428 cds_list_splice(&qsreaders
, ®istry
);
430 mutex_unlock(&rcu_registry_lock
);
431 mutex_unlock(&rcu_gp_lock
);
432 urcu_wake_all_waiters(&waiters
);
435 urcu_qsbr_thread_online();
439 #endif /* !(CAA_BITS_PER_LONG < 64) */
442 * library wrappers to be used by non-LGPL compatible source code.
445 void urcu_qsbr_read_lock(void)
447 _urcu_qsbr_read_lock();
450 void urcu_qsbr_read_unlock(void)
452 _urcu_qsbr_read_unlock();
455 int urcu_qsbr_read_ongoing(void)
457 return _urcu_qsbr_read_ongoing();
459 void rcu_read_ongoing_qsbr();
461 void urcu_qsbr_quiescent_state(void)
463 _urcu_qsbr_quiescent_state();
465 void rcu_quiescent_state_qsbr();
467 void urcu_qsbr_thread_offline(void)
469 _urcu_qsbr_thread_offline();
471 void rcu_thread_offline_qsbr();
473 void urcu_qsbr_thread_online(void)
475 _urcu_qsbr_thread_online();
478 void urcu_qsbr_register_thread(void)
480 URCU_TLS(urcu_qsbr_reader
).tid
= pthread_self();
481 urcu_posix_assert(URCU_TLS(urcu_qsbr_reader
).ctr
== 0);
483 mutex_lock(&rcu_registry_lock
);
484 urcu_posix_assert(!URCU_TLS(urcu_qsbr_reader
).registered
);
485 URCU_TLS(urcu_qsbr_reader
).registered
= 1;
486 cds_list_add(&URCU_TLS(urcu_qsbr_reader
).node
, ®istry
);
487 mutex_unlock(&rcu_registry_lock
);
488 _urcu_qsbr_thread_online();
491 void urcu_qsbr_unregister_thread(void)
494 * We have to make the thread offline otherwise we end up dealocking
495 * with a waiting writer.
497 _urcu_qsbr_thread_offline();
498 urcu_posix_assert(URCU_TLS(urcu_qsbr_reader
).registered
);
499 URCU_TLS(urcu_qsbr_reader
).registered
= 0;
500 mutex_lock(&rcu_registry_lock
);
501 cds_list_del(&URCU_TLS(urcu_qsbr_reader
).node
);
502 mutex_unlock(&rcu_registry_lock
);
505 void urcu_qsbr_exit(void)
508 * Assertion disabled because call_rcu threads are now rcu
509 * readers, and left running at exit.
510 * urcu_posix_assert(cds_list_empty(®istry));
514 DEFINE_RCU_FLAVOR(rcu_flavor
);
516 #include "urcu-call-rcu-impl.h"
517 #include "urcu-defer-impl.h"