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. Maps a RW, anonymous private mapping.
70 * This is not generic.
73 void *mremap_wrapper(void *old_address
, size_t old_size
,
74 size_t new_size
, int flags
)
78 assert(flags
& MREMAP_MAYMOVE
);
79 assert(!(flags
& MREMAP_FIXED
));
80 new_address
= mmap(old_address
, new_size
,
81 PROT_READ
| PROT_WRITE
,
82 MAP_ANONYMOUS
| MAP_PRIVATE
,
84 if (new_address
== MAP_FAILED
)
87 memcpy(new_address
, old_address
, old_size
);
88 munmap(old_address
, old_size
);
94 /* Sleep delay in us */
95 #define RCU_SLEEP_DELAY 1000
96 #define ARENA_INIT_ALLOC 16
99 * Active attempts to check for reader Q.S. before calling sleep().
101 #define RCU_QS_ACTIVE_ATTEMPTS 100
103 void __attribute__((destructor
)) rcu_bp_exit(void);
105 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
108 unsigned int rcu_yield_active
;
109 DEFINE_URCU_TLS(unsigned int, rcu_rand_yield
);
113 * Global grace period counter.
114 * Contains the current RCU_GP_CTR_PHASE.
115 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
116 * Written to only by writer with mutex taken. Read by both writer and readers.
118 unsigned long rcu_gp_ctr
= RCU_GP_COUNT
;
121 * Pointer to registry elements. Written to only by each individual reader. Read
122 * by both the reader and the writers.
124 DEFINE_URCU_TLS(struct rcu_reader
*, rcu_reader
);
126 static CDS_LIST_HEAD(registry
);
128 struct registry_arena
{
134 static struct registry_arena registry_arena
;
136 /* Saved fork signal mask, protected by rcu_gp_lock */
137 static sigset_t saved_fork_signal_mask
;
139 static void rcu_gc_registry(void);
141 static void mutex_lock(pthread_mutex_t
*mutex
)
145 #ifndef DISTRUST_SIGNALS_EXTREME
146 ret
= pthread_mutex_lock(mutex
);
149 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
150 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
151 if (ret
!= EBUSY
&& ret
!= EINTR
)
155 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
158 static void mutex_unlock(pthread_mutex_t
*mutex
)
162 ret
= pthread_mutex_unlock(mutex
);
167 static void wait_for_readers(struct cds_list_head
*input_readers
,
168 struct cds_list_head
*cur_snap_readers
,
169 struct cds_list_head
*qsreaders
)
172 struct rcu_reader
*index
, *tmp
;
175 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
176 * indicate quiescence (not nested), or observe the current
181 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
182 switch (rcu_reader_state(&index
->ctr
)) {
183 case RCU_READER_ACTIVE_CURRENT
:
184 if (cur_snap_readers
) {
185 cds_list_move(&index
->node
,
190 case RCU_READER_INACTIVE
:
191 cds_list_move(&index
->node
, qsreaders
);
193 case RCU_READER_ACTIVE_OLD
:
195 * Old snapshot. Leaving node in
196 * input_readers will make us busy-loop
197 * until the snapshot becomes current or
198 * the reader becomes inactive.
204 if (cds_list_empty(input_readers
)) {
207 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
208 usleep(RCU_SLEEP_DELAY
);
215 void synchronize_rcu(void)
217 CDS_LIST_HEAD(cur_snap_readers
);
218 CDS_LIST_HEAD(qsreaders
);
219 sigset_t newmask
, oldmask
;
222 ret
= sigemptyset(&newmask
);
224 ret
= pthread_sigmask(SIG_SETMASK
, &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 */
237 /* Remove old registry elements */
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();
312 static void resize_arena(struct registry_arena
*arena
, size_t len
)
317 new_arena
= mmap(arena
->p
, len
,
318 PROT_READ
| PROT_WRITE
,
319 MAP_ANONYMOUS
| MAP_PRIVATE
,
322 new_arena
= mremap_wrapper(arena
->p
, arena
->len
,
323 len
, MREMAP_MAYMOVE
);
324 assert(new_arena
!= MAP_FAILED
);
327 * re-used the same region ?
329 if (new_arena
== arena
->p
)
332 bzero(new_arena
+ arena
->len
, len
- arena
->len
);
333 arena
->p
= new_arena
;
336 /* Called with signals off and mutex locked */
337 static void add_thread(void)
339 struct rcu_reader
*rcu_reader_reg
;
341 if (registry_arena
.len
342 < registry_arena
.used
+ sizeof(struct rcu_reader
))
343 resize_arena(®istry_arena
,
344 caa_max(registry_arena
.len
<< 1, ARENA_INIT_ALLOC
));
348 for (rcu_reader_reg
= registry_arena
.p
;
349 (void *)rcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
351 if (!rcu_reader_reg
->alloc
)
354 rcu_reader_reg
->alloc
= 1;
355 registry_arena
.used
+= sizeof(struct rcu_reader
);
357 /* Add to registry */
358 rcu_reader_reg
->tid
= pthread_self();
359 assert(rcu_reader_reg
->ctr
== 0);
360 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
361 URCU_TLS(rcu_reader
) = rcu_reader_reg
;
364 /* Called with signals off and mutex locked */
365 static void rcu_gc_registry(void)
367 struct rcu_reader
*rcu_reader_reg
;
371 for (rcu_reader_reg
= registry_arena
.p
;
372 (void *)rcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
374 if (!rcu_reader_reg
->alloc
)
376 tid
= rcu_reader_reg
->tid
;
377 ret
= pthread_kill(tid
, 0);
378 assert(ret
!= EINVAL
);
380 cds_list_del(&rcu_reader_reg
->node
);
381 rcu_reader_reg
->ctr
= 0;
382 rcu_reader_reg
->alloc
= 0;
383 registry_arena
.used
-= sizeof(struct rcu_reader
);
388 /* Disable signals, take mutex, add to registry */
389 void rcu_bp_register(void)
391 sigset_t newmask
, oldmask
;
394 ret
= sigemptyset(&newmask
);
396 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
400 * Check if a signal concurrently registered our thread since
401 * the check in rcu_read_lock(). */
402 if (URCU_TLS(rcu_reader
))
405 mutex_lock(&rcu_gp_lock
);
407 mutex_unlock(&rcu_gp_lock
);
409 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
413 void rcu_bp_exit(void)
415 if (registry_arena
.p
)
416 munmap(registry_arena
.p
, registry_arena
.len
);
420 * Holding the rcu_gp_lock across fork will make sure we fork() don't race with
421 * a concurrent thread executing with this same lock held. This ensures that the
422 * registry is in a coherent state in the child.
424 void rcu_bp_before_fork(void)
426 sigset_t newmask
, oldmask
;
429 ret
= sigemptyset(&newmask
);
431 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
433 mutex_lock(&rcu_gp_lock
);
434 saved_fork_signal_mask
= oldmask
;
437 void rcu_bp_after_fork_parent(void)
442 oldmask
= saved_fork_signal_mask
;
443 mutex_unlock(&rcu_gp_lock
);
444 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
448 void rcu_bp_after_fork_child(void)
454 oldmask
= saved_fork_signal_mask
;
455 mutex_unlock(&rcu_gp_lock
);
456 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
460 void *rcu_dereference_sym_bp(void *p
)
462 return _rcu_dereference(p
);
465 void *rcu_set_pointer_sym_bp(void **p
, void *v
)
472 void *rcu_xchg_pointer_sym_bp(void **p
, void *v
)
475 return uatomic_xchg(p
, v
);
478 void *rcu_cmpxchg_pointer_sym_bp(void **p
, void *old
, void *_new
)
481 return uatomic_cmpxchg(p
, old
, _new
);
484 DEFINE_RCU_FLAVOR(rcu_flavor
);
486 #include "urcu-call-rcu-impl.h"
487 #include "urcu-defer-impl.h"