4 * Userspace RCU library, "bulletproof" version.
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-bp.h"
41 #include "urcu/static/urcu-bp.h"
42 #include "urcu-pointer.h"
43 #include "urcu/tls-compat.h"
47 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
53 #define MAP_ANONYMOUS MAP_ANON
58 void *mremap_wrapper(void *old_address
, size_t old_size
,
59 size_t new_size
, int flags
)
61 return mremap(old_address
, old_size
, new_size
, flags
);
65 #define MREMAP_MAYMOVE 1
66 #define MREMAP_FIXED 2
69 * mremap wrapper for non-Linux systems not allowing MAYMOVE.
70 * This is not generic.
73 void *mremap_wrapper(void *old_address
, size_t old_size
,
74 size_t new_size
, int flags
)
76 assert(!(flags
& MREMAP_MAYMOVE
));
82 /* Sleep delay in ms */
83 #define RCU_SLEEP_DELAY_MS 10
84 #define INIT_NR_THREADS 8
85 #define ARENA_INIT_ALLOC \
86 sizeof(struct registry_chunk) \
87 + INIT_NR_THREADS * sizeof(struct rcu_reader)
90 * Active attempts to check for reader Q.S. before calling sleep().
92 #define RCU_QS_ACTIVE_ATTEMPTS 100
98 void __attribute__((constructor
)) rcu_bp_init(void);
100 void __attribute__((destructor
)) _rcu_bp_exit(void);
103 * rcu_gp_lock ensures mutual exclusion between threads calling
106 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
108 * rcu_registry_lock ensures mutual exclusion between threads
109 * registering and unregistering themselves to/from the registry, and
110 * with threads reading that registry from synchronize_rcu(). However,
111 * this lock is not held all the way through the completion of awaiting
112 * for the grace period. It is sporadically released between iterations
114 * rcu_registry_lock may nest inside rcu_gp_lock.
116 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
118 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
119 static int initialized
;
121 static pthread_key_t urcu_bp_key
;
124 unsigned int rcu_yield_active
;
125 __DEFINE_URCU_TLS_GLOBAL(unsigned int, rcu_rand_yield
);
128 struct rcu_gp rcu_gp
= { .ctr
= RCU_GP_COUNT
};
131 * Pointer to registry elements. Written to only by each individual reader. Read
132 * by both the reader and the writers.
134 __DEFINE_URCU_TLS_GLOBAL(struct rcu_reader
*, rcu_reader
);
136 static CDS_LIST_HEAD(registry
);
138 struct registry_chunk
{
139 size_t data_len
; /* data length */
140 size_t used
; /* amount of data used */
141 struct cds_list_head node
; /* chunk_list node */
145 struct registry_arena
{
146 struct cds_list_head chunk_list
;
149 static struct registry_arena registry_arena
= {
150 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
153 /* Saved fork signal mask, protected by rcu_gp_lock */
154 static sigset_t saved_fork_signal_mask
;
156 static void mutex_lock(pthread_mutex_t
*mutex
)
160 #ifndef DISTRUST_SIGNALS_EXTREME
161 ret
= pthread_mutex_lock(mutex
);
164 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
165 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
166 if (ret
!= EBUSY
&& ret
!= EINTR
)
170 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
173 static void mutex_unlock(pthread_mutex_t
*mutex
)
177 ret
= pthread_mutex_unlock(mutex
);
183 * Always called with rcu_registry lock held. Releases this lock between
184 * iterations and grabs it again. Holds the lock when it returns.
186 static void wait_for_readers(struct cds_list_head
*input_readers
,
187 struct cds_list_head
*cur_snap_readers
,
188 struct cds_list_head
*qsreaders
)
190 unsigned int wait_loops
= 0;
191 struct rcu_reader
*index
, *tmp
;
194 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
195 * indicate quiescence (not nested), or observe the current
199 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
202 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
203 switch (rcu_reader_state(&index
->ctr
)) {
204 case RCU_READER_ACTIVE_CURRENT
:
205 if (cur_snap_readers
) {
206 cds_list_move(&index
->node
,
211 case RCU_READER_INACTIVE
:
212 cds_list_move(&index
->node
, qsreaders
);
214 case RCU_READER_ACTIVE_OLD
:
216 * Old snapshot. Leaving node in
217 * input_readers will make us busy-loop
218 * until the snapshot becomes current or
219 * the reader becomes inactive.
225 if (cds_list_empty(input_readers
)) {
228 /* Temporarily unlock the registry lock. */
229 mutex_unlock(&rcu_registry_lock
);
230 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
231 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
234 /* Re-lock the registry lock before the next loop. */
235 mutex_lock(&rcu_registry_lock
);
240 void synchronize_rcu(void)
242 CDS_LIST_HEAD(cur_snap_readers
);
243 CDS_LIST_HEAD(qsreaders
);
244 sigset_t newmask
, oldmask
;
247 ret
= sigfillset(&newmask
);
249 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
252 mutex_lock(&rcu_gp_lock
);
254 mutex_lock(&rcu_registry_lock
);
256 if (cds_list_empty(®istry
))
259 /* All threads should read qparity before accessing data structure
260 * where new ptr points to. */
261 /* Write new ptr before changing the qparity */
265 * Wait for readers to observe original parity or be quiescent.
266 * wait_for_readers() can release and grab again rcu_registry_lock
269 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
272 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
273 * model easier to understand. It does not have a big performance impact
274 * anyway, given this is the write-side.
278 /* Switch parity: 0 -> 1, 1 -> 0 */
279 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ RCU_GP_CTR_PHASE
);
282 * Must commit qparity update to memory before waiting for other parity
283 * quiescent state. Failure to do so could result in the writer waiting
284 * forever while new readers are always accessing data (no progress).
285 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
289 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
290 * model easier to understand. It does not have a big performance impact
291 * anyway, given this is the write-side.
296 * Wait for readers to observe new parity or be quiescent.
297 * wait_for_readers() can release and grab again rcu_registry_lock
300 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
303 * Put quiescent reader list back into registry.
305 cds_list_splice(&qsreaders
, ®istry
);
308 * Finish waiting for reader threads before letting the old ptr being
313 mutex_unlock(&rcu_registry_lock
);
314 mutex_unlock(&rcu_gp_lock
);
315 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
320 * library wrappers to be used by non-LGPL compatible source code.
323 void rcu_read_lock(void)
328 void rcu_read_unlock(void)
333 int rcu_read_ongoing(void)
335 return _rcu_read_ongoing();
339 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
340 * Else, try expanding the last chunk. If this fails, allocate a new
341 * chunk twice as big as the last chunk.
342 * Memory used by chunks _never_ moves. A chunk could theoretically be
343 * freed when all "used" slots are released, but we don't do it at this
347 void expand_arena(struct registry_arena
*arena
)
349 struct registry_chunk
*new_chunk
, *last_chunk
;
350 size_t old_chunk_len
, new_chunk_len
;
353 if (cds_list_empty(&arena
->chunk_list
)) {
354 assert(ARENA_INIT_ALLOC
>=
355 sizeof(struct registry_chunk
)
356 + sizeof(struct rcu_reader
));
357 new_chunk_len
= ARENA_INIT_ALLOC
;
358 new_chunk
= mmap(NULL
, new_chunk_len
,
359 PROT_READ
| PROT_WRITE
,
360 MAP_ANONYMOUS
| MAP_PRIVATE
,
362 if (new_chunk
== MAP_FAILED
)
364 bzero(new_chunk
, new_chunk_len
);
365 new_chunk
->data_len
=
366 new_chunk_len
- sizeof(struct registry_chunk
);
367 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
368 return; /* We're done. */
371 /* Try expanding last chunk. */
372 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
373 struct registry_chunk
, node
);
375 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
376 new_chunk_len
= old_chunk_len
<< 1;
378 /* Don't allow memory mapping to move, just expand. */
379 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
381 if (new_chunk
!= MAP_FAILED
) {
382 /* Should not have moved. */
383 assert(new_chunk
== last_chunk
);
384 bzero((char *) last_chunk
+ old_chunk_len
,
385 new_chunk_len
- old_chunk_len
);
386 last_chunk
->data_len
=
387 new_chunk_len
- sizeof(struct registry_chunk
);
388 return; /* We're done. */
391 /* Remap did not succeed, we need to add a new chunk. */
392 new_chunk
= mmap(NULL
, new_chunk_len
,
393 PROT_READ
| PROT_WRITE
,
394 MAP_ANONYMOUS
| MAP_PRIVATE
,
396 if (new_chunk
== MAP_FAILED
)
398 bzero(new_chunk
, new_chunk_len
);
399 new_chunk
->data_len
=
400 new_chunk_len
- sizeof(struct registry_chunk
);
401 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
405 struct rcu_reader
*arena_alloc(struct registry_arena
*arena
)
407 struct registry_chunk
*chunk
;
408 struct rcu_reader
*rcu_reader_reg
;
409 int expand_done
= 0; /* Only allow to expand once per alloc */
410 size_t len
= sizeof(struct rcu_reader
);
413 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
414 if (chunk
->data_len
- chunk
->used
< len
)
417 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
418 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
420 if (!rcu_reader_reg
->alloc
) {
421 rcu_reader_reg
->alloc
= 1;
423 return rcu_reader_reg
;
437 /* Called with signals off and mutex locked */
439 void add_thread(void)
441 struct rcu_reader
*rcu_reader_reg
;
444 rcu_reader_reg
= arena_alloc(®istry_arena
);
447 ret
= pthread_setspecific(urcu_bp_key
, rcu_reader_reg
);
451 /* Add to registry */
452 rcu_reader_reg
->tid
= pthread_self();
453 assert(rcu_reader_reg
->ctr
== 0);
454 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
456 * Reader threads are pointing to the reader registry. This is
457 * why its memory should never be relocated.
459 URCU_TLS(rcu_reader
) = rcu_reader_reg
;
462 /* Called with mutex locked */
464 void cleanup_thread(struct registry_chunk
*chunk
,
465 struct rcu_reader
*rcu_reader_reg
)
467 rcu_reader_reg
->ctr
= 0;
468 cds_list_del(&rcu_reader_reg
->node
);
469 rcu_reader_reg
->tid
= 0;
470 rcu_reader_reg
->alloc
= 0;
471 chunk
->used
-= sizeof(struct rcu_reader
);
475 struct registry_chunk
*find_chunk(struct rcu_reader
*rcu_reader_reg
)
477 struct registry_chunk
*chunk
;
479 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
480 if (rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[0])
482 if (rcu_reader_reg
>= (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
])
489 /* Called with signals off and mutex locked */
491 void remove_thread(struct rcu_reader
*rcu_reader_reg
)
493 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
494 URCU_TLS(rcu_reader
) = NULL
;
497 /* Disable signals, take mutex, add to registry */
498 void rcu_bp_register(void)
500 sigset_t newmask
, oldmask
;
503 ret
= sigfillset(&newmask
);
506 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
511 * Check if a signal concurrently registered our thread since
512 * the check in rcu_read_lock().
514 if (URCU_TLS(rcu_reader
))
518 * Take care of early registration before urcu_bp constructor.
522 mutex_lock(&rcu_registry_lock
);
524 mutex_unlock(&rcu_registry_lock
);
526 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
531 /* Disable signals, take mutex, remove from registry */
533 void rcu_bp_unregister(struct rcu_reader
*rcu_reader_reg
)
535 sigset_t newmask
, oldmask
;
538 ret
= sigfillset(&newmask
);
541 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
545 mutex_lock(&rcu_registry_lock
);
546 remove_thread(rcu_reader_reg
);
547 mutex_unlock(&rcu_registry_lock
);
548 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
555 * Remove thread from the registry when it exits, and flag it as
556 * destroyed so garbage collection can take care of it.
559 void urcu_bp_thread_exit_notifier(void *rcu_key
)
561 rcu_bp_unregister(rcu_key
);
565 void rcu_bp_init(void)
567 mutex_lock(&init_lock
);
568 if (!rcu_bp_refcount
++) {
571 ret
= pthread_key_create(&urcu_bp_key
,
572 urcu_bp_thread_exit_notifier
);
577 mutex_unlock(&init_lock
);
581 void _rcu_bp_exit(void)
583 mutex_lock(&init_lock
);
584 if (!--rcu_bp_refcount
) {
585 struct registry_chunk
*chunk
, *tmp
;
588 cds_list_for_each_entry_safe(chunk
, tmp
,
589 ®istry_arena
.chunk_list
, node
) {
590 munmap(chunk
, chunk
->data_len
591 + sizeof(struct registry_chunk
));
593 ret
= pthread_key_delete(urcu_bp_key
);
597 mutex_unlock(&init_lock
);
601 * Keep ABI compability within stable versions. This has never been
602 * exposed through a header, but needs to stay in the .so until the
605 void rcu_bp_exit(void)
610 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
611 * sure we fork() don't race with a concurrent thread executing with
612 * any of those locks held. This ensures that the registry and data
613 * protected by rcu_gp_lock are in a coherent state in the child.
615 void rcu_bp_before_fork(void)
617 sigset_t newmask
, oldmask
;
620 ret
= sigfillset(&newmask
);
622 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
624 mutex_lock(&rcu_gp_lock
);
625 mutex_lock(&rcu_registry_lock
);
626 saved_fork_signal_mask
= oldmask
;
629 void rcu_bp_after_fork_parent(void)
634 oldmask
= saved_fork_signal_mask
;
635 mutex_unlock(&rcu_registry_lock
);
636 mutex_unlock(&rcu_gp_lock
);
637 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
642 * Prune all entries from registry except our own thread. Fits the Linux
643 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
646 void urcu_bp_prune_registry(void)
648 struct registry_chunk
*chunk
;
649 struct rcu_reader
*rcu_reader_reg
;
651 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
652 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
653 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
655 if (!rcu_reader_reg
->alloc
)
657 if (rcu_reader_reg
->tid
== pthread_self())
659 cleanup_thread(chunk
, rcu_reader_reg
);
664 void rcu_bp_after_fork_child(void)
669 urcu_bp_prune_registry();
670 oldmask
= saved_fork_signal_mask
;
671 mutex_unlock(&rcu_registry_lock
);
672 mutex_unlock(&rcu_gp_lock
);
673 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
677 void *rcu_dereference_sym_bp(void *p
)
679 return _rcu_dereference(p
);
682 void *rcu_set_pointer_sym_bp(void **p
, void *v
)
689 void *rcu_xchg_pointer_sym_bp(void **p
, void *v
)
692 return uatomic_xchg(p
, v
);
695 void *rcu_cmpxchg_pointer_sym_bp(void **p
, void *old
, void *_new
)
698 return uatomic_cmpxchg(p
, old
, _new
);
701 DEFINE_RCU_FLAVOR(rcu_flavor
);
703 #include "urcu-call-rcu-impl.h"
704 #include "urcu-defer-impl.h"