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.
38 #include "urcu/map/urcu-bp.h"
40 #include "urcu/static/urcu-bp.h"
41 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
45 #define MAP_ANONYMOUS MAP_ANON
50 #define MREMAP_MAYMOVE 1
51 #define MREMAP_FIXED 2
54 * mremap wrapper for non-Linux systems. Maps a RW, anonymous private mapping.
55 * This is not generic.
57 void *mremap(void *old_address
, size_t old_size
, size_t new_size
, int flags
)
61 assert(flags
& MREMAP_MAYMOVE
);
62 assert(!(flags
& MREMAP_FIXED
));
63 new_address
= mmap(old_address
, new_size
,
64 PROT_READ
| PROT_WRITE
,
65 MAP_ANONYMOUS
| MAP_PRIVATE
,
67 if (new_address
== MAP_FAILED
)
70 memcpy(new_address
, old_address
, old_size
);
71 munmap(old_address
, old_size
);
77 /* Sleep delay in us */
78 #define RCU_SLEEP_DELAY 1000
79 #define ARENA_INIT_ALLOC 16
82 * Active attempts to check for reader Q.S. before calling sleep().
84 #define RCU_QS_ACTIVE_ATTEMPTS 100
86 void __attribute__((destructor
)) rcu_bp_exit(void);
88 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
91 unsigned int yield_active
;
92 unsigned int __thread rand_yield
;
96 * Global grace period counter.
97 * Contains the current RCU_GP_CTR_PHASE.
98 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
99 * Written to only by writer with mutex taken. Read by both writer and readers.
101 long rcu_gp_ctr
= RCU_GP_COUNT
;
104 * Pointer to registry elements. Written to only by each individual reader. Read
105 * by both the reader and the writers.
107 struct rcu_reader __thread
*rcu_reader
;
109 static CDS_LIST_HEAD(registry
);
111 struct registry_arena
{
117 static struct registry_arena registry_arena
;
119 /* Saved fork signal mask, protected by rcu_gp_lock */
120 static sigset_t saved_fork_signal_mask
;
122 static void rcu_gc_registry(void);
124 static void mutex_lock(pthread_mutex_t
*mutex
)
128 #ifndef DISTRUST_SIGNALS_EXTREME
129 ret
= pthread_mutex_lock(mutex
);
131 perror("Error in pthread mutex lock");
134 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
135 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
136 if (ret
!= EBUSY
&& ret
!= EINTR
) {
137 printf("ret = %d, errno = %d\n", ret
, errno
);
138 perror("Error in pthread mutex lock");
143 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
146 static void mutex_unlock(pthread_mutex_t
*mutex
)
150 ret
= pthread_mutex_unlock(mutex
);
152 perror("Error in pthread mutex unlock");
157 void update_counter_and_wait(void)
159 CDS_LIST_HEAD(qsreaders
);
161 struct rcu_reader
*index
, *tmp
;
163 /* Switch parity: 0 -> 1, 1 -> 0 */
164 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
167 * Must commit qparity update to memory before waiting for other parity
168 * quiescent state. Failure to do so could result in the writer waiting
169 * forever while new readers are always accessing data (no progress).
170 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
174 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
175 * model easier to understand. It does not have a big performance impact
176 * anyway, given this is the write-side.
181 * Wait for each thread rcu_reader.ctr count to become 0.
185 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
186 if (!rcu_old_gp_ongoing(&index
->ctr
))
187 cds_list_move(&index
->node
, &qsreaders
);
190 if (cds_list_empty(®istry
)) {
193 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
194 usleep(RCU_SLEEP_DELAY
);
199 /* put back the reader list in the registry */
200 cds_list_splice(&qsreaders
, ®istry
);
203 void synchronize_rcu(void)
205 sigset_t newmask
, oldmask
;
208 ret
= sigemptyset(&newmask
);
210 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
213 mutex_lock(&rcu_gp_lock
);
215 if (cds_list_empty(®istry
))
218 /* All threads should read qparity before accessing data structure
219 * where new ptr points to. */
220 /* Write new ptr before changing the qparity */
223 /* Remove old registry elements */
227 * Wait for previous parity to be empty of readers.
229 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
232 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
233 * model easier to understand. It does not have a big performance impact
234 * anyway, given this is the write-side.
239 * Wait for previous parity to be empty of readers.
241 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
244 * Finish waiting for reader threads before letting the old ptr being
249 mutex_unlock(&rcu_gp_lock
);
250 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
255 * library wrappers to be used by non-LGPL compatible source code.
258 void rcu_read_lock(void)
263 void rcu_read_unlock(void)
271 static void resize_arena(struct registry_arena
*arena
, size_t len
)
276 new_arena
= mmap(arena
->p
, len
,
277 PROT_READ
| PROT_WRITE
,
278 MAP_ANONYMOUS
| MAP_PRIVATE
,
281 new_arena
= mremap(arena
->p
, arena
->len
,
282 len
, MREMAP_MAYMOVE
);
283 assert(new_arena
!= MAP_FAILED
);
286 * re-used the same region ?
288 if (new_arena
== arena
->p
)
291 bzero(new_arena
+ arena
->len
, len
- arena
->len
);
292 arena
->p
= new_arena
;
295 /* Called with signals off and mutex locked */
296 static void add_thread(void)
298 struct rcu_reader
*rcu_reader_reg
;
300 if (registry_arena
.len
301 < registry_arena
.used
+ sizeof(struct rcu_reader
))
302 resize_arena(®istry_arena
,
303 caa_max(registry_arena
.len
<< 1, ARENA_INIT_ALLOC
));
307 for (rcu_reader_reg
= registry_arena
.p
;
308 (void *)rcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
310 if (!rcu_reader_reg
->alloc
)
313 rcu_reader_reg
->alloc
= 1;
314 registry_arena
.used
+= sizeof(struct rcu_reader
);
316 /* Add to registry */
317 rcu_reader_reg
->tid
= pthread_self();
318 assert(rcu_reader_reg
->ctr
== 0);
319 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
320 rcu_reader
= rcu_reader_reg
;
323 /* Called with signals off and mutex locked */
324 static void rcu_gc_registry(void)
326 struct rcu_reader
*rcu_reader_reg
;
330 for (rcu_reader_reg
= registry_arena
.p
;
331 (void *)rcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
333 if (!rcu_reader_reg
->alloc
)
335 tid
= rcu_reader_reg
->tid
;
336 ret
= pthread_kill(tid
, 0);
337 assert(ret
!= EINVAL
);
339 cds_list_del(&rcu_reader_reg
->node
);
340 rcu_reader_reg
->ctr
= 0;
341 rcu_reader_reg
->alloc
= 0;
342 registry_arena
.used
-= sizeof(struct rcu_reader
);
347 /* Disable signals, take mutex, add to registry */
348 void rcu_bp_register(void)
350 sigset_t newmask
, oldmask
;
353 ret
= sigemptyset(&newmask
);
355 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
359 * Check if a signal concurrently registered our thread since
360 * the check in rcu_read_lock(). */
364 mutex_lock(&rcu_gp_lock
);
366 mutex_unlock(&rcu_gp_lock
);
368 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
374 munmap(registry_arena
.p
, registry_arena
.len
);
378 * Holding the rcu_gp_lock across fork will make sure we fork() don't race with
379 * a concurrent thread executing with this same lock held. This ensures that the
380 * registry is in a coherent state in the child.
382 void rcu_bp_before_fork(void)
384 sigset_t newmask
, oldmask
;
387 ret
= sigemptyset(&newmask
);
389 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
391 mutex_lock(&rcu_gp_lock
);
392 saved_fork_signal_mask
= oldmask
;
395 void rcu_bp_after_fork_parent(void)
400 oldmask
= saved_fork_signal_mask
;
401 mutex_unlock(&rcu_gp_lock
);
402 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
406 void rcu_bp_after_fork_child(void)
412 oldmask
= saved_fork_signal_mask
;
413 mutex_unlock(&rcu_gp_lock
);
414 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
418 #include "urcu-call-rcu-impl.h"
419 #include "urcu-defer-impl.h"