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"
47 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
49 #include "urcu-qsbr.h"
52 void __attribute__((destructor
)) rcu_exit(void);
55 * rcu_gp_lock ensures mutual exclusion between threads calling
58 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
60 * rcu_registry_lock ensures mutual exclusion between threads
61 * registering and unregistering themselves to/from the registry, and
62 * with threads reading that registry from synchronize_rcu(). However,
63 * this lock is not held all the way through the completion of awaiting
64 * for the grace period. It is sporadically released between iterations
66 * rcu_registry_lock may nest inside rcu_gp_lock.
68 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
69 struct rcu_gp rcu_gp
= { .ctr
= RCU_GP_ONLINE
};
72 * Active attempts to check for reader Q.S. before calling futex().
74 #define RCU_QS_ACTIVE_ATTEMPTS 100
77 * Written to only by each individual reader. Read by both the reader and the
80 DEFINE_URCU_TLS(struct rcu_reader
, rcu_reader
);
82 static CDS_LIST_HEAD(registry
);
85 * Queue keeping threads awaiting to wait for a grace period. Contains
86 * struct gp_waiters_thread objects.
88 static DEFINE_URCU_WAIT_QUEUE(gp_waiters
);
90 static void mutex_lock(pthread_mutex_t
*mutex
)
94 #ifndef DISTRUST_SIGNALS_EXTREME
95 ret
= pthread_mutex_lock(mutex
);
98 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
99 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
100 if (ret
!= EBUSY
&& ret
!= EINTR
)
104 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
107 static void mutex_unlock(pthread_mutex_t
*mutex
)
111 ret
= pthread_mutex_unlock(mutex
);
117 * synchronize_rcu() waiting. Single thread.
119 static void wait_gp(void)
121 /* Read reader_gp before read futex */
123 if (uatomic_read(&rcu_gp
.futex
) != -1)
125 while (futex_noasync(&rcu_gp
.futex
, FUTEX_WAIT
, -1,
129 /* Value already changed. */
132 /* Retry if interrupted by signal. */
133 break; /* Get out of switch. */
135 /* Unexpected error. */
142 * Always called with rcu_registry lock held. Releases this lock between
143 * iterations and grabs it again. Holds the lock when it returns.
145 static void wait_for_readers(struct cds_list_head
*input_readers
,
146 struct cds_list_head
*cur_snap_readers
,
147 struct cds_list_head
*qsreaders
)
149 unsigned int wait_loops
= 0;
150 struct rcu_reader
*index
, *tmp
;
153 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
154 * indicate quiescence (offline), or for them to observe the
155 * current rcu_gp.ctr value.
158 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
160 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
161 uatomic_set(&rcu_gp
.futex
, -1);
163 * Write futex before write waiting (the other side
164 * reads them in the opposite order).
167 cds_list_for_each_entry(index
, input_readers
, node
) {
168 _CMM_STORE_SHARED(index
->waiting
, 1);
170 /* Write futex before read reader_gp */
173 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
174 switch (rcu_reader_state(&index
->ctr
)) {
175 case RCU_READER_ACTIVE_CURRENT
:
176 if (cur_snap_readers
) {
177 cds_list_move(&index
->node
,
182 case RCU_READER_INACTIVE
:
183 cds_list_move(&index
->node
, qsreaders
);
185 case RCU_READER_ACTIVE_OLD
:
187 * Old snapshot. Leaving node in
188 * input_readers will make us busy-loop
189 * until the snapshot becomes current or
190 * the reader becomes inactive.
196 if (cds_list_empty(input_readers
)) {
197 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
198 /* Read reader_gp before write futex */
200 uatomic_set(&rcu_gp
.futex
, 0);
204 /* Temporarily unlock the registry lock. */
205 mutex_unlock(&rcu_registry_lock
);
206 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
209 #ifndef HAS_INCOHERENT_CACHES
211 #else /* #ifndef HAS_INCOHERENT_CACHES */
213 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
215 /* Re-lock the registry lock before the next loop. */
216 mutex_lock(&rcu_registry_lock
);
222 * Using a two-subphases algorithm for architectures with smaller than 64-bit
223 * long-size to ensure we do not encounter an overflow bug.
226 #if (CAA_BITS_PER_LONG < 64)
227 void synchronize_rcu(void)
229 CDS_LIST_HEAD(cur_snap_readers
);
230 CDS_LIST_HEAD(qsreaders
);
231 unsigned long was_online
;
232 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
233 struct urcu_waiters waiters
;
235 was_online
= rcu_read_ongoing();
237 /* All threads should read qparity before accessing data structure
238 * where new ptr points to. In the "then" case, rcu_thread_offline
239 * includes a memory barrier.
241 * Mark the writer thread offline to make sure we don't wait for
242 * our own quiescent state. This allows using synchronize_rcu()
243 * in threads registered as readers.
246 rcu_thread_offline();
251 * Add ourself to gp_waiters queue of threads awaiting to wait
252 * for a grace period. Proceed to perform the grace period only
253 * if we are the first thread added into the queue.
255 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
256 /* Not first in queue: will be awakened by another thread. */
257 urcu_adaptative_busy_wait(&wait
);
260 /* We won't need to wake ourself up */
261 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
263 mutex_lock(&rcu_gp_lock
);
266 * Move all waiters into our local queue.
268 urcu_move_waiters(&waiters
, &gp_waiters
);
270 mutex_lock(&rcu_registry_lock
);
272 if (cds_list_empty(®istry
))
276 * Wait for readers to observe original parity or be quiescent.
277 * wait_for_readers() can release and grab again rcu_registry_lock
280 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
283 * Must finish waiting for quiescent state for original parity
284 * before committing next rcu_gp.ctr update to memory. Failure
285 * to do so could result in the writer waiting forever while new
286 * readers are always accessing data (no progress). Enforce
287 * compiler-order of load URCU_TLS(rcu_reader).ctr before store
293 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
294 * model easier to understand. It does not have a big performance impact
295 * anyway, given this is the write-side.
299 /* Switch parity: 0 -> 1, 1 -> 0 */
300 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ RCU_GP_CTR
);
303 * Must commit rcu_gp.ctr update to memory before waiting for
304 * quiescent state. Failure to do so could result in the writer
305 * waiting forever while new readers are always accessing data
306 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
307 * before load URCU_TLS(rcu_reader).ctr.
312 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
313 * model easier to understand. It does not have a big performance impact
314 * anyway, given this is the write-side.
319 * Wait for readers to observe new parity or be quiescent.
320 * wait_for_readers() can release and grab again rcu_registry_lock
323 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
326 * Put quiescent reader list back into registry.
328 cds_list_splice(&qsreaders
, ®istry
);
330 mutex_unlock(&rcu_registry_lock
);
331 mutex_unlock(&rcu_gp_lock
);
332 urcu_wake_all_waiters(&waiters
);
335 * Finish waiting for reader threads before letting the old ptr being
343 #else /* !(CAA_BITS_PER_LONG < 64) */
344 void synchronize_rcu(void)
346 CDS_LIST_HEAD(qsreaders
);
347 unsigned long was_online
;
348 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
349 struct urcu_waiters waiters
;
351 was_online
= rcu_read_ongoing();
354 * Mark the writer thread offline to make sure we don't wait for
355 * our own quiescent state. This allows using synchronize_rcu()
356 * in threads registered as readers.
359 rcu_thread_offline();
364 * Add ourself to gp_waiters queue of threads awaiting to wait
365 * for a grace period. Proceed to perform the grace period only
366 * if we are the first thread added into the queue.
368 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
369 /* Not first in queue: will be awakened by another thread. */
370 urcu_adaptative_busy_wait(&wait
);
373 /* We won't need to wake ourself up */
374 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
376 mutex_lock(&rcu_gp_lock
);
379 * Move all waiters into our local queue.
381 urcu_move_waiters(&waiters
, &gp_waiters
);
383 mutex_lock(&rcu_registry_lock
);
385 if (cds_list_empty(®istry
))
388 /* Increment current G.P. */
389 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
+ RCU_GP_CTR
);
392 * Must commit rcu_gp.ctr update to memory before waiting for
393 * quiescent state. Failure to do so could result in the writer
394 * waiting forever while new readers are always accessing data
395 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
396 * before load URCU_TLS(rcu_reader).ctr.
401 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
402 * model easier to understand. It does not have a big performance impact
403 * anyway, given this is the write-side.
408 * Wait for readers to observe new count of be quiescent.
409 * wait_for_readers() can release and grab again rcu_registry_lock
412 wait_for_readers(®istry
, NULL
, &qsreaders
);
415 * Put quiescent reader list back into registry.
417 cds_list_splice(&qsreaders
, ®istry
);
419 mutex_unlock(&rcu_registry_lock
);
420 mutex_unlock(&rcu_gp_lock
);
421 urcu_wake_all_waiters(&waiters
);
428 #endif /* !(CAA_BITS_PER_LONG < 64) */
431 * library wrappers to be used by non-LGPL compatible source code.
434 void rcu_read_lock(void)
439 void rcu_read_unlock(void)
444 int rcu_read_ongoing(void)
446 return _rcu_read_ongoing();
449 void rcu_quiescent_state(void)
451 _rcu_quiescent_state();
454 void rcu_thread_offline(void)
456 _rcu_thread_offline();
459 void rcu_thread_online(void)
461 _rcu_thread_online();
464 void rcu_register_thread(void)
466 URCU_TLS(rcu_reader
).tid
= pthread_self();
467 assert(URCU_TLS(rcu_reader
).ctr
== 0);
469 mutex_lock(&rcu_registry_lock
);
470 assert(!URCU_TLS(rcu_reader
).registered
);
471 URCU_TLS(rcu_reader
).registered
= 1;
472 cds_list_add(&URCU_TLS(rcu_reader
).node
, ®istry
);
473 mutex_unlock(&rcu_registry_lock
);
474 _rcu_thread_online();
477 void rcu_unregister_thread(void)
480 * We have to make the thread offline otherwise we end up dealocking
481 * with a waiting writer.
483 _rcu_thread_offline();
484 assert(URCU_TLS(rcu_reader
).registered
);
485 URCU_TLS(rcu_reader
).registered
= 0;
486 mutex_lock(&rcu_registry_lock
);
487 cds_list_del(&URCU_TLS(rcu_reader
).node
);
488 mutex_unlock(&rcu_registry_lock
);
494 * Assertion disabled because call_rcu threads are now rcu
495 * readers, and left running at exit.
496 * assert(cds_list_empty(®istry));
500 DEFINE_RCU_FLAVOR(rcu_flavor
);
502 #include "urcu-call-rcu-impl.h"
503 #include "urcu-defer-impl.h"