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-static.h"
39 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
42 /* Sleep delay in us */
43 #define RCU_SLEEP_DELAY 1000
44 #define ARENA_INIT_ALLOC 16
46 void __attribute__((destructor
)) rcu_bp_exit(void);
48 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
51 unsigned int yield_active
;
52 unsigned int __thread rand_yield
;
56 * Global grace period counter.
57 * Contains the current RCU_GP_CTR_PHASE.
58 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
59 * Written to only by writer with mutex taken. Read by both writer and readers.
61 long rcu_gp_ctr
= RCU_GP_COUNT
;
64 * Pointer to registry elements. Written to only by each individual reader. Read
65 * by both the reader and the writers.
67 struct rcu_reader __thread
*rcu_reader
;
69 static CDS_LIST_HEAD(registry
);
71 struct registry_arena
{
77 static struct registry_arena registry_arena
;
79 /* Saved fork signal mask, protected by rcu_gp_lock */
80 static sigset_t saved_fork_signal_mask
;
82 static void rcu_gc_registry(void);
84 static void mutex_lock(pthread_mutex_t
*mutex
)
88 #ifndef DISTRUST_SIGNALS_EXTREME
89 ret
= pthread_mutex_lock(mutex
);
91 perror("Error in pthread mutex lock");
94 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
95 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
96 if (ret
!= EBUSY
&& ret
!= EINTR
) {
97 printf("ret = %d, errno = %d\n", ret
, errno
);
98 perror("Error in pthread mutex lock");
103 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
106 static void mutex_unlock(pthread_mutex_t
*mutex
)
110 ret
= pthread_mutex_unlock(mutex
);
112 perror("Error in pthread mutex unlock");
117 void update_counter_and_wait(void)
119 CDS_LIST_HEAD(qsreaders
);
121 struct rcu_reader
*index
, *tmp
;
123 /* Switch parity: 0 -> 1, 1 -> 0 */
124 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
127 * Must commit qparity update to memory before waiting for other parity
128 * quiescent state. Failure to do so could result in the writer waiting
129 * forever while new readers are always accessing data (no progress).
130 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
134 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
135 * model easier to understand. It does not have a big performance impact
136 * anyway, given this is the write-side.
141 * Wait for each thread rcu_reader.ctr count to become 0.
145 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
146 if (!rcu_old_gp_ongoing(&index
->ctr
))
147 cds_list_move(&index
->node
, &qsreaders
);
150 if (cds_list_empty(®istry
)) {
153 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
154 usleep(RCU_SLEEP_DELAY
);
159 /* put back the reader list in the registry */
160 cds_list_splice(&qsreaders
, ®istry
);
163 void synchronize_rcu(void)
165 sigset_t newmask
, oldmask
;
168 ret
= sigemptyset(&newmask
);
170 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
173 mutex_lock(&rcu_gp_lock
);
175 if (cds_list_empty(®istry
))
178 /* All threads should read qparity before accessing data structure
179 * where new ptr points to. */
180 /* Write new ptr before changing the qparity */
183 /* Remove old registry elements */
187 * Wait for previous parity to be empty of readers.
189 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
192 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
193 * model easier to understand. It does not have a big performance impact
194 * anyway, given this is the write-side.
199 * Wait for previous parity to be empty of readers.
201 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
204 * Finish waiting for reader threads before letting the old ptr being
209 mutex_unlock(&rcu_gp_lock
);
210 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
215 * library wrappers to be used by non-LGPL compatible source code.
218 void rcu_read_lock(void)
223 void rcu_read_unlock(void)
231 static void resize_arena(struct registry_arena
*arena
, size_t len
)
236 new_arena
= mmap(arena
->p
, len
,
237 PROT_READ
| PROT_WRITE
,
238 MAP_ANONYMOUS
| MAP_PRIVATE
,
241 new_arena
= mremap(arena
->p
, arena
->len
,
242 len
, MREMAP_MAYMOVE
);
243 assert(new_arena
!= MAP_FAILED
);
246 * re-used the same region ?
248 if (new_arena
== arena
->p
)
251 memcpy(new_arena
, arena
->p
, arena
->len
);
252 bzero(new_arena
+ arena
->len
, len
- arena
->len
);
253 arena
->p
= new_arena
;
256 /* Called with signals off and mutex locked */
257 static void add_thread(void)
259 struct rcu_reader
*rcu_reader_reg
;
261 if (registry_arena
.len
262 < registry_arena
.used
+ sizeof(struct rcu_reader
))
263 resize_arena(®istry_arena
,
264 max(registry_arena
.len
<< 1, ARENA_INIT_ALLOC
));
268 for (rcu_reader_reg
= registry_arena
.p
;
269 (void *)rcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
271 if (!rcu_reader_reg
->alloc
)
274 rcu_reader_reg
->alloc
= 1;
275 registry_arena
.used
+= sizeof(struct rcu_reader
);
277 /* Add to registry */
278 rcu_reader_reg
->tid
= pthread_self();
279 assert(rcu_reader_reg
->ctr
== 0);
280 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
281 rcu_reader
= rcu_reader_reg
;
284 /* Called with signals off and mutex locked */
285 static void rcu_gc_registry(void)
287 struct rcu_reader
*rcu_reader_reg
;
291 for (rcu_reader_reg
= registry_arena
.p
;
292 (void *)rcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
294 if (!rcu_reader_reg
->alloc
)
296 tid
= rcu_reader_reg
->tid
;
297 ret
= pthread_kill(tid
, 0);
298 assert(ret
!= EINVAL
);
300 cds_list_del(&rcu_reader_reg
->node
);
301 rcu_reader_reg
->ctr
= 0;
302 rcu_reader_reg
->alloc
= 0;
303 registry_arena
.used
-= sizeof(struct rcu_reader
);
308 /* Disable signals, take mutex, add to registry */
309 void rcu_bp_register(void)
311 sigset_t newmask
, oldmask
;
314 ret
= sigemptyset(&newmask
);
316 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
320 * Check if a signal concurrently registered our thread since
321 * the check in rcu_read_lock(). */
325 mutex_lock(&rcu_gp_lock
);
327 mutex_unlock(&rcu_gp_lock
);
329 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
335 munmap(registry_arena
.p
, registry_arena
.len
);
339 * Holding the rcu_gp_lock across fork will make sure we fork() don't race with
340 * a concurrent thread executing with this same lock held. This ensures that the
341 * registry is in a coherent state in the child.
343 void rcu_bp_before_fork(void)
345 sigset_t newmask
, oldmask
;
348 ret
= sigemptyset(&newmask
);
350 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
352 mutex_lock(&rcu_gp_lock
);
353 saved_fork_signal_mask
= oldmask
;
356 void rcu_bp_after_fork_parent(void)
361 oldmask
= saved_fork_signal_mask
;
362 mutex_unlock(&rcu_gp_lock
);
363 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
367 void rcu_bp_after_fork_child(void)
373 oldmask
= saved_fork_signal_mask
;
374 mutex_unlock(&rcu_gp_lock
);
375 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);