4 * Userspace RCU library
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.
35 #include "urcu-static.h"
36 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
42 void __attribute__((constructor
)) rcu_init(void);
43 void __attribute__((destructor
)) rcu_exit(void);
50 static pthread_mutex_t rcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
55 * Global grace period counter.
56 * Contains the current RCU_GP_CTR_PHASE.
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 rcu_gp_ctr
= RCU_GP_COUNT
;
63 * Written to only by each individual reader. Read by both the reader and the
66 struct rcu_reader __thread rcu_reader
;
69 unsigned int yield_active
;
70 unsigned int __thread rand_yield
;
73 static LIST_HEAD(registry
);
75 static void internal_rcu_lock(void)
79 #ifndef DISTRUST_SIGNALS_EXTREME
80 ret
= pthread_mutex_lock(&rcu_mutex
);
82 perror("Error in pthread mutex lock");
85 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
86 while ((ret
= pthread_mutex_trylock(&rcu_mutex
)) != 0) {
87 if (ret
!= EBUSY
&& ret
!= EINTR
) {
88 printf("ret = %d, errno = %d\n", ret
, errno
);
89 perror("Error in pthread mutex lock");
92 if (rcu_reader
.need_mb
) {
94 rcu_reader
.need_mb
= 0;
99 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
102 static void internal_rcu_unlock(void)
106 ret
= pthread_mutex_unlock(&rcu_mutex
);
108 perror("Error in pthread mutex unlock");
114 * called with rcu_mutex held.
116 static void switch_next_rcu_qparity(void)
118 STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
122 static void smp_mb_heavy()
127 static void force_mb_all_readers(void)
129 struct rcu_reader
*index
;
132 * Ask for each threads to execute a smp_mb() so we can consider the
133 * compiler barriers around rcu read lock as real memory barriers.
135 if (list_empty(®istry
))
138 * pthread_kill has a smp_mb(). But beware, we assume it performs
139 * a cache flush on architectures with non-coherent cache. Let's play
140 * safe and don't assume anything : we use smp_mc() to make sure the
141 * cache flush is enforced.
143 list_for_each_entry(index
, ®istry
, head
) {
145 smp_mc(); /* write need_mb before sending the signal */
146 pthread_kill(index
->tid
, SIGRCU
);
149 * Wait for sighandler (and thus mb()) to execute on every thread.
151 * Note that the pthread_kill() will never be executed on systems
152 * that correctly deliver signals in a timely manner. However, it
153 * is not uncommon for kernels to have bugs that can result in
154 * lost or unduly delayed signals.
156 * If you are seeing the below pthread_kill() executing much at
157 * all, we suggest testing the underlying kernel and filing the
158 * relevant bug report. For Linux kernels, we recommend getting
159 * the Linux Test Project (LTP).
161 list_for_each_entry(index
, ®istry
, head
) {
162 while (index
->need_mb
) {
163 pthread_kill(index
->tid
, SIGRCU
);
167 smp_mb(); /* read ->need_mb before ending the barrier */
170 static void smp_mb_heavy()
172 force_mb_all_readers();
174 #endif /* #else #ifdef RCU_MB */
177 * synchronize_rcu() waiting. Single thread.
179 static void wait_gp(void)
181 /* Read reader_gp before read futex */
183 if (uatomic_read(&gp_futex
) == -1)
184 futex_async(&gp_futex
, FUTEX_WAIT
, -1,
188 void wait_for_quiescent_state(void)
190 LIST_HEAD(qsreaders
);
192 struct rcu_reader
*index
, *tmp
;
194 if (list_empty(®istry
))
197 * Wait for each thread rcu_reader.ctr count to become 0.
201 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
202 uatomic_dec(&gp_futex
);
203 /* Write futex before read reader_gp */
207 list_for_each_entry_safe(index
, tmp
, ®istry
, head
) {
208 if (!rcu_old_gp_ongoing(&index
->ctr
))
209 list_move(&index
->head
, &qsreaders
);
212 #ifndef HAS_INCOHERENT_CACHES
213 if (list_empty(®istry
)) {
214 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
215 /* Read reader_gp before write futex */
217 uatomic_set(&gp_futex
, 0);
221 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
226 #else /* #ifndef HAS_INCOHERENT_CACHES */
228 * BUSY-LOOP. Force the reader thread to commit its
229 * rcu_reader.ctr update to memory if we wait for too long.
231 if (list_empty(®istry
)) {
232 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
233 /* Read reader_gp before write futex */
235 uatomic_set(&gp_futex
, 0);
239 switch (wait_loops
) {
240 case RCU_QS_ACTIVE_ATTEMPTS
:
242 break; /* only escape switch */
243 case KICK_READER_LOOPS
:
246 break; /* only escape switch */
251 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
253 /* put back the reader list in the registry */
254 list_splice(&qsreaders
, ®istry
);
257 void synchronize_rcu(void)
261 /* All threads should read qparity before accessing data structure
262 * where new ptr points to. Must be done within internal_rcu_lock
263 * because it iterates on reader threads.*/
264 /* Write new ptr before changing the qparity */
267 switch_next_rcu_qparity(); /* 0 -> 1 */
270 * Must commit qparity update to memory before waiting for parity
271 * 0 quiescent state. Failure to do so could result in the writer
272 * waiting forever while new readers are always accessing data (no
274 * Ensured by STORE_SHARED and LOAD_SHARED.
278 * Adding a smp_mb() which is _not_ formally required, but makes the
279 * model easier to understand. It does not have a big performance impact
280 * anyway, given this is the write-side.
285 * Wait for previous parity to be empty of readers.
287 wait_for_quiescent_state(); /* Wait readers in parity 0 */
290 * Must finish waiting for quiescent state for parity 0 before
291 * committing qparity update to memory. Failure to do so could result in
292 * the writer waiting forever while new readers are always accessing
293 * data (no progress).
294 * Ensured by STORE_SHARED and LOAD_SHARED.
298 * Adding a smp_mb() which is _not_ formally required, but makes the
299 * model easier to understand. It does not have a big performance impact
300 * anyway, given this is the write-side.
304 switch_next_rcu_qparity(); /* 1 -> 0 */
307 * Must commit qparity update to memory before waiting for parity
308 * 1 quiescent state. Failure to do so could result in the writer
309 * waiting forever while new readers are always accessing data (no
311 * Ensured by STORE_SHARED and LOAD_SHARED.
315 * Adding a smp_mb() which is _not_ formally required, but makes the
316 * model easier to understand. It does not have a big performance impact
317 * anyway, given this is the write-side.
322 * Wait for previous parity to be empty of readers.
324 wait_for_quiescent_state(); /* Wait readers in parity 1 */
326 /* Finish waiting for reader threads before letting the old ptr being
327 * freed. Must be done within internal_rcu_lock because it iterates on
331 internal_rcu_unlock();
335 * library wrappers to be used by non-LGPL compatible source code.
338 void rcu_read_lock(void)
343 void rcu_read_unlock(void)
348 void rcu_register_thread(void)
350 rcu_reader
.tid
= pthread_self();
351 assert(rcu_reader
.need_mb
== 0);
352 assert(rcu_reader
.ctr
== 0);
355 rcu_init(); /* In case gcc does not support constructor attribute */
356 list_add(&rcu_reader
.head
, ®istry
);
357 internal_rcu_unlock();
360 void rcu_unregister_thread(void)
363 list_del(&rcu_reader
.head
);
364 internal_rcu_unlock();
368 static void sigrcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
371 * Executing this smp_mb() is the only purpose of this signal handler.
372 * It punctually promotes barrier() into smp_mb() on every thread it is
376 rcu_reader
.need_mb
= 0;
381 * rcu_init constructor. Called when the library is linked, but also when
382 * reader threads are calling rcu_register_thread().
383 * Should only be called by a single thread at a given time. This is ensured by
384 * holing the internal_rcu_lock() from rcu_register_thread() or by running at
385 * library load time, which should not be executed by multiple threads nor
386 * concurrently with rcu_register_thread() anyway.
390 struct sigaction act
;
397 act
.sa_sigaction
= sigrcu_handler
;
398 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
399 sigemptyset(&act
.sa_mask
);
400 ret
= sigaction(SIGRCU
, &act
, NULL
);
402 perror("Error in sigaction");
409 struct sigaction act
;
412 ret
= sigaction(SIGRCU
, NULL
, &act
);
414 perror("Error in sigaction");
417 assert(act
.sa_sigaction
== sigrcu_handler
);
418 assert(list_empty(®istry
));
420 #endif /* #ifndef RCU_MB */