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/wfqueue.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 us */
83 #define RCU_SLEEP_DELAY 1000
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
95 void __attribute__((constructor
)) rcu_bp_init(void);
97 void __attribute__((destructor
)) rcu_bp_exit(void);
99 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
101 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
102 static int initialized
;
104 static pthread_key_t urcu_bp_key
;
107 unsigned int yield_active
;
108 __DEFINE_URCU_TLS_GLOBAL(unsigned int, rand_yield
);
112 * Global grace period counter.
113 * Contains the current RCU_GP_CTR_PHASE.
114 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
115 * Written to only by writer with mutex taken. Read by both writer and readers.
117 long rcu_gp_ctr
= RCU_GP_COUNT
;
120 * Pointer to registry elements. Written to only by each individual reader. Read
121 * by both the reader and the writers.
123 __DEFINE_URCU_TLS_GLOBAL(struct rcu_reader
*, rcu_reader
);
125 static CDS_LIST_HEAD(registry
);
127 struct registry_chunk
{
128 size_t data_len
; /* data length */
129 size_t used
; /* amount of data used */
130 struct cds_list_head node
; /* chunk_list node */
134 struct registry_arena
{
135 struct cds_list_head chunk_list
;
138 static struct registry_arena registry_arena
= {
139 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
142 /* Saved fork signal mask, protected by rcu_gp_lock */
143 static sigset_t saved_fork_signal_mask
;
145 static void mutex_lock(pthread_mutex_t
*mutex
)
149 #ifndef DISTRUST_SIGNALS_EXTREME
150 ret
= pthread_mutex_lock(mutex
);
153 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
154 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
155 if (ret
!= EBUSY
&& ret
!= EINTR
)
159 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
162 static void mutex_unlock(pthread_mutex_t
*mutex
)
166 ret
= pthread_mutex_unlock(mutex
);
171 void update_counter_and_wait(void)
173 CDS_LIST_HEAD(qsreaders
);
175 struct rcu_reader
*index
, *tmp
;
177 /* Switch parity: 0 -> 1, 1 -> 0 */
178 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
181 * Must commit qparity update to memory before waiting for other parity
182 * quiescent state. Failure to do so could result in the writer waiting
183 * forever while new readers are always accessing data (no progress).
184 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
188 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
189 * model easier to understand. It does not have a big performance impact
190 * anyway, given this is the write-side.
195 * Wait for each thread rcu_reader.ctr count to become 0.
199 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
200 if (!rcu_old_gp_ongoing(&index
->ctr
))
201 cds_list_move(&index
->node
, &qsreaders
);
204 if (cds_list_empty(®istry
)) {
207 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
208 usleep(RCU_SLEEP_DELAY
);
213 /* put back the reader list in the registry */
214 cds_list_splice(&qsreaders
, ®istry
);
217 void synchronize_rcu(void)
219 sigset_t newmask
, oldmask
;
222 ret
= sigfillset(&newmask
);
224 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
227 mutex_lock(&rcu_gp_lock
);
229 if (cds_list_empty(®istry
))
232 /* All threads should read qparity before accessing data structure
233 * where new ptr points to. */
234 /* Write new ptr before changing the qparity */
238 * Wait for previous parity to be empty of readers.
240 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
243 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
244 * model easier to understand. It does not have a big performance impact
245 * anyway, given this is the write-side.
250 * Wait for previous parity to be empty of readers.
252 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
255 * Finish waiting for reader threads before letting the old ptr being
260 mutex_unlock(&rcu_gp_lock
);
261 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
266 * library wrappers to be used by non-LGPL compatible source code.
269 void rcu_read_lock(void)
274 void rcu_read_unlock(void)
280 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
281 * Else, try expanding the last chunk. If this fails, allocate a new
282 * chunk twice as big as the last chunk.
283 * Memory used by chunks _never_ moves. A chunk could theoretically be
284 * freed when all "used" slots are released, but we don't do it at this
288 void expand_arena(struct registry_arena
*arena
)
290 struct registry_chunk
*new_chunk
, *last_chunk
;
291 size_t old_chunk_len
, new_chunk_len
;
294 if (cds_list_empty(&arena
->chunk_list
)) {
295 assert(ARENA_INIT_ALLOC
>=
296 sizeof(struct registry_chunk
)
297 + sizeof(struct rcu_reader
));
298 new_chunk_len
= ARENA_INIT_ALLOC
;
299 new_chunk
= mmap(NULL
, new_chunk_len
,
300 PROT_READ
| PROT_WRITE
,
301 MAP_ANONYMOUS
| MAP_PRIVATE
,
303 if (new_chunk
== MAP_FAILED
)
305 bzero(new_chunk
, new_chunk_len
);
306 new_chunk
->data_len
=
307 new_chunk_len
- sizeof(struct registry_chunk
);
308 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
309 return; /* We're done. */
312 /* Try expanding last chunk. */
313 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
314 struct registry_chunk
, node
);
316 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
317 new_chunk_len
= old_chunk_len
<< 1;
319 /* Don't allow memory mapping to move, just expand. */
320 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
322 if (new_chunk
!= MAP_FAILED
) {
323 /* Should not have moved. */
324 assert(new_chunk
== last_chunk
);
325 bzero((char *) last_chunk
+ old_chunk_len
,
326 new_chunk_len
- old_chunk_len
);
327 last_chunk
->data_len
=
328 new_chunk_len
- sizeof(struct registry_chunk
);
329 return; /* We're done. */
332 /* Remap did not succeed, we need to add a new chunk. */
333 new_chunk
= mmap(NULL
, new_chunk_len
,
334 PROT_READ
| PROT_WRITE
,
335 MAP_ANONYMOUS
| MAP_PRIVATE
,
337 if (new_chunk
== MAP_FAILED
)
339 bzero(new_chunk
, new_chunk_len
);
340 new_chunk
->data_len
=
341 new_chunk_len
- sizeof(struct registry_chunk
);
342 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
346 struct rcu_reader
*arena_alloc(struct registry_arena
*arena
)
348 struct registry_chunk
*chunk
;
349 struct rcu_reader
*rcu_reader_reg
;
350 int expand_done
= 0; /* Only allow to expand once per alloc */
351 size_t len
= sizeof(struct rcu_reader
);
354 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
355 if (chunk
->data_len
- chunk
->used
< len
)
358 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
359 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
361 if (!rcu_reader_reg
->alloc
) {
362 rcu_reader_reg
->alloc
= 1;
364 return rcu_reader_reg
;
378 /* Called with signals off and mutex locked */
380 void add_thread(void)
382 struct rcu_reader
*rcu_reader_reg
;
385 rcu_reader_reg
= arena_alloc(®istry_arena
);
388 ret
= pthread_setspecific(urcu_bp_key
, rcu_reader_reg
);
392 /* Add to registry */
393 rcu_reader_reg
->tid
= pthread_self();
394 assert(rcu_reader_reg
->ctr
== 0);
395 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
397 * Reader threads are pointing to the reader registry. This is
398 * why its memory should never be relocated.
400 URCU_TLS(rcu_reader
) = rcu_reader_reg
;
403 /* Called with mutex locked */
405 void cleanup_thread(struct registry_chunk
*chunk
,
406 struct rcu_reader
*rcu_reader_reg
)
408 rcu_reader_reg
->ctr
= 0;
409 cds_list_del(&rcu_reader_reg
->node
);
410 rcu_reader_reg
->tid
= 0;
411 rcu_reader_reg
->alloc
= 0;
412 chunk
->used
-= sizeof(struct rcu_reader
);
416 struct registry_chunk
*find_chunk(struct rcu_reader
*rcu_reader_reg
)
418 struct registry_chunk
*chunk
;
420 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
421 if (rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[0])
423 if (rcu_reader_reg
>= (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
])
430 /* Called with signals off and mutex locked */
432 void remove_thread(void)
434 struct rcu_reader
*rcu_reader_reg
;
436 rcu_reader_reg
= URCU_TLS(rcu_reader
);
437 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
438 URCU_TLS(rcu_reader
) = NULL
;
441 /* Disable signals, take mutex, add to registry */
442 void rcu_bp_register(void)
444 sigset_t newmask
, oldmask
;
447 ret
= sigfillset(&newmask
);
450 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
455 * Check if a signal concurrently registered our thread since
456 * the check in rcu_read_lock().
458 if (URCU_TLS(rcu_reader
))
462 * Take care of early registration before urcu_bp constructor.
466 mutex_lock(&rcu_gp_lock
);
468 mutex_unlock(&rcu_gp_lock
);
470 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
475 /* Disable signals, take mutex, remove from registry */
477 void rcu_bp_unregister(void)
479 sigset_t newmask
, oldmask
;
482 ret
= sigfillset(&newmask
);
485 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
489 mutex_lock(&rcu_gp_lock
);
491 mutex_unlock(&rcu_gp_lock
);
492 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
498 * Remove thread from the registry when it exits, and flag it as
499 * destroyed so garbage collection can take care of it.
502 void urcu_bp_thread_exit_notifier(void *rcu_key
)
504 assert(rcu_key
== URCU_TLS(rcu_reader
));
509 void rcu_bp_init(void)
511 mutex_lock(&init_lock
);
515 ret
= pthread_key_create(&urcu_bp_key
,
516 urcu_bp_thread_exit_notifier
);
521 mutex_unlock(&init_lock
);
525 void rcu_bp_exit(void)
527 struct registry_chunk
*chunk
, *tmp
;
530 cds_list_for_each_entry_safe(chunk
, tmp
,
531 ®istry_arena
.chunk_list
, node
) {
532 munmap(chunk
, chunk
->data_len
+ sizeof(struct registry_chunk
));
534 ret
= pthread_key_delete(urcu_bp_key
);
540 * Holding the rcu_gp_lock across fork will make sure we fork() don't race with
541 * a concurrent thread executing with this same lock held. This ensures that the
542 * registry is in a coherent state in the child.
544 void rcu_bp_before_fork(void)
546 sigset_t newmask
, oldmask
;
549 ret
= sigfillset(&newmask
);
551 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
553 mutex_lock(&rcu_gp_lock
);
554 saved_fork_signal_mask
= oldmask
;
557 void rcu_bp_after_fork_parent(void)
562 oldmask
= saved_fork_signal_mask
;
563 mutex_unlock(&rcu_gp_lock
);
564 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
569 * Prune all entries from registry except our own thread. Fits the Linux
570 * fork behavior. Called with rcu_gp_lock held.
573 void urcu_bp_prune_registry(void)
575 struct registry_chunk
*chunk
;
576 struct rcu_reader
*rcu_reader_reg
;
578 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
579 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
580 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
582 if (!rcu_reader_reg
->alloc
)
584 if (rcu_reader_reg
->tid
== pthread_self())
586 cleanup_thread(chunk
, rcu_reader_reg
);
591 void rcu_bp_after_fork_child(void)
596 urcu_bp_prune_registry();
597 oldmask
= saved_fork_signal_mask
;
598 mutex_unlock(&rcu_gp_lock
);
599 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
603 void *rcu_dereference_sym_bp(void *p
)
605 return _rcu_dereference(p
);
608 void *rcu_set_pointer_sym_bp(void **p
, void *v
)
615 void *rcu_xchg_pointer_sym_bp(void **p
, void *v
)
618 return uatomic_xchg(p
, v
);
621 void *rcu_cmpxchg_pointer_sym_bp(void **p
, void *old
, void *_new
)
624 return uatomic_cmpxchg(p
, old
, _new
);
627 DEFINE_RCU_FLAVOR(rcu_flavor
);
629 #include "urcu-call-rcu-impl.h"
630 #include "urcu-defer-impl.h"