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
94 void __attribute__((destructor
)) rcu_bp_exit(void);
96 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
99 unsigned int yield_active
;
100 DEFINE_URCU_TLS(unsigned int, rand_yield
);
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 long rcu_gp_ctr
= RCU_GP_COUNT
;
112 * Pointer to registry elements. Written to only by each individual reader. Read
113 * by both the reader and the writers.
115 DEFINE_URCU_TLS(struct rcu_reader
*, rcu_reader
);
117 static CDS_LIST_HEAD(registry
);
119 struct registry_chunk
{
120 size_t data_len
; /* data length */
121 size_t used
; /* data used */
122 struct cds_list_head node
; /* chunk_list node */
126 struct registry_arena
{
127 struct cds_list_head chunk_list
;
130 static struct registry_arena registry_arena
= {
131 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
134 /* Saved fork signal mask, protected by rcu_gp_lock */
135 static sigset_t saved_fork_signal_mask
;
137 static void rcu_gc_registry(void);
139 static void mutex_lock(pthread_mutex_t
*mutex
)
143 #ifndef DISTRUST_SIGNALS_EXTREME
144 ret
= pthread_mutex_lock(mutex
);
147 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
148 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
149 if (ret
!= EBUSY
&& ret
!= EINTR
)
153 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
156 static void mutex_unlock(pthread_mutex_t
*mutex
)
160 ret
= pthread_mutex_unlock(mutex
);
165 void update_counter_and_wait(void)
167 CDS_LIST_HEAD(qsreaders
);
169 struct rcu_reader
*index
, *tmp
;
171 /* Switch parity: 0 -> 1, 1 -> 0 */
172 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
175 * Must commit qparity update to memory before waiting for other parity
176 * quiescent state. Failure to do so could result in the writer waiting
177 * forever while new readers are always accessing data (no progress).
178 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
182 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
183 * model easier to understand. It does not have a big performance impact
184 * anyway, given this is the write-side.
189 * Wait for each thread rcu_reader.ctr count to become 0.
193 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
194 if (!rcu_old_gp_ongoing(&index
->ctr
))
195 cds_list_move(&index
->node
, &qsreaders
);
198 if (cds_list_empty(®istry
)) {
201 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
202 usleep(RCU_SLEEP_DELAY
);
207 /* put back the reader list in the registry */
208 cds_list_splice(&qsreaders
, ®istry
);
211 void synchronize_rcu(void)
213 sigset_t newmask
, oldmask
;
216 ret
= sigfillset(&newmask
);
218 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
221 mutex_lock(&rcu_gp_lock
);
223 if (cds_list_empty(®istry
))
226 /* All threads should read qparity before accessing data structure
227 * where new ptr points to. */
228 /* Write new ptr before changing the qparity */
231 /* Remove old registry elements */
235 * Wait for previous parity to be empty of readers.
237 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
240 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
241 * model easier to understand. It does not have a big performance impact
242 * anyway, given this is the write-side.
247 * Wait for previous parity to be empty of readers.
249 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
252 * Finish waiting for reader threads before letting the old ptr being
257 mutex_unlock(&rcu_gp_lock
);
258 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
263 * library wrappers to be used by non-LGPL compatible source code.
266 void rcu_read_lock(void)
271 void rcu_read_unlock(void)
277 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
278 * Else, try expanding the last chunk. If this fails, allocate a new
279 * chunk twice as big as the last chunk.
280 * Memory used by chunks _never_ moves. A chunk could theoretically be
281 * freed when all "used" slots are released, but we don't do it at this
285 void expand_arena(struct registry_arena
*arena
)
287 struct registry_chunk
*new_chunk
, *last_chunk
;
288 size_t old_chunk_len
, new_chunk_len
;
291 if (cds_list_empty(&arena
->chunk_list
)) {
292 assert(ARENA_INIT_ALLOC
>=
293 sizeof(struct registry_chunk
)
294 + sizeof(struct rcu_reader
));
295 new_chunk_len
= ARENA_INIT_ALLOC
;
296 new_chunk
= mmap(NULL
, new_chunk_len
,
297 PROT_READ
| PROT_WRITE
,
298 MAP_ANONYMOUS
| MAP_PRIVATE
,
300 if (new_chunk
== MAP_FAILED
)
302 bzero(new_chunk
, new_chunk_len
);
303 new_chunk
->data_len
=
304 new_chunk_len
- sizeof(struct registry_chunk
);
305 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
306 return; /* We're done. */
309 /* Try expanding last chunk. */
310 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
311 struct registry_chunk
, node
);
313 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
314 new_chunk_len
= old_chunk_len
<< 1;
316 /* Don't allow memory mapping to move, just expand. */
317 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
319 if (new_chunk
!= MAP_FAILED
) {
320 /* Should not have moved. */
321 assert(new_chunk
== last_chunk
);
322 bzero((char *) last_chunk
+ old_chunk_len
,
323 new_chunk_len
- old_chunk_len
);
324 last_chunk
->data_len
=
325 new_chunk_len
- sizeof(struct registry_chunk
);
326 return; /* We're done. */
329 /* Remap did not succeed, we need to add a new chunk. */
330 new_chunk
= mmap(NULL
, new_chunk_len
,
331 PROT_READ
| PROT_WRITE
,
332 MAP_ANONYMOUS
| MAP_PRIVATE
,
334 if (new_chunk
== MAP_FAILED
)
336 bzero(new_chunk
, new_chunk_len
);
337 new_chunk
->data_len
=
338 new_chunk_len
- sizeof(struct registry_chunk
);
339 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
343 struct rcu_reader
*arena_alloc(struct registry_arena
*arena
)
345 struct registry_chunk
*chunk
;
346 struct rcu_reader
*rcu_reader_reg
;
347 int expand_done
= 0; /* Only allow to expand once per alloc */
348 size_t len
= sizeof(struct rcu_reader
);
351 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
352 if (chunk
->data_len
- chunk
->used
< len
)
355 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
356 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
358 if (!rcu_reader_reg
->alloc
) {
359 rcu_reader_reg
->alloc
= 1;
361 return rcu_reader_reg
;
375 /* Called with signals off and mutex locked */
377 void add_thread(void)
379 struct rcu_reader
*rcu_reader_reg
;
381 rcu_reader_reg
= arena_alloc(®istry_arena
);
385 /* Add to registry */
386 rcu_reader_reg
->tid
= pthread_self();
387 assert(rcu_reader_reg
->ctr
== 0);
388 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
390 * Reader threads are pointing to the reader registry. This is
391 * why its memory should never be relocated.
393 URCU_TLS(rcu_reader
) = rcu_reader_reg
;
396 /* Called with signals off and mutex locked */
397 static void rcu_gc_registry(void)
399 struct registry_chunk
*chunk
;
400 struct rcu_reader
*rcu_reader_reg
;
402 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
403 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
404 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
409 if (!rcu_reader_reg
->alloc
)
411 tid
= rcu_reader_reg
->tid
;
412 ret
= pthread_kill(tid
, 0);
413 assert(ret
!= EINVAL
);
415 cds_list_del(&rcu_reader_reg
->node
);
416 rcu_reader_reg
->ctr
= 0;
417 rcu_reader_reg
->alloc
= 0;
418 chunk
->used
-= sizeof(struct rcu_reader
);
425 /* Disable signals, take mutex, add to registry */
426 void rcu_bp_register(void)
428 sigset_t newmask
, oldmask
;
431 ret
= sigfillset(&newmask
);
433 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
437 * Check if a signal concurrently registered our thread since
438 * the check in rcu_read_lock(). */
439 if (URCU_TLS(rcu_reader
))
442 mutex_lock(&rcu_gp_lock
);
444 mutex_unlock(&rcu_gp_lock
);
446 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
450 void rcu_bp_exit(void)
452 struct registry_chunk
*chunk
, *tmp
;
454 cds_list_for_each_entry_safe(chunk
, tmp
,
455 ®istry_arena
.chunk_list
, node
) {
456 munmap(chunk
, chunk
->data_len
+ sizeof(struct registry_chunk
));
461 * Holding the rcu_gp_lock across fork will make sure we fork() don't race with
462 * a concurrent thread executing with this same lock held. This ensures that the
463 * registry is in a coherent state in the child.
465 void rcu_bp_before_fork(void)
467 sigset_t newmask
, oldmask
;
470 ret
= sigfillset(&newmask
);
472 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
474 mutex_lock(&rcu_gp_lock
);
475 saved_fork_signal_mask
= oldmask
;
478 void rcu_bp_after_fork_parent(void)
483 oldmask
= saved_fork_signal_mask
;
484 mutex_unlock(&rcu_gp_lock
);
485 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
489 void rcu_bp_after_fork_child(void)
495 oldmask
= saved_fork_signal_mask
;
496 mutex_unlock(&rcu_gp_lock
);
497 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
501 void *rcu_dereference_sym_bp(void *p
)
503 return _rcu_dereference(p
);
506 void *rcu_set_pointer_sym_bp(void **p
, void *v
)
513 void *rcu_xchg_pointer_sym_bp(void **p
, void *v
)
516 return uatomic_xchg(p
, v
);
519 void *rcu_cmpxchg_pointer_sym_bp(void **p
, void *old
, void *_new
)
522 return uatomic_cmpxchg(p
, old
, _new
);
525 DEFINE_RCU_FLAVOR(rcu_flavor
);
527 #include "urcu-call-rcu-impl.h"
528 #include "urcu-defer-impl.h"