4 * Userspace RCU library, "bulletproof" version.
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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.
37 #include "urcu-bp-static.h"
38 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
41 /* Sleep delay in us */
42 #define RCU_SLEEP_DELAY 1000
43 #define ARENA_INIT_ALLOC 16
45 void __attribute__((destructor
)) urcu_bp_exit(void);
47 static pthread_mutex_t urcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
50 unsigned int yield_active
;
51 unsigned int __thread rand_yield
;
55 * Global grace period counter.
56 * Contains the current RCU_GP_CTR_BIT.
57 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
58 * Written to only by writer with mutex taken. Read by both writer and readers.
60 long urcu_gp_ctr
= RCU_GP_COUNT
;
63 * Pointer to registry elements. Written to only by each individual reader. Read
64 * by both the reader and the writers.
66 struct urcu_reader __thread
*urcu_reader
;
68 static LIST_HEAD(registry
);
70 struct registry_arena
{
76 static struct registry_arena registry_arena
;
78 static void rcu_gc_registry(void);
80 static void internal_urcu_lock(void)
84 #ifndef DISTRUST_SIGNALS_EXTREME
85 ret
= pthread_mutex_lock(&urcu_mutex
);
87 perror("Error in pthread mutex lock");
90 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
91 while ((ret
= pthread_mutex_trylock(&urcu_mutex
)) != 0) {
92 if (ret
!= EBUSY
&& ret
!= EINTR
) {
93 printf("ret = %d, errno = %d\n", ret
, errno
);
94 perror("Error in pthread mutex lock");
97 if (urcu_reader
.need_mb
) {
99 urcu_reader
.need_mb
= 0;
104 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
107 static void internal_urcu_unlock(void)
111 ret
= pthread_mutex_unlock(&urcu_mutex
);
113 perror("Error in pthread mutex unlock");
119 * called with urcu_mutex held.
121 static void switch_next_urcu_qparity(void)
123 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_CTR_BIT
);
126 void wait_for_quiescent_state(void)
128 LIST_HEAD(qsreaders
);
130 struct urcu_reader
*index
, *tmp
;
132 if (list_empty(®istry
))
135 * Wait for each thread urcu_reader.ctr count to become 0.
139 list_for_each_entry_safe(index
, tmp
, ®istry
, head
) {
140 if (!rcu_old_gp_ongoing(&index
->ctr
))
141 list_move(&index
->head
, &qsreaders
);
144 if (list_empty(®istry
)) {
147 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
148 usleep(RCU_SLEEP_DELAY
);
153 /* put back the reader list in the registry */
154 list_splice(&qsreaders
, ®istry
);
157 void synchronize_rcu(void)
159 sigset_t newmask
, oldmask
;
162 ret
= sigemptyset(&newmask
);
164 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
167 internal_urcu_lock();
169 /* Remove old registry elements */
172 /* All threads should read qparity before accessing data structure
173 * where new ptr points to. Must be done within internal_urcu_lock
174 * because it iterates on reader threads.*/
175 /* Write new ptr before changing the qparity */
178 switch_next_urcu_qparity(); /* 0 -> 1 */
181 * Must commit qparity update to memory before waiting for parity
182 * 0 quiescent state. Failure to do so could result in the writer
183 * waiting forever while new readers are always accessing data (no
185 * Ensured by STORE_SHARED and LOAD_SHARED.
189 * Adding a smp_mb() which is _not_ formally required, but makes the
190 * model easier to understand. It does not have a big performance impact
191 * anyway, given this is the write-side.
196 * Wait for previous parity to be empty of readers.
198 wait_for_quiescent_state(); /* Wait readers in parity 0 */
201 * Must finish waiting for quiescent state for parity 0 before
202 * committing qparity update to memory. Failure to do so could result in
203 * the writer waiting forever while new readers are always accessing
204 * data (no progress).
205 * Ensured by STORE_SHARED and LOAD_SHARED.
209 * Adding a smp_mb() which is _not_ formally required, but makes the
210 * model easier to understand. It does not have a big performance impact
211 * anyway, given this is the write-side.
215 switch_next_urcu_qparity(); /* 1 -> 0 */
218 * Must commit qparity update to memory before waiting for parity
219 * 1 quiescent state. Failure to do so could result in the writer
220 * waiting forever while new readers are always accessing data (no
222 * Ensured by STORE_SHARED and LOAD_SHARED.
226 * Adding a smp_mb() which is _not_ formally required, but makes the
227 * model easier to understand. It does not have a big performance impact
228 * anyway, given this is the write-side.
233 * Wait for previous parity to be empty of readers.
235 wait_for_quiescent_state(); /* Wait readers in parity 1 */
237 /* Finish waiting for reader threads before letting the old ptr being
238 * freed. Must be done within internal_urcu_lock because it iterates on
242 internal_urcu_unlock();
243 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
248 * library wrappers to be used by non-LGPL compatible source code.
251 void rcu_read_lock(void)
256 void rcu_read_unlock(void)
264 static void resize_arena(struct registry_arena
*arena
, size_t len
)
268 new_arena
= mmap(arena
->p
, len
,
269 PROT_READ
| PROT_WRITE
,
270 MAP_ANONYMOUS
| MAP_PRIVATE
,
273 * re-used the same region ?
275 if (new_arena
== arena
->p
)
278 memcpy(new_arena
, arena
->p
, arena
->len
);
279 bzero(new_arena
+ arena
->len
, len
- arena
->len
);
280 arena
->p
= new_arena
;
283 /* Called with signals off and mutex locked */
284 static void add_thread(void)
286 struct urcu_reader
*urcu_reader_reg
;
288 if (registry_arena
.len
289 < registry_arena
.used
+ sizeof(struct urcu_reader
))
290 resize_arena(®istry_arena
,
291 max(registry_arena
.len
<< 1, ARENA_INIT_ALLOC
));
295 for (urcu_reader_reg
= registry_arena
.p
;
296 (void *)urcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
298 if (!urcu_reader_reg
->alloc
)
301 urcu_reader_reg
->alloc
= 1;
302 registry_arena
.used
+= sizeof(struct urcu_reader
);
304 /* Add to registry */
305 urcu_reader_reg
->tid
= pthread_self();
306 assert(urcu_reader_reg
->ctr
== 0);
307 list_add(&urcu_reader_reg
->head
, ®istry
);
308 urcu_reader
= urcu_reader_reg
;
311 /* Called with signals off and mutex locked */
312 static void rcu_gc_registry(void)
314 struct urcu_reader
*urcu_reader_reg
;
318 for (urcu_reader_reg
= registry_arena
.p
;
319 (void *)urcu_reader_reg
< registry_arena
.p
+ registry_arena
.len
;
321 if (!urcu_reader_reg
->alloc
)
323 tid
= urcu_reader_reg
->tid
;
324 ret
= pthread_kill(tid
, 0);
325 assert(ret
!= EINVAL
);
327 list_del(&urcu_reader_reg
->head
);
328 urcu_reader_reg
->alloc
= 0;
329 registry_arena
.used
-= sizeof(struct urcu_reader
);
334 /* Disable signals, take mutex, add to registry */
335 void rcu_bp_register(void)
337 sigset_t newmask
, oldmask
;
340 ret
= sigemptyset(&newmask
);
342 ret
= pthread_sigmask(SIG_SETMASK
, &newmask
, &oldmask
);
346 * Check if a signal concurrently registered our thread since
347 * the check in rcu_read_lock(). */
351 internal_urcu_lock();
353 internal_urcu_unlock();
355 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
361 munmap(registry_arena
.p
, registry_arena
.len
);