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);
102 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
104 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
105 static int initialized
;
107 static pthread_key_t urcu_bp_key
;
110 unsigned int rcu_yield_active
;
111 __DEFINE_URCU_TLS_GLOBAL(unsigned int, rcu_rand_yield
);
114 struct rcu_gp rcu_gp
= { .ctr
= RCU_GP_COUNT
};
117 * Pointer to registry elements. Written to only by each individual reader. Read
118 * by both the reader and the writers.
120 __DEFINE_URCU_TLS_GLOBAL(struct rcu_reader
*, rcu_reader
);
122 static CDS_LIST_HEAD(registry
);
124 struct registry_chunk
{
125 size_t data_len
; /* data length */
126 size_t used
; /* amount of data used */
127 struct cds_list_head node
; /* chunk_list node */
131 struct registry_arena
{
132 struct cds_list_head chunk_list
;
135 static struct registry_arena registry_arena
= {
136 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
139 /* Saved fork signal mask, protected by rcu_gp_lock */
140 static sigset_t saved_fork_signal_mask
;
142 static void mutex_lock(pthread_mutex_t
*mutex
)
146 #ifndef DISTRUST_SIGNALS_EXTREME
147 ret
= pthread_mutex_lock(mutex
);
150 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
151 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
152 if (ret
!= EBUSY
&& ret
!= EINTR
)
156 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
159 static void mutex_unlock(pthread_mutex_t
*mutex
)
163 ret
= pthread_mutex_unlock(mutex
);
168 static void wait_for_readers(struct cds_list_head
*input_readers
,
169 struct cds_list_head
*cur_snap_readers
,
170 struct cds_list_head
*qsreaders
)
172 unsigned int wait_loops
= 0;
173 struct rcu_reader
*index
, *tmp
;
176 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
177 * indicate quiescence (not nested), or observe the current
181 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
184 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
185 switch (rcu_reader_state(&index
->ctr
)) {
186 case RCU_READER_ACTIVE_CURRENT
:
187 if (cur_snap_readers
) {
188 cds_list_move(&index
->node
,
193 case RCU_READER_INACTIVE
:
194 cds_list_move(&index
->node
, qsreaders
);
196 case RCU_READER_ACTIVE_OLD
:
198 * Old snapshot. Leaving node in
199 * input_readers will make us busy-loop
200 * until the snapshot becomes current or
201 * the reader becomes inactive.
207 if (cds_list_empty(input_readers
)) {
210 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
211 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
218 void synchronize_rcu(void)
220 CDS_LIST_HEAD(cur_snap_readers
);
221 CDS_LIST_HEAD(qsreaders
);
222 sigset_t newmask
, oldmask
;
225 ret
= sigfillset(&newmask
);
227 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
230 mutex_lock(&rcu_gp_lock
);
232 if (cds_list_empty(®istry
))
235 /* All threads should read qparity before accessing data structure
236 * where new ptr points to. */
237 /* Write new ptr before changing the qparity */
241 * Wait for readers to observe original parity or be quiescent.
243 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
246 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
247 * model easier to understand. It does not have a big performance impact
248 * anyway, given this is the write-side.
252 /* Switch parity: 0 -> 1, 1 -> 0 */
253 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ RCU_GP_CTR_PHASE
);
256 * Must commit qparity update to memory before waiting for other parity
257 * quiescent state. Failure to do so could result in the writer waiting
258 * forever while new readers are always accessing data (no progress).
259 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
263 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
264 * model easier to understand. It does not have a big performance impact
265 * anyway, given this is the write-side.
270 * Wait for readers to observe new parity or be quiescent.
272 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
275 * Put quiescent reader list back into registry.
277 cds_list_splice(&qsreaders
, ®istry
);
280 * Finish waiting for reader threads before letting the old ptr being
285 mutex_unlock(&rcu_gp_lock
);
286 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
291 * library wrappers to be used by non-LGPL compatible source code.
294 void rcu_read_lock(void)
299 void rcu_read_unlock(void)
304 int rcu_read_ongoing(void)
306 return _rcu_read_ongoing();
310 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
311 * Else, try expanding the last chunk. If this fails, allocate a new
312 * chunk twice as big as the last chunk.
313 * Memory used by chunks _never_ moves. A chunk could theoretically be
314 * freed when all "used" slots are released, but we don't do it at this
318 void expand_arena(struct registry_arena
*arena
)
320 struct registry_chunk
*new_chunk
, *last_chunk
;
321 size_t old_chunk_len
, new_chunk_len
;
324 if (cds_list_empty(&arena
->chunk_list
)) {
325 assert(ARENA_INIT_ALLOC
>=
326 sizeof(struct registry_chunk
)
327 + sizeof(struct rcu_reader
));
328 new_chunk_len
= ARENA_INIT_ALLOC
;
329 new_chunk
= mmap(NULL
, new_chunk_len
,
330 PROT_READ
| PROT_WRITE
,
331 MAP_ANONYMOUS
| MAP_PRIVATE
,
333 if (new_chunk
== MAP_FAILED
)
335 bzero(new_chunk
, new_chunk_len
);
336 new_chunk
->data_len
=
337 new_chunk_len
- sizeof(struct registry_chunk
);
338 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
339 return; /* We're done. */
342 /* Try expanding last chunk. */
343 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
344 struct registry_chunk
, node
);
346 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
347 new_chunk_len
= old_chunk_len
<< 1;
349 /* Don't allow memory mapping to move, just expand. */
350 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
352 if (new_chunk
!= MAP_FAILED
) {
353 /* Should not have moved. */
354 assert(new_chunk
== last_chunk
);
355 bzero((char *) last_chunk
+ old_chunk_len
,
356 new_chunk_len
- old_chunk_len
);
357 last_chunk
->data_len
=
358 new_chunk_len
- sizeof(struct registry_chunk
);
359 return; /* We're done. */
362 /* Remap did not succeed, we need to add a new chunk. */
363 new_chunk
= mmap(NULL
, new_chunk_len
,
364 PROT_READ
| PROT_WRITE
,
365 MAP_ANONYMOUS
| MAP_PRIVATE
,
367 if (new_chunk
== MAP_FAILED
)
369 bzero(new_chunk
, new_chunk_len
);
370 new_chunk
->data_len
=
371 new_chunk_len
- sizeof(struct registry_chunk
);
372 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
376 struct rcu_reader
*arena_alloc(struct registry_arena
*arena
)
378 struct registry_chunk
*chunk
;
379 struct rcu_reader
*rcu_reader_reg
;
380 int expand_done
= 0; /* Only allow to expand once per alloc */
381 size_t len
= sizeof(struct rcu_reader
);
384 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
385 if (chunk
->data_len
- chunk
->used
< len
)
388 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
389 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
391 if (!rcu_reader_reg
->alloc
) {
392 rcu_reader_reg
->alloc
= 1;
394 return rcu_reader_reg
;
408 /* Called with signals off and mutex locked */
410 void add_thread(void)
412 struct rcu_reader
*rcu_reader_reg
;
415 rcu_reader_reg
= arena_alloc(®istry_arena
);
418 ret
= pthread_setspecific(urcu_bp_key
, rcu_reader_reg
);
422 /* Add to registry */
423 rcu_reader_reg
->tid
= pthread_self();
424 assert(rcu_reader_reg
->ctr
== 0);
425 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
427 * Reader threads are pointing to the reader registry. This is
428 * why its memory should never be relocated.
430 URCU_TLS(rcu_reader
) = rcu_reader_reg
;
433 /* Called with mutex locked */
435 void cleanup_thread(struct registry_chunk
*chunk
,
436 struct rcu_reader
*rcu_reader_reg
)
438 rcu_reader_reg
->ctr
= 0;
439 cds_list_del(&rcu_reader_reg
->node
);
440 rcu_reader_reg
->tid
= 0;
441 rcu_reader_reg
->alloc
= 0;
442 chunk
->used
-= sizeof(struct rcu_reader
);
446 struct registry_chunk
*find_chunk(struct rcu_reader
*rcu_reader_reg
)
448 struct registry_chunk
*chunk
;
450 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
451 if (rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[0])
453 if (rcu_reader_reg
>= (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
])
460 /* Called with signals off and mutex locked */
462 void remove_thread(struct rcu_reader
*rcu_reader_reg
)
464 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
465 URCU_TLS(rcu_reader
) = NULL
;
468 /* Disable signals, take mutex, add to registry */
469 void rcu_bp_register(void)
471 sigset_t newmask
, oldmask
;
474 ret
= sigfillset(&newmask
);
477 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
482 * Check if a signal concurrently registered our thread since
483 * the check in rcu_read_lock().
485 if (URCU_TLS(rcu_reader
))
489 * Take care of early registration before urcu_bp constructor.
493 mutex_lock(&rcu_gp_lock
);
495 mutex_unlock(&rcu_gp_lock
);
497 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
502 /* Disable signals, take mutex, remove from registry */
504 void rcu_bp_unregister(struct rcu_reader
*rcu_reader_reg
)
506 sigset_t newmask
, oldmask
;
509 ret
= sigfillset(&newmask
);
512 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
516 mutex_lock(&rcu_gp_lock
);
517 remove_thread(rcu_reader_reg
);
518 mutex_unlock(&rcu_gp_lock
);
519 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
526 * Remove thread from the registry when it exits, and flag it as
527 * destroyed so garbage collection can take care of it.
530 void urcu_bp_thread_exit_notifier(void *rcu_key
)
532 rcu_bp_unregister(rcu_key
);
536 void rcu_bp_init(void)
538 mutex_lock(&init_lock
);
539 if (!rcu_bp_refcount
++) {
542 ret
= pthread_key_create(&urcu_bp_key
,
543 urcu_bp_thread_exit_notifier
);
548 mutex_unlock(&init_lock
);
552 void _rcu_bp_exit(void)
554 mutex_lock(&init_lock
);
555 if (!--rcu_bp_refcount
) {
556 struct registry_chunk
*chunk
, *tmp
;
559 cds_list_for_each_entry_safe(chunk
, tmp
,
560 ®istry_arena
.chunk_list
, node
) {
561 munmap(chunk
, chunk
->data_len
562 + sizeof(struct registry_chunk
));
564 ret
= pthread_key_delete(urcu_bp_key
);
568 mutex_unlock(&init_lock
);
572 * Keep ABI compability within stable versions. This has never been
573 * exposed through a header, but needs to stay in the .so until the
576 void rcu_bp_exit(void)
581 * Holding the rcu_gp_lock across fork will make sure we fork() don't race with
582 * a concurrent thread executing with this same lock held. This ensures that the
583 * registry is in a coherent state in the child.
585 void rcu_bp_before_fork(void)
587 sigset_t newmask
, oldmask
;
590 ret
= sigfillset(&newmask
);
592 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
594 mutex_lock(&rcu_gp_lock
);
595 saved_fork_signal_mask
= oldmask
;
598 void rcu_bp_after_fork_parent(void)
603 oldmask
= saved_fork_signal_mask
;
604 mutex_unlock(&rcu_gp_lock
);
605 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
610 * Prune all entries from registry except our own thread. Fits the Linux
611 * fork behavior. Called with rcu_gp_lock held.
614 void urcu_bp_prune_registry(void)
616 struct registry_chunk
*chunk
;
617 struct rcu_reader
*rcu_reader_reg
;
619 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
620 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
621 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
623 if (!rcu_reader_reg
->alloc
)
625 if (rcu_reader_reg
->tid
== pthread_self())
627 cleanup_thread(chunk
, rcu_reader_reg
);
632 void rcu_bp_after_fork_child(void)
637 urcu_bp_prune_registry();
638 oldmask
= saved_fork_signal_mask
;
639 mutex_unlock(&rcu_gp_lock
);
640 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
644 void *rcu_dereference_sym_bp(void *p
)
646 return _rcu_dereference(p
);
649 void *rcu_set_pointer_sym_bp(void **p
, void *v
)
656 void *rcu_xchg_pointer_sym_bp(void **p
, void *v
)
659 return uatomic_xchg(p
, v
);
662 void *rcu_cmpxchg_pointer_sym_bp(void **p
, void *old
, void *_new
)
665 return uatomic_cmpxchg(p
, old
, _new
);
668 DEFINE_RCU_FLAVOR(rcu_flavor
);
670 #include "urcu-call-rcu-impl.h"
671 #include "urcu-defer-impl.h"