4 * Userspace RCU 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.
29 #define _DEFAULT_SOURCE
40 #include "urcu/wfqueue.h"
41 #include "urcu/map/urcu.h"
42 #include "urcu/static/urcu.h"
43 #include "urcu-pointer.h"
44 #include "urcu/tls-compat.h"
48 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
54 * If a reader is really non-cooperative and refuses to commit its
55 * rcu_active_readers count to memory (there is no barrier in the reader
56 * per-se), kick it after 10 loops waiting for it.
58 #define KICK_READER_LOOPS 10
61 * Active attempts to check for reader Q.S. before calling futex().
63 #define RCU_QS_ACTIVE_ATTEMPTS 100
67 int has_sys_membarrier
;
69 void __attribute__((constructor
)) rcu_init(void);
81 void __attribute__((constructor
)) rcu_init(void);
82 void __attribute__((destructor
)) rcu_exit(void);
86 * rcu_gp_lock ensures mutual exclusion between threads calling
89 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
91 * rcu_registry_lock ensures mutual exclusion between threads
92 * registering and unregistering themselves to/from the registry, and
93 * with threads reading that registry from synchronize_rcu(). However,
94 * this lock is not held all the way through the completion of awaiting
95 * for the grace period. It is sporadically released between iterations
97 * rcu_registry_lock may nest inside rcu_gp_lock.
99 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
104 * Global grace period counter.
105 * Contains the current RCU_GP_CTR_PHASE.
106 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
107 * Written to only by writer with mutex taken. Read by both writer and readers.
109 unsigned long rcu_gp_ctr
= RCU_GP_COUNT
;
111 * Written to only by each individual reader. Read by both the reader and the
114 __DEFINE_URCU_TLS_GLOBAL(struct rcu_reader
, rcu_reader
);
117 unsigned int yield_active
;
118 __DEFINE_URCU_TLS_GLOBAL(unsigned int, rand_yield
);
121 static CDS_LIST_HEAD(registry
);
123 static void mutex_lock(pthread_mutex_t
*mutex
)
127 #ifndef DISTRUST_SIGNALS_EXTREME
128 ret
= pthread_mutex_lock(mutex
);
131 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
132 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
133 if (ret
!= EBUSY
&& ret
!= EINTR
)
135 if (CMM_LOAD_SHARED(URCU_TLS(rcu_reader
).need_mb
)) {
137 _CMM_STORE_SHARED(URCU_TLS(rcu_reader
).need_mb
, 0);
142 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
145 static void mutex_unlock(pthread_mutex_t
*mutex
)
149 ret
= pthread_mutex_unlock(mutex
);
154 #ifdef RCU_MEMBARRIER
155 static void smp_mb_master(int group
)
157 if (caa_likely(has_sys_membarrier
))
158 membarrier(MEMBARRIER_EXPEDITED
);
165 static void smp_mb_master(int group
)
172 static void force_mb_all_readers(void)
174 struct rcu_reader
*index
;
177 * Ask for each threads to execute a cmm_smp_mb() so we can consider the
178 * compiler barriers around rcu read lock as real memory barriers.
180 if (cds_list_empty(®istry
))
183 * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs
184 * a cache flush on architectures with non-coherent cache. Let's play
185 * safe and don't assume anything : we use cmm_smp_mc() to make sure the
186 * cache flush is enforced.
188 cds_list_for_each_entry(index
, ®istry
, node
) {
189 CMM_STORE_SHARED(index
->need_mb
, 1);
190 pthread_kill(index
->tid
, SIGRCU
);
193 * Wait for sighandler (and thus mb()) to execute on every thread.
195 * Note that the pthread_kill() will never be executed on systems
196 * that correctly deliver signals in a timely manner. However, it
197 * is not uncommon for kernels to have bugs that can result in
198 * lost or unduly delayed signals.
200 * If you are seeing the below pthread_kill() executing much at
201 * all, we suggest testing the underlying kernel and filing the
202 * relevant bug report. For Linux kernels, we recommend getting
203 * the Linux Test Project (LTP).
205 cds_list_for_each_entry(index
, ®istry
, node
) {
206 while (CMM_LOAD_SHARED(index
->need_mb
)) {
207 pthread_kill(index
->tid
, SIGRCU
);
211 cmm_smp_mb(); /* read ->need_mb before ending the barrier */
214 static void smp_mb_master(int group
)
216 force_mb_all_readers();
218 #endif /* #ifdef RCU_SIGNAL */
221 * synchronize_rcu() waiting. Single thread.
222 * Always called with rcu_registry lock held. Releases this lock and
223 * grabs it again. Holds the lock when it returns.
225 static void wait_gp(void)
228 * Read reader_gp before read futex. smp_mb_master() needs to
229 * be called with the rcu registry lock held in RCU_SIGNAL
232 smp_mb_master(RCU_MB_GROUP
);
233 /* Temporarily unlock the registry lock. */
234 mutex_unlock(&rcu_registry_lock
);
235 if (uatomic_read(&gp_futex
) != -1)
237 while (futex_async(&gp_futex
, FUTEX_WAIT
, -1,
241 /* Value already changed. */
244 /* Retry if interrupted by signal. */
245 break; /* Get out of switch. */
247 /* Unexpected error. */
253 * Re-lock the registry lock before the next loop.
255 mutex_lock(&rcu_registry_lock
);
259 * Always called with rcu_registry lock held. Releases this lock between
260 * iterations and grabs it again. Holds the lock when it returns.
262 void update_counter_and_wait(void)
264 CDS_LIST_HEAD(qsreaders
);
265 unsigned int wait_loops
= 0;
266 struct rcu_reader
*index
, *tmp
;
267 #ifdef HAS_INCOHERENT_CACHES
268 unsigned int wait_gp_loops
= 0;
269 #endif /* HAS_INCOHERENT_CACHES */
271 /* Switch parity: 0 -> 1, 1 -> 0 */
272 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
275 * Must commit rcu_gp_ctr update to memory before waiting for quiescent
276 * state. Failure to do so could result in the writer waiting forever
277 * while new readers are always accessing data (no progress). Enforce
278 * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr.
284 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
285 * model easier to understand. It does not have a big performance impact
286 * anyway, given this is the write-side.
291 * Wait for each thread URCU_TLS(rcu_reader).ctr count to become 0.
294 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
296 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
297 uatomic_dec(&gp_futex
);
298 /* Write futex before read reader_gp */
299 smp_mb_master(RCU_MB_GROUP
);
302 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
303 if (!rcu_gp_ongoing(&index
->ctr
))
304 cds_list_move(&index
->node
, &qsreaders
);
307 #ifndef HAS_INCOHERENT_CACHES
308 if (cds_list_empty(®istry
)) {
309 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
310 /* Read reader_gp before write futex */
311 smp_mb_master(RCU_MB_GROUP
);
312 uatomic_set(&gp_futex
, 0);
316 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
317 /* wait_gp unlocks/locks registry lock. */
320 /* Temporarily unlock the registry lock. */
321 mutex_unlock(&rcu_registry_lock
);
324 * Re-lock the registry lock before the
327 mutex_lock(&rcu_registry_lock
);
330 #else /* #ifndef HAS_INCOHERENT_CACHES */
332 * BUSY-LOOP. Force the reader thread to commit its
333 * URCU_TLS(rcu_reader).ctr update to memory if we wait
336 if (cds_list_empty(®istry
)) {
337 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
338 /* Read reader_gp before write futex */
339 smp_mb_master(RCU_MB_GROUP
);
340 uatomic_set(&gp_futex
, 0);
344 if (wait_gp_loops
== KICK_READER_LOOPS
) {
345 smp_mb_master(RCU_MB_GROUP
);
348 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
349 /* wait_gp unlocks/locks registry lock. */
353 /* Temporarily unlock the registry lock. */
354 mutex_unlock(&rcu_registry_lock
);
357 * Re-lock the registry lock before the
360 mutex_lock(&rcu_registry_lock
);
363 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
365 /* put back the reader list in the registry */
366 cds_list_splice(&qsreaders
, ®istry
);
369 void synchronize_rcu(void)
371 mutex_lock(&rcu_gp_lock
);
372 mutex_lock(&rcu_registry_lock
);
374 if (cds_list_empty(®istry
))
378 * All threads should read qparity before accessing data structure
379 * where new ptr points to. Must be done within rcu_registry_lock
380 * because it iterates on reader threads.
382 /* Write new ptr before changing the qparity */
383 smp_mb_master(RCU_MB_GROUP
);
386 * Wait for previous parity to be empty of readers.
387 * update_counter_and_wait() can release and grab again
388 * rcu_registry_lock interally.
390 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
393 * Must finish waiting for quiescent state for parity 0 before
394 * committing next rcu_gp_ctr update to memory. Failure to do so could
395 * result in the writer waiting forever while new readers are always
396 * accessing data (no progress). Enforce compiler-order of load
397 * URCU_TLS(rcu_reader).ctr before store to rcu_gp_ctr.
402 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
403 * model easier to understand. It does not have a big performance impact
404 * anyway, given this is the write-side.
409 * Wait for previous parity to be empty of readers.
410 * update_counter_and_wait() can release and grab again
411 * rcu_registry_lock interally.
413 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
416 * Finish waiting for reader threads before letting the old ptr
417 * being freed. Must be done within rcu_registry_lock because it
418 * iterates on reader threads.
420 smp_mb_master(RCU_MB_GROUP
);
422 mutex_unlock(&rcu_registry_lock
);
423 mutex_unlock(&rcu_gp_lock
);
427 * library wrappers to be used by non-LGPL compatible source code.
430 void rcu_read_lock(void)
435 void rcu_read_unlock(void)
440 void rcu_register_thread(void)
442 URCU_TLS(rcu_reader
).tid
= pthread_self();
443 assert(URCU_TLS(rcu_reader
).need_mb
== 0);
444 assert(!(URCU_TLS(rcu_reader
).ctr
& RCU_GP_CTR_NEST_MASK
));
446 mutex_lock(&rcu_registry_lock
);
447 rcu_init(); /* In case gcc does not support constructor attribute */
448 cds_list_add(&URCU_TLS(rcu_reader
).node
, ®istry
);
449 mutex_unlock(&rcu_registry_lock
);
452 void rcu_unregister_thread(void)
454 mutex_lock(&rcu_registry_lock
);
455 cds_list_del(&URCU_TLS(rcu_reader
).node
);
456 mutex_unlock(&rcu_registry_lock
);
459 #ifdef RCU_MEMBARRIER
465 if (!membarrier(MEMBARRIER_EXPEDITED
| MEMBARRIER_QUERY
))
466 has_sys_membarrier
= 1;
471 static void sigrcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
474 * Executing this cmm_smp_mb() is the only purpose of this signal handler.
475 * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
479 _CMM_STORE_SHARED(URCU_TLS(rcu_reader
).need_mb
, 0);
484 * rcu_init constructor. Called when the library is linked, but also when
485 * reader threads are calling rcu_register_thread().
486 * Should only be called by a single thread at a given time. This is ensured by
487 * holing the rcu_registry_lock from rcu_register_thread() or by running
488 * at library load time, which should not be executed by multiple
489 * threads nor concurrently with rcu_register_thread() anyway.
493 struct sigaction act
;
500 act
.sa_sigaction
= sigrcu_handler
;
501 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
502 sigemptyset(&act
.sa_mask
);
503 ret
= sigaction(SIGRCU
, &act
, NULL
);
510 struct sigaction act
;
513 ret
= sigaction(SIGRCU
, NULL
, &act
);
516 assert(act
.sa_sigaction
== sigrcu_handler
);
517 assert(cds_list_empty(®istry
));
520 #endif /* #ifdef RCU_SIGNAL */
522 DEFINE_RCU_FLAVOR(rcu_flavor
);
524 #include "urcu-call-rcu-impl.h"
525 #include "urcu-defer-impl.h"