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
48 /* Sleep delay in us */
49 #define RCU_SLEEP_DELAY 1000
50 #define ARENA_INIT_ALLOC 16
52 void __attribute__((destructor
)) rcu_bp_exit(void);
54 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
57 unsigned int yield_active
;
58 unsigned int __thread rand_yield
;
62 * Global grace period counter.
63 * Contains the current RCU_GP_CTR_PHASE.
64 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
65 * Written to only by writer with mutex taken. Read by both writer and readers.
67 long rcu_gp_ctr
= RCU_GP_COUNT
;
70 * Pointer to registry elements. Written to only by each individual reader. Read
71 * by both the reader and the writers.
73 struct rcu_reader __thread
*rcu_reader
;
75 static CDS_LIST_HEAD(registry
);
77 struct registry_arena
{
83 static struct registry_arena registry_arena
;
85 /* Saved fork signal mask, protected by rcu_gp_lock */
86 static sigset_t saved_fork_signal_mask
;
88 static void rcu_gc_registry(void);
90 static void mutex_lock(pthread_mutex_t
*mutex
)
94 #ifndef DISTRUST_SIGNALS_EXTREME
95 ret
= pthread_mutex_lock(mutex
);
97 perror("Error in pthread mutex lock");
100 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
101 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
102 if (ret
!= EBUSY
&& ret
!= EINTR
) {
103 printf("ret = %d, errno = %d\n", ret
, errno
);
104 perror("Error in pthread mutex lock");
109 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
112 static void mutex_unlock(pthread_mutex_t
*mutex
)
116 ret
= pthread_mutex_unlock(mutex
);
118 perror("Error in pthread mutex unlock");
123 void update_counter_and_wait(void)
125 CDS_LIST_HEAD(qsreaders
);
127 struct rcu_reader
*index
, *tmp
;
129 /* Switch parity: 0 -> 1, 1 -> 0 */
130 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
133 * Must commit qparity update to memory before waiting for other parity
134 * quiescent state. Failure to do so could result in the writer waiting
135 * forever while new readers are always accessing data (no progress).
136 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
140 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
141 * model easier to understand. It does not have a big performance impact
142 * anyway, given this is the write-side.
147 * Wait for each thread rcu_reader.ctr count to become 0.
151 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
152 if (!rcu_old_gp_ongoing(&index
->ctr
))
153 cds_list_move(&index
->node
, &qsreaders
);
156 if (cds_list_empty(®istry
)) {
159 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
160 usleep(RCU_SLEEP_DELAY
);
165 /* put back the reader list in the registry */
166 cds_list_splice(&qsreaders
, ®istry
);
169 void synchronize_rcu(void)
171 sigset_t newmask
, oldmask
;
174 ret
= sigemptyset(&newmask
);
176 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
179 mutex_lock(&rcu_gp_lock
);
181 if (cds_list_empty(®istry
))
184 /* All threads should read qparity before accessing data structure
185 * where new ptr points to. */
186 /* Write new ptr before changing the qparity */
189 /* Remove old registry elements */
193 * Wait for previous parity to be empty of readers.
195 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
198 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
199 * model easier to understand. It does not have a big performance impact
200 * anyway, given this is the write-side.
205 * Wait for previous parity to be empty of readers.
207 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
210 * Finish waiting for reader threads before letting the old ptr being
215 mutex_unlock(&rcu_gp_lock
);
216 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
221 * library wrappers to be used by non-LGPL compatible source code.
224 void rcu_read_lock(void)
229 void rcu_read_unlock(void)
237 static void resize_arena(struct registry_arena
*arena
, size_t len
)
242 new_arena
= mmap(arena
->p
, len
,
243 PROT_READ
| PROT_WRITE
,
244 MAP_ANONYMOUS
| MAP_PRIVATE
,
247 new_arena
= mremap(arena
->p
, arena
->len
,
248 len
, MREMAP_MAYMOVE
);
249 assert(new_arena
!= MAP_FAILED
);
252 * re-used the same region ?
254 if (new_arena
== arena
->p
)
257 memcpy(new_arena
, arena
->p
, arena
->len
);
258 bzero(new_arena
+ arena
->len
, len
- arena
->len
);
259 arena
->p
= new_arena
;
262 /* Called with signals off and mutex locked */
263 static void add_thread(void)
265 struct rcu_reader
*rcu_reader_reg
;
267 if (registry_arena
.len
268 < registry_arena
.used
+ sizeof(struct rcu_reader
))
269 resize_arena(®istry_arena
,
270 max(registry_arena
.len
<< 1, ARENA_INIT_ALLOC
));
274 for (rcu_reader_reg
= registry_arena
.p
;
275 (void *)rcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
277 if (!rcu_reader_reg
->alloc
)
280 rcu_reader_reg
->alloc
= 1;
281 registry_arena
.used
+= sizeof(struct rcu_reader
);
283 /* Add to registry */
284 rcu_reader_reg
->tid
= pthread_self();
285 assert(rcu_reader_reg
->ctr
== 0);
286 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
287 rcu_reader
= rcu_reader_reg
;
290 /* Called with signals off and mutex locked */
291 static void rcu_gc_registry(void)
293 struct rcu_reader
*rcu_reader_reg
;
297 for (rcu_reader_reg
= registry_arena
.p
;
298 (void *)rcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
300 if (!rcu_reader_reg
->alloc
)
302 tid
= rcu_reader_reg
->tid
;
303 ret
= pthread_kill(tid
, 0);
304 assert(ret
!= EINVAL
);
306 cds_list_del(&rcu_reader_reg
->node
);
307 rcu_reader_reg
->ctr
= 0;
308 rcu_reader_reg
->alloc
= 0;
309 registry_arena
.used
-= sizeof(struct rcu_reader
);
314 /* Disable signals, take mutex, add to registry */
315 void rcu_bp_register(void)
317 sigset_t newmask
, oldmask
;
320 ret
= sigemptyset(&newmask
);
322 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
326 * Check if a signal concurrently registered our thread since
327 * the check in rcu_read_lock(). */
331 mutex_lock(&rcu_gp_lock
);
333 mutex_unlock(&rcu_gp_lock
);
335 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
341 munmap(registry_arena
.p
, registry_arena
.len
);
345 * Holding the rcu_gp_lock across fork will make sure we fork() don't race with
346 * a concurrent thread executing with this same lock held. This ensures that the
347 * registry is in a coherent state in the child.
349 void rcu_bp_before_fork(void)
351 sigset_t newmask
, oldmask
;
354 ret
= sigemptyset(&newmask
);
356 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
358 mutex_lock(&rcu_gp_lock
);
359 saved_fork_signal_mask
= oldmask
;
362 void rcu_bp_after_fork_parent(void)
367 oldmask
= saved_fork_signal_mask
;
368 mutex_unlock(&rcu_gp_lock
);
369 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
373 void rcu_bp_after_fork_child(void)
379 oldmask
= saved_fork_signal_mask
;
380 mutex_unlock(&rcu_gp_lock
);
381 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
385 #include "urcu-call-rcu-impl.h"
386 #include "urcu-defer-impl.h"