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-bp-map.h"
40 #include "urcu-bp-static.h"
41 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
44 /* Sleep delay in us */
45 #define RCU_SLEEP_DELAY 1000
46 #define ARENA_INIT_ALLOC 16
48 void __attribute__((destructor
)) rcu_bp_exit(void);
50 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
53 unsigned int yield_active
;
54 unsigned int __thread rand_yield
;
58 * Global grace period counter.
59 * Contains the current RCU_GP_CTR_PHASE.
60 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
61 * Written to only by writer with mutex taken. Read by both writer and readers.
63 long rcu_gp_ctr
= RCU_GP_COUNT
;
66 * Pointer to registry elements. Written to only by each individual reader. Read
67 * by both the reader and the writers.
69 struct rcu_reader __thread
*rcu_reader
;
71 static CDS_LIST_HEAD(registry
);
73 struct registry_arena
{
79 static struct registry_arena registry_arena
;
81 /* Saved fork signal mask, protected by rcu_gp_lock */
82 static sigset_t saved_fork_signal_mask
;
84 static void rcu_gc_registry(void);
86 static void mutex_lock(pthread_mutex_t
*mutex
)
90 #ifndef DISTRUST_SIGNALS_EXTREME
91 ret
= pthread_mutex_lock(mutex
);
93 perror("Error in pthread mutex lock");
96 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
97 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
98 if (ret
!= EBUSY
&& ret
!= EINTR
) {
99 printf("ret = %d, errno = %d\n", ret
, errno
);
100 perror("Error in pthread mutex lock");
105 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
108 static void mutex_unlock(pthread_mutex_t
*mutex
)
112 ret
= pthread_mutex_unlock(mutex
);
114 perror("Error in pthread mutex unlock");
119 void update_counter_and_wait(void)
121 CDS_LIST_HEAD(qsreaders
);
123 struct rcu_reader
*index
, *tmp
;
125 /* Switch parity: 0 -> 1, 1 -> 0 */
126 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
129 * Must commit qparity update to memory before waiting for other parity
130 * quiescent state. Failure to do so could result in the writer waiting
131 * forever while new readers are always accessing data (no progress).
132 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
136 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
137 * model easier to understand. It does not have a big performance impact
138 * anyway, given this is the write-side.
143 * Wait for each thread rcu_reader.ctr count to become 0.
147 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
148 if (!rcu_old_gp_ongoing(&index
->ctr
))
149 cds_list_move(&index
->node
, &qsreaders
);
152 if (cds_list_empty(®istry
)) {
155 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
156 usleep(RCU_SLEEP_DELAY
);
161 /* put back the reader list in the registry */
162 cds_list_splice(&qsreaders
, ®istry
);
165 void synchronize_rcu(void)
167 sigset_t newmask
, oldmask
;
170 ret
= sigemptyset(&newmask
);
172 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
175 mutex_lock(&rcu_gp_lock
);
177 if (cds_list_empty(®istry
))
180 /* All threads should read qparity before accessing data structure
181 * where new ptr points to. */
182 /* Write new ptr before changing the qparity */
185 /* Remove old registry elements */
189 * Wait for previous parity to be empty of readers.
191 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
194 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
195 * model easier to understand. It does not have a big performance impact
196 * anyway, given this is the write-side.
201 * Wait for previous parity to be empty of readers.
203 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
206 * Finish waiting for reader threads before letting the old ptr being
211 mutex_unlock(&rcu_gp_lock
);
212 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
217 * library wrappers to be used by non-LGPL compatible source code.
220 void rcu_read_lock(void)
225 void rcu_read_unlock(void)
233 static void resize_arena(struct registry_arena
*arena
, size_t len
)
238 new_arena
= mmap(arena
->p
, len
,
239 PROT_READ
| PROT_WRITE
,
240 MAP_ANONYMOUS
| MAP_PRIVATE
,
243 new_arena
= mremap(arena
->p
, arena
->len
,
244 len
, MREMAP_MAYMOVE
);
245 assert(new_arena
!= MAP_FAILED
);
248 * re-used the same region ?
250 if (new_arena
== arena
->p
)
253 memcpy(new_arena
, arena
->p
, arena
->len
);
254 bzero(new_arena
+ arena
->len
, len
- arena
->len
);
255 arena
->p
= new_arena
;
258 /* Called with signals off and mutex locked */
259 static void add_thread(void)
261 struct rcu_reader
*rcu_reader_reg
;
263 if (registry_arena
.len
264 < registry_arena
.used
+ sizeof(struct rcu_reader
))
265 resize_arena(®istry_arena
,
266 max(registry_arena
.len
<< 1, ARENA_INIT_ALLOC
));
270 for (rcu_reader_reg
= registry_arena
.p
;
271 (void *)rcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
273 if (!rcu_reader_reg
->alloc
)
276 rcu_reader_reg
->alloc
= 1;
277 registry_arena
.used
+= sizeof(struct rcu_reader
);
279 /* Add to registry */
280 rcu_reader_reg
->tid
= pthread_self();
281 assert(rcu_reader_reg
->ctr
== 0);
282 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
283 rcu_reader
= rcu_reader_reg
;
286 /* Called with signals off and mutex locked */
287 static void rcu_gc_registry(void)
289 struct rcu_reader
*rcu_reader_reg
;
293 for (rcu_reader_reg
= registry_arena
.p
;
294 (void *)rcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
296 if (!rcu_reader_reg
->alloc
)
298 tid
= rcu_reader_reg
->tid
;
299 ret
= pthread_kill(tid
, 0);
300 assert(ret
!= EINVAL
);
302 cds_list_del(&rcu_reader_reg
->node
);
303 rcu_reader_reg
->ctr
= 0;
304 rcu_reader_reg
->alloc
= 0;
305 registry_arena
.used
-= sizeof(struct rcu_reader
);
310 /* Disable signals, take mutex, add to registry */
311 void rcu_bp_register(void)
313 sigset_t newmask
, oldmask
;
316 ret
= sigemptyset(&newmask
);
318 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
322 * Check if a signal concurrently registered our thread since
323 * the check in rcu_read_lock(). */
327 mutex_lock(&rcu_gp_lock
);
329 mutex_unlock(&rcu_gp_lock
);
331 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
337 munmap(registry_arena
.p
, registry_arena
.len
);
341 * Holding the rcu_gp_lock across fork will make sure we fork() don't race with
342 * a concurrent thread executing with this same lock held. This ensures that the
343 * registry is in a coherent state in the child.
345 void rcu_bp_before_fork(void)
347 sigset_t newmask
, oldmask
;
350 ret
= sigemptyset(&newmask
);
352 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
354 mutex_lock(&rcu_gp_lock
);
355 saved_fork_signal_mask
= oldmask
;
358 void rcu_bp_after_fork_parent(void)
363 oldmask
= saved_fork_signal_mask
;
364 mutex_unlock(&rcu_gp_lock
);
365 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
369 void rcu_bp_after_fork_child(void)
375 oldmask
= saved_fork_signal_mask
;
376 mutex_unlock(&rcu_gp_lock
);
377 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
381 #include "urcu-call-rcu-impl.h"
382 #include "urcu-defer-impl.h"