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.
37 #include <urcu/wfcqueue.h>
38 #include <urcu/map/urcu-qsbr.h>
39 #define BUILD_QSBR_LIB
40 #include <urcu/static/urcu-qsbr.h>
41 #include <urcu/pointer.h>
42 #include <urcu/tls-compat.h>
45 #include "urcu-wait.h"
46 #include "urcu-utils.h"
49 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
51 #include <urcu/urcu-qsbr.h>
54 void __attribute__((destructor
)) urcu_qsbr_exit(void);
57 * rcu_gp_lock ensures mutual exclusion between threads calling
60 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
62 * rcu_registry_lock ensures mutual exclusion between threads
63 * registering and unregistering themselves to/from the registry, and
64 * with threads reading that registry from synchronize_rcu(). However,
65 * this lock is not held all the way through the completion of awaiting
66 * for the grace period. It is sporadically released between iterations
68 * rcu_registry_lock may nest inside rcu_gp_lock.
70 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
71 struct urcu_gp urcu_qsbr_gp
= { .ctr
= URCU_QSBR_GP_ONLINE
};
72 URCU_ATTR_ALIAS("urcu_qsbr_gp") extern struct urcu_gp rcu_gp_qsbr
;
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
);
84 DEFINE_URCU_TLS_ALIAS(struct urcu_qsbr_reader
, urcu_qsbr_reader
, rcu_reader_qsbr
);
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 if (uatomic_read(&urcu_qsbr_gp
.futex
) != -1)
129 while (futex_noasync(&urcu_qsbr_gp
.futex
, FUTEX_WAIT
, -1,
133 /* Value already changed. */
136 /* Retry if interrupted by signal. */
137 break; /* Get out of switch. */
139 /* Unexpected error. */
146 * Always called with rcu_registry lock held. Releases this lock between
147 * iterations and grabs it again. Holds the lock when it returns.
149 static void wait_for_readers(struct cds_list_head
*input_readers
,
150 struct cds_list_head
*cur_snap_readers
,
151 struct cds_list_head
*qsreaders
)
153 unsigned int wait_loops
= 0;
154 struct urcu_qsbr_reader
*index
, *tmp
;
157 * Wait for each thread URCU_TLS(urcu_qsbr_reader).ctr to either
158 * indicate quiescence (offline), or for them to observe the
159 * current urcu_qsbr_gp.ctr value.
162 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
164 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
165 uatomic_set(&urcu_qsbr_gp
.futex
, -1);
167 * Write futex before write waiting (the other side
168 * reads them in the opposite order).
171 cds_list_for_each_entry(index
, input_readers
, node
) {
172 _CMM_STORE_SHARED(index
->waiting
, 1);
174 /* Write futex before read reader_gp */
177 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
178 switch (urcu_qsbr_reader_state(&index
->ctr
)) {
179 case URCU_READER_ACTIVE_CURRENT
:
180 if (cur_snap_readers
) {
181 cds_list_move(&index
->node
,
186 case URCU_READER_INACTIVE
:
187 cds_list_move(&index
->node
, qsreaders
);
189 case URCU_READER_ACTIVE_OLD
:
191 * Old snapshot. Leaving node in
192 * input_readers will make us busy-loop
193 * until the snapshot becomes current or
194 * the reader becomes inactive.
200 if (cds_list_empty(input_readers
)) {
201 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
202 /* Read reader_gp before write futex */
204 uatomic_set(&urcu_qsbr_gp
.futex
, 0);
208 /* Temporarily unlock the registry lock. */
209 mutex_unlock(&rcu_registry_lock
);
210 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
213 #ifndef HAS_INCOHERENT_CACHES
215 #else /* #ifndef HAS_INCOHERENT_CACHES */
217 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
219 /* Re-lock the registry lock before the next loop. */
220 mutex_lock(&rcu_registry_lock
);
226 * Using a two-subphases algorithm for architectures with smaller than 64-bit
227 * long-size to ensure we do not encounter an overflow bug.
230 #if (CAA_BITS_PER_LONG < 64)
231 void urcu_qsbr_synchronize_rcu(void)
233 CDS_LIST_HEAD(cur_snap_readers
);
234 CDS_LIST_HEAD(qsreaders
);
235 unsigned long was_online
;
236 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
237 struct urcu_waiters waiters
;
239 was_online
= urcu_qsbr_read_ongoing();
241 /* All threads should read qparity before accessing data structure
242 * where new ptr points to. In the "then" case, rcu_thread_offline
243 * includes a memory barrier.
245 * Mark the writer thread offline to make sure we don't wait for
246 * our own quiescent state. This allows using synchronize_rcu()
247 * in threads registered as readers.
250 urcu_qsbr_thread_offline();
255 * Add ourself to gp_waiters queue of threads awaiting to wait
256 * for a grace period. Proceed to perform the grace period only
257 * if we are the first thread added into the queue.
259 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
260 /* Not first in queue: will be awakened by another thread. */
261 urcu_adaptative_busy_wait(&wait
);
264 /* We won't need to wake ourself up */
265 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
267 mutex_lock(&rcu_gp_lock
);
270 * Move all waiters into our local queue.
272 urcu_move_waiters(&waiters
, &gp_waiters
);
274 mutex_lock(&rcu_registry_lock
);
276 if (cds_list_empty(®istry
))
280 * Wait for readers to observe original parity or be quiescent.
281 * wait_for_readers() can release and grab again rcu_registry_lock
284 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
287 * Must finish waiting for quiescent state for original parity
288 * before committing next urcu_qsbr_gp.ctr update to memory. Failure
289 * to do so could result in the writer waiting forever while new
290 * readers are always accessing data (no progress). Enforce
291 * compiler-order of load URCU_TLS(urcu_qsbr_reader).ctr before store
292 * to urcu_qsbr_gp.ctr.
297 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
298 * model easier to understand. It does not have a big performance impact
299 * anyway, given this is the write-side.
303 /* Switch parity: 0 -> 1, 1 -> 0 */
304 CMM_STORE_SHARED(urcu_qsbr_gp
.ctr
, urcu_qsbr_gp
.ctr
^ URCU_QSBR_GP_CTR
);
307 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
308 * quiescent state. Failure to do so could result in the writer
309 * waiting forever while new readers are always accessing data
310 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
311 * before load URCU_TLS(urcu_qsbr_reader).ctr.
316 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
317 * model easier to understand. It does not have a big performance impact
318 * anyway, given this is the write-side.
323 * Wait for readers to observe new parity or be quiescent.
324 * wait_for_readers() can release and grab again rcu_registry_lock
327 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
330 * Put quiescent reader list back into registry.
332 cds_list_splice(&qsreaders
, ®istry
);
334 mutex_unlock(&rcu_registry_lock
);
335 mutex_unlock(&rcu_gp_lock
);
336 urcu_wake_all_waiters(&waiters
);
339 * Finish waiting for reader threads before letting the old ptr being
343 urcu_qsbr_thread_online();
347 #else /* !(CAA_BITS_PER_LONG < 64) */
348 void urcu_qsbr_synchronize_rcu(void)
350 CDS_LIST_HEAD(qsreaders
);
351 unsigned long was_online
;
352 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
353 struct urcu_waiters waiters
;
355 was_online
= urcu_qsbr_read_ongoing();
358 * Mark the writer thread offline to make sure we don't wait for
359 * our own quiescent state. This allows using synchronize_rcu()
360 * in threads registered as readers.
363 urcu_qsbr_thread_offline();
368 * Add ourself to gp_waiters queue of threads awaiting to wait
369 * for a grace period. Proceed to perform the grace period only
370 * if we are the first thread added into the queue.
372 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
373 /* Not first in queue: will be awakened by another thread. */
374 urcu_adaptative_busy_wait(&wait
);
377 /* We won't need to wake ourself up */
378 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
380 mutex_lock(&rcu_gp_lock
);
383 * Move all waiters into our local queue.
385 urcu_move_waiters(&waiters
, &gp_waiters
);
387 mutex_lock(&rcu_registry_lock
);
389 if (cds_list_empty(®istry
))
392 /* Increment current G.P. */
393 CMM_STORE_SHARED(urcu_qsbr_gp
.ctr
, urcu_qsbr_gp
.ctr
+ URCU_QSBR_GP_CTR
);
396 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
397 * quiescent state. Failure to do so could result in the writer
398 * waiting forever while new readers are always accessing data
399 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
400 * before load URCU_TLS(urcu_qsbr_reader).ctr.
405 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
406 * model easier to understand. It does not have a big performance impact
407 * anyway, given this is the write-side.
412 * Wait for readers to observe new count of be quiescent.
413 * wait_for_readers() can release and grab again rcu_registry_lock
416 wait_for_readers(®istry
, NULL
, &qsreaders
);
419 * Put quiescent reader list back into registry.
421 cds_list_splice(&qsreaders
, ®istry
);
423 mutex_unlock(&rcu_registry_lock
);
424 mutex_unlock(&rcu_gp_lock
);
425 urcu_wake_all_waiters(&waiters
);
428 urcu_qsbr_thread_online();
432 #endif /* !(CAA_BITS_PER_LONG < 64) */
433 URCU_ATTR_ALIAS("urcu_qsbr_synchronize_rcu")
434 void synchronize_rcu_qsbr();
437 * library wrappers to be used by non-LGPL compatible source code.
440 void urcu_qsbr_read_lock(void)
442 _urcu_qsbr_read_lock();
444 URCU_ATTR_ALIAS("urcu_qsbr_read_lock") void rcu_read_lock_qsbr();
446 void urcu_qsbr_read_unlock(void)
448 _urcu_qsbr_read_unlock();
450 URCU_ATTR_ALIAS("urcu_qsbr_read_unlock") void rcu_read_unlock_qsbr();
452 int urcu_qsbr_read_ongoing(void)
454 return _urcu_qsbr_read_ongoing();
456 URCU_ATTR_ALIAS("urcu_qsbr_read_ongoing")
457 void rcu_read_ongoing_qsbr();
459 void urcu_qsbr_quiescent_state(void)
461 _urcu_qsbr_quiescent_state();
463 URCU_ATTR_ALIAS("urcu_qsbr_quiescent_state")
464 void rcu_quiescent_state_qsbr();
466 void urcu_qsbr_thread_offline(void)
468 _urcu_qsbr_thread_offline();
470 URCU_ATTR_ALIAS("urcu_qsbr_thread_offline")
471 void rcu_thread_offline_qsbr();
473 void urcu_qsbr_thread_online(void)
475 _urcu_qsbr_thread_online();
477 URCU_ATTR_ALIAS("urcu_qsbr_thread_online")
478 void rcu_thread_online_qsbr();
480 void urcu_qsbr_register_thread(void)
482 URCU_TLS(urcu_qsbr_reader
).tid
= pthread_self();
483 assert(URCU_TLS(urcu_qsbr_reader
).ctr
== 0);
485 mutex_lock(&rcu_registry_lock
);
486 assert(!URCU_TLS(urcu_qsbr_reader
).registered
);
487 URCU_TLS(urcu_qsbr_reader
).registered
= 1;
488 cds_list_add(&URCU_TLS(urcu_qsbr_reader
).node
, ®istry
);
489 mutex_unlock(&rcu_registry_lock
);
490 _urcu_qsbr_thread_online();
492 URCU_ATTR_ALIAS("urcu_qsbr_register_thread")
493 void rcu_register_thread_qsbr();
495 void urcu_qsbr_unregister_thread(void)
498 * We have to make the thread offline otherwise we end up dealocking
499 * with a waiting writer.
501 _urcu_qsbr_thread_offline();
502 assert(URCU_TLS(urcu_qsbr_reader
).registered
);
503 URCU_TLS(urcu_qsbr_reader
).registered
= 0;
504 mutex_lock(&rcu_registry_lock
);
505 cds_list_del(&URCU_TLS(urcu_qsbr_reader
).node
);
506 mutex_unlock(&rcu_registry_lock
);
508 URCU_ATTR_ALIAS("urcu_qsbr_unregister_thread")
509 void rcu_unregister_thread_qsbr();
511 void urcu_qsbr_exit(void)
514 * Assertion disabled because call_rcu threads are now rcu
515 * readers, and left running at exit.
516 * assert(cds_list_empty(®istry));
519 URCU_ATTR_ALIAS("urcu_qsbr_exit") void rcu_exit_qsbr();
521 DEFINE_RCU_FLAVOR(rcu_flavor
);
522 DEFINE_RCU_FLAVOR_ALIAS(rcu_flavor
, alias_rcu_flavor
);
524 #include "urcu-call-rcu-impl.h"
525 #include "urcu-defer-impl.h"