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"
42 #include "urcu-pointer.h"
44 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
50 #define MAP_ANONYMOUS MAP_ANON
55 #define MREMAP_MAYMOVE 1
56 #define MREMAP_FIXED 2
59 * mremap wrapper for non-Linux systems. Maps a RW, anonymous private mapping.
60 * This is not generic.
62 void *mremap(void *old_address
, size_t old_size
, size_t new_size
, int flags
)
66 assert(flags
& MREMAP_MAYMOVE
);
67 assert(!(flags
& MREMAP_FIXED
));
68 new_address
= mmap(old_address
, new_size
,
69 PROT_READ
| PROT_WRITE
,
70 MAP_ANONYMOUS
| MAP_PRIVATE
,
72 if (new_address
== MAP_FAILED
)
75 memcpy(new_address
, old_address
, old_size
);
76 munmap(old_address
, old_size
);
82 /* Sleep delay in us */
83 #define RCU_SLEEP_DELAY 1000
84 #define ARENA_INIT_ALLOC 16
87 * Active attempts to check for reader Q.S. before calling sleep().
89 #define RCU_QS_ACTIVE_ATTEMPTS 100
91 void __attribute__((destructor
)) rcu_bp_exit(void);
93 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
96 unsigned int yield_active
;
97 unsigned int __thread rand_yield
;
101 * Global grace period counter.
102 * Contains the current RCU_GP_CTR_PHASE.
103 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
104 * Written to only by writer with mutex taken. Read by both writer and readers.
106 long rcu_gp_ctr
= RCU_GP_COUNT
;
109 * Pointer to registry elements. Written to only by each individual reader. Read
110 * by both the reader and the writers.
112 struct rcu_reader __thread
*rcu_reader
;
114 static CDS_LIST_HEAD(registry
);
116 struct registry_arena
{
122 static struct registry_arena registry_arena
;
124 /* Saved fork signal mask, protected by rcu_gp_lock */
125 static sigset_t saved_fork_signal_mask
;
127 static void rcu_gc_registry(void);
129 static void mutex_lock(pthread_mutex_t
*mutex
)
133 #ifndef DISTRUST_SIGNALS_EXTREME
134 ret
= pthread_mutex_lock(mutex
);
136 perror("Error in pthread mutex lock");
139 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
140 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
141 if (ret
!= EBUSY
&& ret
!= EINTR
) {
142 printf("ret = %d, errno = %d\n", ret
, errno
);
143 perror("Error in pthread mutex lock");
148 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
151 static void mutex_unlock(pthread_mutex_t
*mutex
)
155 ret
= pthread_mutex_unlock(mutex
);
157 perror("Error in pthread mutex unlock");
162 void update_counter_and_wait(void)
164 CDS_LIST_HEAD(qsreaders
);
166 struct rcu_reader
*index
, *tmp
;
168 /* Switch parity: 0 -> 1, 1 -> 0 */
169 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
172 * Must commit qparity update to memory before waiting for other parity
173 * quiescent state. Failure to do so could result in the writer waiting
174 * forever while new readers are always accessing data (no progress).
175 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
179 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
180 * model easier to understand. It does not have a big performance impact
181 * anyway, given this is the write-side.
186 * Wait for each thread rcu_reader.ctr count to become 0.
190 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
191 if (!rcu_old_gp_ongoing(&index
->ctr
))
192 cds_list_move(&index
->node
, &qsreaders
);
195 if (cds_list_empty(®istry
)) {
198 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
199 usleep(RCU_SLEEP_DELAY
);
204 /* put back the reader list in the registry */
205 cds_list_splice(&qsreaders
, ®istry
);
208 void synchronize_rcu(void)
210 sigset_t newmask
, oldmask
;
213 ret
= sigemptyset(&newmask
);
215 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
218 mutex_lock(&rcu_gp_lock
);
220 if (cds_list_empty(®istry
))
223 /* All threads should read qparity before accessing data structure
224 * where new ptr points to. */
225 /* Write new ptr before changing the qparity */
228 /* Remove old registry elements */
232 * Wait for previous parity to be empty of readers.
234 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
237 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
238 * model easier to understand. It does not have a big performance impact
239 * anyway, given this is the write-side.
244 * Wait for previous parity to be empty of readers.
246 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
249 * Finish waiting for reader threads before letting the old ptr being
254 mutex_unlock(&rcu_gp_lock
);
255 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
260 * library wrappers to be used by non-LGPL compatible source code.
263 void rcu_read_lock(void)
268 void rcu_read_unlock(void)
276 static void resize_arena(struct registry_arena
*arena
, size_t len
)
281 new_arena
= mmap(arena
->p
, len
,
282 PROT_READ
| PROT_WRITE
,
283 MAP_ANONYMOUS
| MAP_PRIVATE
,
286 new_arena
= mremap(arena
->p
, arena
->len
,
287 len
, MREMAP_MAYMOVE
);
288 assert(new_arena
!= MAP_FAILED
);
291 * re-used the same region ?
293 if (new_arena
== arena
->p
)
296 bzero(new_arena
+ arena
->len
, len
- arena
->len
);
297 arena
->p
= new_arena
;
300 /* Called with signals off and mutex locked */
301 static void add_thread(void)
303 struct rcu_reader
*rcu_reader_reg
;
305 if (registry_arena
.len
306 < registry_arena
.used
+ sizeof(struct rcu_reader
))
307 resize_arena(®istry_arena
,
308 caa_max(registry_arena
.len
<< 1, ARENA_INIT_ALLOC
));
312 for (rcu_reader_reg
= registry_arena
.p
;
313 (void *)rcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
315 if (!rcu_reader_reg
->alloc
)
318 rcu_reader_reg
->alloc
= 1;
319 registry_arena
.used
+= sizeof(struct rcu_reader
);
321 /* Add to registry */
322 rcu_reader_reg
->tid
= pthread_self();
323 assert(rcu_reader_reg
->ctr
== 0);
324 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
325 rcu_reader
= rcu_reader_reg
;
328 /* Called with signals off and mutex locked */
329 static void rcu_gc_registry(void)
331 struct rcu_reader
*rcu_reader_reg
;
335 for (rcu_reader_reg
= registry_arena
.p
;
336 (void *)rcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
338 if (!rcu_reader_reg
->alloc
)
340 tid
= rcu_reader_reg
->tid
;
341 ret
= pthread_kill(tid
, 0);
342 assert(ret
!= EINVAL
);
344 cds_list_del(&rcu_reader_reg
->node
);
345 rcu_reader_reg
->ctr
= 0;
346 rcu_reader_reg
->alloc
= 0;
347 registry_arena
.used
-= sizeof(struct rcu_reader
);
352 /* Disable signals, take mutex, add to registry */
353 void rcu_bp_register(void)
355 sigset_t newmask
, oldmask
;
358 ret
= sigemptyset(&newmask
);
360 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
364 * Check if a signal concurrently registered our thread since
365 * the check in rcu_read_lock(). */
369 mutex_lock(&rcu_gp_lock
);
371 mutex_unlock(&rcu_gp_lock
);
373 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
377 void rcu_bp_exit(void)
379 if (registry_arena
.p
)
380 munmap(registry_arena
.p
, registry_arena
.len
);
384 * Holding the rcu_gp_lock across fork will make sure we fork() don't race with
385 * a concurrent thread executing with this same lock held. This ensures that the
386 * registry is in a coherent state in the child.
388 void rcu_bp_before_fork(void)
390 sigset_t newmask
, oldmask
;
393 ret
= sigemptyset(&newmask
);
395 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
397 mutex_lock(&rcu_gp_lock
);
398 saved_fork_signal_mask
= oldmask
;
401 void rcu_bp_after_fork_parent(void)
406 oldmask
= saved_fork_signal_mask
;
407 mutex_unlock(&rcu_gp_lock
);
408 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
412 void rcu_bp_after_fork_child(void)
418 oldmask
= saved_fork_signal_mask
;
419 mutex_unlock(&rcu_gp_lock
);
420 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
426 #include "urcu-call-rcu-impl.h"
427 #include "urcu-defer-impl.h"