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.
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"
48 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
50 #include "urcu-qsbr.h"
53 void __attribute__((destructor
)) rcu_exit(void);
56 * rcu_gp_lock ensures mutual exclusion between threads calling
59 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
61 * rcu_registry_lock ensures mutual exclusion between threads
62 * registering and unregistering themselves to/from the registry, and
63 * with threads reading that registry from synchronize_rcu(). However,
64 * this lock is not held all the way through the completion of awaiting
65 * for the grace period. It is sporadically released between iterations
67 * rcu_registry_lock may nest inside rcu_gp_lock.
69 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
70 struct rcu_gp rcu_gp
= { .ctr
= RCU_GP_ONLINE
};
73 * Active attempts to check for reader Q.S. before calling futex().
75 #define RCU_QS_ACTIVE_ATTEMPTS 100
78 * Written to only by each individual reader. Read by both the reader and the
81 __DEFINE_URCU_TLS_GLOBAL(struct rcu_reader
, rcu_reader
);
84 unsigned int rcu_yield_active
;
85 __DEFINE_URCU_TLS_GLOBAL(unsigned int, rcu_rand_yield
);
88 static CDS_LIST_HEAD(registry
);
91 * Queue keeping threads awaiting to wait for a grace period. Contains
92 * struct gp_waiters_thread objects.
94 static DEFINE_URCU_WAIT_QUEUE(gp_waiters
);
96 static void mutex_lock(pthread_mutex_t
*mutex
)
100 #ifndef DISTRUST_SIGNALS_EXTREME
101 ret
= pthread_mutex_lock(mutex
);
104 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
105 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
106 if (ret
!= EBUSY
&& ret
!= EINTR
)
110 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
113 static void mutex_unlock(pthread_mutex_t
*mutex
)
117 ret
= pthread_mutex_unlock(mutex
);
123 * synchronize_rcu() waiting. Single thread.
125 static void wait_gp(void)
127 /* Read reader_gp before read futex */
129 if (uatomic_read(&rcu_gp
.futex
) == -1)
130 futex_noasync(&rcu_gp
.futex
, FUTEX_WAIT
, -1,
135 * Always called with rcu_registry lock held. Releases this lock between
136 * iterations and grabs it again. Holds the lock when it returns.
138 static void wait_for_readers(struct cds_list_head
*input_readers
,
139 struct cds_list_head
*cur_snap_readers
,
140 struct cds_list_head
*qsreaders
)
142 unsigned int wait_loops
= 0;
143 struct rcu_reader
*index
, *tmp
;
146 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
147 * indicate quiescence (offline), or for them to observe the
148 * current rcu_gp.ctr value.
151 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
153 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
154 uatomic_set(&rcu_gp
.futex
, -1);
156 * Write futex before write waiting (the other side
157 * reads them in the opposite order).
160 cds_list_for_each_entry(index
, input_readers
, node
) {
161 _CMM_STORE_SHARED(index
->waiting
, 1);
163 /* Write futex before read reader_gp */
166 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
167 switch (rcu_reader_state(&index
->ctr
)) {
168 case RCU_READER_ACTIVE_CURRENT
:
169 if (cur_snap_readers
) {
170 cds_list_move(&index
->node
,
175 case RCU_READER_INACTIVE
:
176 cds_list_move(&index
->node
, qsreaders
);
178 case RCU_READER_ACTIVE_OLD
:
180 * Old snapshot. Leaving node in
181 * input_readers will make us busy-loop
182 * until the snapshot becomes current or
183 * the reader becomes inactive.
189 if (cds_list_empty(input_readers
)) {
190 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
191 /* Read reader_gp before write futex */
193 uatomic_set(&rcu_gp
.futex
, 0);
197 /* Temporarily unlock the registry lock. */
198 mutex_unlock(&rcu_registry_lock
);
199 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
202 #ifndef HAS_INCOHERENT_CACHES
204 #else /* #ifndef HAS_INCOHERENT_CACHES */
206 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
208 /* Re-lock the registry lock before the next loop. */
209 mutex_lock(&rcu_registry_lock
);
215 * Using a two-subphases algorithm for architectures with smaller than 64-bit
216 * long-size to ensure we do not encounter an overflow bug.
219 #if (CAA_BITS_PER_LONG < 64)
220 void synchronize_rcu(void)
222 CDS_LIST_HEAD(cur_snap_readers
);
223 CDS_LIST_HEAD(qsreaders
);
224 unsigned long was_online
;
225 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
226 struct urcu_waiters waiters
;
228 was_online
= rcu_read_ongoing();
230 /* All threads should read qparity before accessing data structure
231 * where new ptr points to. In the "then" case, rcu_thread_offline
232 * includes a memory barrier.
234 * Mark the writer thread offline to make sure we don't wait for
235 * our own quiescent state. This allows using synchronize_rcu()
236 * in threads registered as readers.
239 rcu_thread_offline();
244 * Add ourself to gp_waiters queue of threads awaiting to wait
245 * for a grace period. Proceed to perform the grace period only
246 * if we are the first thread added into the queue.
248 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
249 /* Not first in queue: will be awakened by another thread. */
250 urcu_adaptative_busy_wait(&wait
);
253 /* We won't need to wake ourself up */
254 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
256 mutex_lock(&rcu_gp_lock
);
259 * Move all waiters into our local queue.
261 urcu_move_waiters(&waiters
, &gp_waiters
);
263 mutex_lock(&rcu_registry_lock
);
265 if (cds_list_empty(®istry
))
269 * Wait for readers to observe original parity or be quiescent.
270 * wait_for_readers() can release and grab again rcu_registry_lock
273 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
276 * Must finish waiting for quiescent state for original parity
277 * before committing next rcu_gp.ctr update to memory. Failure
278 * to do so could result in the writer waiting forever while new
279 * readers are always accessing data (no progress). Enforce
280 * compiler-order of load URCU_TLS(rcu_reader).ctr before store
286 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
287 * model easier to understand. It does not have a big performance impact
288 * anyway, given this is the write-side.
292 /* Switch parity: 0 -> 1, 1 -> 0 */
293 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ RCU_GP_CTR
);
296 * Must commit rcu_gp.ctr update to memory before waiting for
297 * quiescent state. Failure to do so could result in the writer
298 * waiting forever while new readers are always accessing data
299 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
300 * before load URCU_TLS(rcu_reader).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.
312 * Wait for readers to observe new parity or be quiescent.
313 * wait_for_readers() can release and grab again rcu_registry_lock
316 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
319 * Put quiescent reader list back into registry.
321 cds_list_splice(&qsreaders
, ®istry
);
323 mutex_unlock(&rcu_registry_lock
);
324 mutex_unlock(&rcu_gp_lock
);
325 urcu_wake_all_waiters(&waiters
);
328 * Finish waiting for reader threads before letting the old ptr being
336 #else /* !(CAA_BITS_PER_LONG < 64) */
337 void synchronize_rcu(void)
339 CDS_LIST_HEAD(qsreaders
);
340 unsigned long was_online
;
341 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
342 struct urcu_waiters waiters
;
344 was_online
= rcu_read_ongoing();
347 * Mark the writer thread offline to make sure we don't wait for
348 * our own quiescent state. This allows using synchronize_rcu()
349 * in threads registered as readers.
352 rcu_thread_offline();
357 * Add ourself to gp_waiters queue of threads awaiting to wait
358 * for a grace period. Proceed to perform the grace period only
359 * if we are the first thread added into the queue.
361 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
362 /* Not first in queue: will be awakened by another thread. */
363 urcu_adaptative_busy_wait(&wait
);
366 /* We won't need to wake ourself up */
367 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
369 mutex_lock(&rcu_gp_lock
);
372 * Move all waiters into our local queue.
374 urcu_move_waiters(&waiters
, &gp_waiters
);
376 mutex_lock(&rcu_registry_lock
);
378 if (cds_list_empty(®istry
))
381 /* Increment current G.P. */
382 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
+ RCU_GP_CTR
);
385 * Must commit rcu_gp.ctr update to memory before waiting for
386 * quiescent state. Failure to do so could result in the writer
387 * waiting forever while new readers are always accessing data
388 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
389 * before load URCU_TLS(rcu_reader).ctr.
394 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
395 * model easier to understand. It does not have a big performance impact
396 * anyway, given this is the write-side.
401 * Wait for readers to observe new count of be quiescent.
402 * wait_for_readers() can release and grab again rcu_registry_lock
405 wait_for_readers(®istry
, NULL
, &qsreaders
);
408 * Put quiescent reader list back into registry.
410 cds_list_splice(&qsreaders
, ®istry
);
412 mutex_unlock(&rcu_registry_lock
);
413 mutex_unlock(&rcu_gp_lock
);
414 urcu_wake_all_waiters(&waiters
);
421 #endif /* !(CAA_BITS_PER_LONG < 64) */
424 * library wrappers to be used by non-LGPL compatible source code.
427 void rcu_read_lock(void)
432 void rcu_read_unlock(void)
437 int rcu_read_ongoing(void)
439 return _rcu_read_ongoing();
442 void rcu_quiescent_state(void)
444 _rcu_quiescent_state();
447 void rcu_thread_offline(void)
449 _rcu_thread_offline();
452 void rcu_thread_online(void)
454 _rcu_thread_online();
457 void rcu_register_thread(void)
459 URCU_TLS(rcu_reader
).tid
= pthread_self();
460 assert(URCU_TLS(rcu_reader
).ctr
== 0);
462 mutex_lock(&rcu_registry_lock
);
463 cds_list_add(&URCU_TLS(rcu_reader
).node
, ®istry
);
464 mutex_unlock(&rcu_registry_lock
);
465 _rcu_thread_online();
468 void rcu_unregister_thread(void)
471 * We have to make the thread offline otherwise we end up dealocking
472 * with a waiting writer.
474 _rcu_thread_offline();
475 mutex_lock(&rcu_registry_lock
);
476 cds_list_del(&URCU_TLS(rcu_reader
).node
);
477 mutex_unlock(&rcu_registry_lock
);
483 * Assertion disabled because call_rcu threads are now rcu
484 * readers, and left running at exit.
485 * assert(cds_list_empty(®istry));
489 DEFINE_RCU_FLAVOR(rcu_flavor
);
491 #include "urcu-call-rcu-impl.h"
492 #include "urcu-defer-impl.h"