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.
39 #include "urcu/wfcqueue.h"
40 #include "urcu/map/urcu.h"
41 #include "urcu/static/urcu.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 */
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 a few loops waiting for it.
58 #define KICK_READER_LOOPS 10000
61 * Active attempts to check for reader Q.S. before calling futex().
63 #define RCU_QS_ACTIVE_ATTEMPTS 100
67 int rcu_has_sys_membarrier
;
69 void __attribute__((constructor
)) rcu_init(void);
81 void __attribute__((constructor
)) rcu_init(void);
82 void __attribute__((destructor
)) rcu_exit(void);
85 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
90 * Global grace period counter.
91 * Contains the current RCU_GP_CTR_PHASE.
92 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
93 * Written to only by writer with mutex taken. Read by both writer and readers.
95 unsigned long rcu_gp_ctr
= RCU_GP_COUNT
;
98 * Written to only by each individual reader. Read by both the reader and the
101 DEFINE_URCU_TLS(struct rcu_reader
, rcu_reader
);
104 unsigned int rcu_yield_active
;
105 DEFINE_URCU_TLS(unsigned int, rcu_rand_yield
);
108 static CDS_LIST_HEAD(registry
);
111 * Queue keeping threads awaiting to wait for a grace period. Contains
112 * struct gp_waiters_thread objects.
114 static DEFINE_URCU_WAIT_QUEUE(gp_waiters
);
116 static void mutex_lock(pthread_mutex_t
*mutex
)
120 #ifndef DISTRUST_SIGNALS_EXTREME
121 ret
= pthread_mutex_lock(mutex
);
124 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
125 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
126 if (ret
!= EBUSY
&& ret
!= EINTR
)
128 if (CMM_LOAD_SHARED(URCU_TLS(rcu_reader
).need_mb
)) {
130 _CMM_STORE_SHARED(URCU_TLS(rcu_reader
).need_mb
, 0);
135 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
138 static void mutex_unlock(pthread_mutex_t
*mutex
)
142 ret
= pthread_mutex_unlock(mutex
);
147 #ifdef RCU_MEMBARRIER
148 static void smp_mb_master(int group
)
150 if (caa_likely(rcu_has_sys_membarrier
))
151 membarrier(MEMBARRIER_EXPEDITED
);
158 static void smp_mb_master(int group
)
165 static void force_mb_all_readers(void)
167 struct rcu_reader
*index
;
170 * Ask for each threads to execute a cmm_smp_mb() so we can consider the
171 * compiler barriers around rcu read lock as real memory barriers.
173 if (cds_list_empty(®istry
))
176 * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs
177 * a cache flush on architectures with non-coherent cache. Let's play
178 * safe and don't assume anything : we use cmm_smp_mc() to make sure the
179 * cache flush is enforced.
181 cds_list_for_each_entry(index
, ®istry
, node
) {
182 CMM_STORE_SHARED(index
->need_mb
, 1);
183 pthread_kill(index
->tid
, SIGRCU
);
186 * Wait for sighandler (and thus mb()) to execute on every thread.
188 * Note that the pthread_kill() will never be executed on systems
189 * that correctly deliver signals in a timely manner. However, it
190 * is not uncommon for kernels to have bugs that can result in
191 * lost or unduly delayed signals.
193 * If you are seeing the below pthread_kill() executing much at
194 * all, we suggest testing the underlying kernel and filing the
195 * relevant bug report. For Linux kernels, we recommend getting
196 * the Linux Test Project (LTP).
198 cds_list_for_each_entry(index
, ®istry
, node
) {
199 while (CMM_LOAD_SHARED(index
->need_mb
)) {
200 pthread_kill(index
->tid
, SIGRCU
);
204 cmm_smp_mb(); /* read ->need_mb before ending the barrier */
207 static void smp_mb_master(int group
)
209 force_mb_all_readers();
211 #endif /* #ifdef RCU_SIGNAL */
214 * synchronize_rcu() waiting. Single thread.
216 static void wait_gp(void)
218 /* Read reader_gp before read futex */
219 smp_mb_master(RCU_MB_GROUP
);
220 if (uatomic_read(&rcu_gp_futex
) == -1)
221 futex_async(&rcu_gp_futex
, FUTEX_WAIT
, -1,
225 static void wait_for_readers(struct cds_list_head
*input_readers
,
226 struct cds_list_head
*cur_snap_readers
,
227 struct cds_list_head
*qsreaders
)
230 struct rcu_reader
*index
, *tmp
;
233 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
234 * indicate quiescence (not nested), or observe the current
239 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
240 uatomic_dec(&rcu_gp_futex
);
241 /* Write futex before read reader_gp */
242 smp_mb_master(RCU_MB_GROUP
);
245 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
246 switch (rcu_reader_state(&index
->ctr
)) {
247 case RCU_READER_ACTIVE_CURRENT
:
248 if (cur_snap_readers
) {
249 cds_list_move(&index
->node
,
254 case RCU_READER_INACTIVE
:
255 cds_list_move(&index
->node
, qsreaders
);
257 case RCU_READER_ACTIVE_OLD
:
259 * Old snapshot. Leaving node in
260 * input_readers will make us busy-loop
261 * until the snapshot becomes current or
262 * the reader becomes inactive.
268 #ifndef HAS_INCOHERENT_CACHES
269 if (cds_list_empty(input_readers
)) {
270 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
271 /* Read reader_gp before write futex */
272 smp_mb_master(RCU_MB_GROUP
);
273 uatomic_set(&rcu_gp_futex
, 0);
277 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
282 #else /* #ifndef HAS_INCOHERENT_CACHES */
284 * BUSY-LOOP. Force the reader thread to commit its
285 * URCU_TLS(rcu_reader).ctr update to memory if we wait
288 if (cds_list_empty(input_readers
)) {
289 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
290 /* Read reader_gp before write futex */
291 smp_mb_master(RCU_MB_GROUP
);
292 uatomic_set(&rcu_gp_futex
, 0);
296 switch (wait_loops
) {
297 case RCU_QS_ACTIVE_ATTEMPTS
:
299 break; /* only escape switch */
300 case KICK_READER_LOOPS
:
301 smp_mb_master(RCU_MB_GROUP
);
303 break; /* only escape switch */
308 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
312 void synchronize_rcu(void)
314 CDS_LIST_HEAD(cur_snap_readers
);
315 CDS_LIST_HEAD(qsreaders
);
316 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
317 struct urcu_waiters waiters
;
320 * Add ourself to gp_waiters queue of threads awaiting to wait
321 * for a grace period. Proceed to perform the grace period only
322 * if we are the first thread added into the queue.
323 * The implicit memory barrier before urcu_wait_add()
324 * orders prior memory accesses of threads put into the wait
325 * queue before their insertion into the wait queue.
327 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
328 /* Not first in queue: will be awakened by another thread. */
329 urcu_adaptative_busy_wait(&wait
);
330 /* Order following memory accesses after grace period. */
334 /* We won't need to wake ourself up */
335 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
337 mutex_lock(&rcu_gp_lock
);
340 * Move all waiters into our local queue.
342 urcu_move_waiters(&waiters
, &gp_waiters
);
344 if (cds_list_empty(®istry
))
347 /* All threads should read qparity before accessing data structure
348 * where new ptr points to. Must be done within rcu_gp_lock because it
349 * iterates on reader threads.*/
350 /* Write new ptr before changing the qparity */
351 smp_mb_master(RCU_MB_GROUP
);
354 * Wait for readers to observe original parity or be quiescent.
356 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
359 * Must finish waiting for quiescent state for original parity before
360 * committing next rcu_gp_ctr update to memory. Failure to do so could
361 * result in the writer waiting forever while new readers are always
362 * accessing data (no progress). Enforce compiler-order of load
363 * URCU_TLS(rcu_reader).ctr before store to rcu_gp_ctr.
368 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
369 * model easier to understand. It does not have a big performance impact
370 * anyway, given this is the write-side.
374 /* Switch parity: 0 -> 1, 1 -> 0 */
375 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
378 * Must commit rcu_gp_ctr update to memory before waiting for quiescent
379 * state. Failure to do so could result in the writer waiting forever
380 * while new readers are always accessing data (no progress). Enforce
381 * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr.
387 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
388 * model easier to understand. It does not have a big performance impact
389 * anyway, given this is the write-side.
394 * Wait for readers to observe new parity or be quiescent.
396 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
399 * Put quiescent reader list back into registry.
401 cds_list_splice(&qsreaders
, ®istry
);
403 /* Finish waiting for reader threads before letting the old ptr being
404 * freed. Must be done within rcu_gp_lock because it iterates on reader
406 smp_mb_master(RCU_MB_GROUP
);
408 mutex_unlock(&rcu_gp_lock
);
411 * Wakeup waiters only after we have completed the grace period
412 * and have ensured the memory barriers at the end of the grace
413 * period have been issued.
415 urcu_wake_all_waiters(&waiters
);
419 * library wrappers to be used by non-LGPL compatible source code.
422 void rcu_read_lock(void)
427 void rcu_read_unlock(void)
432 int rcu_read_ongoing(void)
434 return _rcu_read_ongoing();
437 void rcu_register_thread(void)
439 URCU_TLS(rcu_reader
).tid
= pthread_self();
440 assert(URCU_TLS(rcu_reader
).need_mb
== 0);
441 assert(!(URCU_TLS(rcu_reader
).ctr
& RCU_GP_CTR_NEST_MASK
));
443 mutex_lock(&rcu_gp_lock
);
444 rcu_init(); /* In case gcc does not support constructor attribute */
445 cds_list_add(&URCU_TLS(rcu_reader
).node
, ®istry
);
446 mutex_unlock(&rcu_gp_lock
);
449 void rcu_unregister_thread(void)
451 mutex_lock(&rcu_gp_lock
);
452 cds_list_del(&URCU_TLS(rcu_reader
).node
);
453 mutex_unlock(&rcu_gp_lock
);
456 #ifdef RCU_MEMBARRIER
462 if (!membarrier(MEMBARRIER_EXPEDITED
| MEMBARRIER_QUERY
))
463 rcu_has_sys_membarrier
= 1;
468 static void sigrcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
471 * Executing this cmm_smp_mb() is the only purpose of this signal handler.
472 * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
476 _CMM_STORE_SHARED(URCU_TLS(rcu_reader
).need_mb
, 0);
481 * rcu_init constructor. Called when the library is linked, but also when
482 * reader threads are calling rcu_register_thread().
483 * Should only be called by a single thread at a given time. This is ensured by
484 * holing the rcu_gp_lock from rcu_register_thread() or by running at library
485 * load time, which should not be executed by multiple threads nor concurrently
486 * with rcu_register_thread() anyway.
490 struct sigaction act
;
497 act
.sa_sigaction
= sigrcu_handler
;
498 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
499 sigemptyset(&act
.sa_mask
);
500 ret
= sigaction(SIGRCU
, &act
, NULL
);
507 struct sigaction act
;
510 ret
= sigaction(SIGRCU
, NULL
, &act
);
513 assert(act
.sa_sigaction
== sigrcu_handler
);
514 assert(cds_list_empty(®istry
));
517 #endif /* #ifdef RCU_SIGNAL */
519 DEFINE_RCU_FLAVOR(rcu_flavor
);
521 #include "urcu-call-rcu-impl.h"
522 #include "urcu-defer-impl.h"