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"
43 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
49 #define MAP_ANONYMOUS MAP_ANON
54 #define MREMAP_MAYMOVE 1
55 #define MREMAP_FIXED 2
58 * mremap wrapper for non-Linux systems. Maps a RW, anonymous private mapping.
59 * This is not generic.
61 void *mremap(void *old_address
, size_t old_size
, size_t new_size
, int flags
)
65 assert(flags
& MREMAP_MAYMOVE
);
66 assert(!(flags
& MREMAP_FIXED
));
67 new_address
= mmap(old_address
, new_size
,
68 PROT_READ
| PROT_WRITE
,
69 MAP_ANONYMOUS
| MAP_PRIVATE
,
71 if (new_address
== MAP_FAILED
)
74 memcpy(new_address
, old_address
, old_size
);
75 munmap(old_address
, old_size
);
81 /* Sleep delay in us */
82 #define RCU_SLEEP_DELAY 1000
83 #define ARENA_INIT_ALLOC 16
86 * Active attempts to check for reader Q.S. before calling sleep().
88 #define RCU_QS_ACTIVE_ATTEMPTS 100
90 void __attribute__((destructor
)) rcu_bp_exit(void);
92 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
95 unsigned int yield_active
;
96 unsigned int __thread rand_yield
;
100 * Global grace period counter.
101 * Contains the current RCU_GP_CTR_PHASE.
102 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
103 * Written to only by writer with mutex taken. Read by both writer and readers.
105 long rcu_gp_ctr
= RCU_GP_COUNT
;
108 * Pointer to registry elements. Written to only by each individual reader. Read
109 * by both the reader and the writers.
111 struct rcu_reader __thread
*rcu_reader
;
113 static CDS_LIST_HEAD(registry
);
115 struct registry_arena
{
121 static struct registry_arena registry_arena
;
123 /* Saved fork signal mask, protected by rcu_gp_lock */
124 static sigset_t saved_fork_signal_mask
;
126 static void rcu_gc_registry(void);
128 static void mutex_lock(pthread_mutex_t
*mutex
)
132 #ifndef DISTRUST_SIGNALS_EXTREME
133 ret
= pthread_mutex_lock(mutex
);
135 perror("Error in pthread mutex lock");
138 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
139 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
140 if (ret
!= EBUSY
&& ret
!= EINTR
) {
141 printf("ret = %d, errno = %d\n", ret
, errno
);
142 perror("Error in pthread mutex lock");
147 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
150 static void mutex_unlock(pthread_mutex_t
*mutex
)
154 ret
= pthread_mutex_unlock(mutex
);
156 perror("Error in pthread mutex unlock");
161 void update_counter_and_wait(void)
163 CDS_LIST_HEAD(qsreaders
);
165 struct rcu_reader
*index
, *tmp
;
167 /* Switch parity: 0 -> 1, 1 -> 0 */
168 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
171 * Must commit qparity update to memory before waiting for other parity
172 * quiescent state. Failure to do so could result in the writer waiting
173 * forever while new readers are always accessing data (no progress).
174 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
178 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
179 * model easier to understand. It does not have a big performance impact
180 * anyway, given this is the write-side.
185 * Wait for each thread rcu_reader.ctr count to become 0.
189 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
190 if (!rcu_old_gp_ongoing(&index
->ctr
))
191 cds_list_move(&index
->node
, &qsreaders
);
194 if (cds_list_empty(®istry
)) {
197 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
198 usleep(RCU_SLEEP_DELAY
);
203 /* put back the reader list in the registry */
204 cds_list_splice(&qsreaders
, ®istry
);
207 void synchronize_rcu(void)
209 sigset_t newmask
, oldmask
;
212 ret
= sigemptyset(&newmask
);
214 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
217 mutex_lock(&rcu_gp_lock
);
219 if (cds_list_empty(®istry
))
222 /* All threads should read qparity before accessing data structure
223 * where new ptr points to. */
224 /* Write new ptr before changing the qparity */
227 /* Remove old registry elements */
231 * Wait for previous parity to be empty of readers.
233 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
236 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
237 * model easier to understand. It does not have a big performance impact
238 * anyway, given this is the write-side.
243 * Wait for previous parity to be empty of readers.
245 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
248 * Finish waiting for reader threads before letting the old ptr being
253 mutex_unlock(&rcu_gp_lock
);
254 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
259 * library wrappers to be used by non-LGPL compatible source code.
262 void rcu_read_lock(void)
267 void rcu_read_unlock(void)
275 static void resize_arena(struct registry_arena
*arena
, size_t len
)
280 new_arena
= mmap(arena
->p
, len
,
281 PROT_READ
| PROT_WRITE
,
282 MAP_ANONYMOUS
| MAP_PRIVATE
,
285 new_arena
= mremap(arena
->p
, arena
->len
,
286 len
, MREMAP_MAYMOVE
);
287 assert(new_arena
!= MAP_FAILED
);
290 * re-used the same region ?
292 if (new_arena
== arena
->p
)
295 bzero(new_arena
+ arena
->len
, len
- arena
->len
);
296 arena
->p
= new_arena
;
299 /* Called with signals off and mutex locked */
300 static void add_thread(void)
302 struct rcu_reader
*rcu_reader_reg
;
304 if (registry_arena
.len
305 < registry_arena
.used
+ sizeof(struct rcu_reader
))
306 resize_arena(®istry_arena
,
307 caa_max(registry_arena
.len
<< 1, ARENA_INIT_ALLOC
));
311 for (rcu_reader_reg
= registry_arena
.p
;
312 (void *)rcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
314 if (!rcu_reader_reg
->alloc
)
317 rcu_reader_reg
->alloc
= 1;
318 registry_arena
.used
+= sizeof(struct rcu_reader
);
320 /* Add to registry */
321 rcu_reader_reg
->tid
= pthread_self();
322 assert(rcu_reader_reg
->ctr
== 0);
323 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
324 rcu_reader
= rcu_reader_reg
;
327 /* Called with signals off and mutex locked */
328 static void rcu_gc_registry(void)
330 struct rcu_reader
*rcu_reader_reg
;
334 for (rcu_reader_reg
= registry_arena
.p
;
335 (void *)rcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
337 if (!rcu_reader_reg
->alloc
)
339 tid
= rcu_reader_reg
->tid
;
340 ret
= pthread_kill(tid
, 0);
341 assert(ret
!= EINVAL
);
343 cds_list_del(&rcu_reader_reg
->node
);
344 rcu_reader_reg
->ctr
= 0;
345 rcu_reader_reg
->alloc
= 0;
346 registry_arena
.used
-= sizeof(struct rcu_reader
);
351 /* Disable signals, take mutex, add to registry */
352 void rcu_bp_register(void)
354 sigset_t newmask
, oldmask
;
357 ret
= sigemptyset(&newmask
);
359 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
363 * Check if a signal concurrently registered our thread since
364 * the check in rcu_read_lock(). */
368 mutex_lock(&rcu_gp_lock
);
370 mutex_unlock(&rcu_gp_lock
);
372 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
376 void rcu_bp_exit(void)
378 if (registry_arena
.p
)
379 munmap(registry_arena
.p
, registry_arena
.len
);
383 * Holding the rcu_gp_lock across fork will make sure we fork() don't race with
384 * a concurrent thread executing with this same lock held. This ensures that the
385 * registry is in a coherent state in the child.
387 void rcu_bp_before_fork(void)
389 sigset_t newmask
, oldmask
;
392 ret
= sigemptyset(&newmask
);
394 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
396 mutex_lock(&rcu_gp_lock
);
397 saved_fork_signal_mask
= oldmask
;
400 void rcu_bp_after_fork_parent(void)
405 oldmask
= saved_fork_signal_mask
;
406 mutex_unlock(&rcu_gp_lock
);
407 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
411 void rcu_bp_after_fork_child(void)
417 oldmask
= saved_fork_signal_mask
;
418 mutex_unlock(&rcu_gp_lock
);
419 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
423 #include "urcu-call-rcu-impl.h"
424 #include "urcu-defer-impl.h"