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.
36 #include "urcu-static.h"
37 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
42 int has_sys_membarrier
;
44 void __attribute__((constructor
)) rcu_init(void);
56 void __attribute__((constructor
)) rcu_init(void);
57 void __attribute__((destructor
)) rcu_exit(void);
60 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
65 * Global grace period counter.
66 * Contains the current RCU_GP_CTR_PHASE.
67 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
68 * Written to only by writer with mutex taken. Read by both writer and readers.
70 long rcu_gp_ctr
= RCU_GP_COUNT
;
73 * Written to only by each individual reader. Read by both the reader and the
76 struct rcu_reader __thread rcu_reader
;
79 unsigned int yield_active
;
80 unsigned int __thread rand_yield
;
83 static LIST_HEAD(registry
);
85 static void mutex_lock(pthread_mutex_t
*mutex
)
89 #ifndef DISTRUST_SIGNALS_EXTREME
90 ret
= pthread_mutex_lock(mutex
);
92 perror("Error in pthread mutex lock");
95 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
96 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
97 if (ret
!= EBUSY
&& ret
!= EINTR
) {
98 printf("ret = %d, errno = %d\n", ret
, errno
);
99 perror("Error in pthread mutex lock");
102 if (rcu_reader
.need_mb
) {
104 rcu_reader
.need_mb
= 0;
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");
124 * called with rcu_gp_lock held.
126 static void switch_next_rcu_qparity(void)
128 STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
131 #ifdef RCU_MEMBARRIER
132 static void smp_mb_heavy(void)
134 if (likely(has_sys_membarrier
))
135 membarrier(MEMBARRIER_EXPEDITED
);
142 static void smp_mb_heavy(void)
149 static void force_mb_all_readers(void)
151 struct rcu_reader
*index
;
154 * Ask for each threads to execute a smp_mb() so we can consider the
155 * compiler barriers around rcu read lock as real memory barriers.
157 if (list_empty(®istry
))
160 * pthread_kill has a smp_mb(). But beware, we assume it performs
161 * a cache flush on architectures with non-coherent cache. Let's play
162 * safe and don't assume anything : we use smp_mc() to make sure the
163 * cache flush is enforced.
165 list_for_each_entry(index
, ®istry
, head
) {
167 smp_mc(); /* write need_mb before sending the signal */
168 pthread_kill(index
->tid
, SIGRCU
);
171 * Wait for sighandler (and thus mb()) to execute on every thread.
173 * Note that the pthread_kill() will never be executed on systems
174 * that correctly deliver signals in a timely manner. However, it
175 * is not uncommon for kernels to have bugs that can result in
176 * lost or unduly delayed signals.
178 * If you are seeing the below pthread_kill() executing much at
179 * all, we suggest testing the underlying kernel and filing the
180 * relevant bug report. For Linux kernels, we recommend getting
181 * the Linux Test Project (LTP).
183 list_for_each_entry(index
, ®istry
, head
) {
184 while (index
->need_mb
) {
185 pthread_kill(index
->tid
, SIGRCU
);
189 smp_mb(); /* read ->need_mb before ending the barrier */
192 static void smp_mb_heavy(void)
194 force_mb_all_readers();
196 #endif /* #ifdef RCU_SIGNAL */
199 * synchronize_rcu() waiting. Single thread.
201 static void wait_gp(void)
203 /* Read reader_gp before read futex */
205 if (uatomic_read(&gp_futex
) == -1)
206 futex_async(&gp_futex
, FUTEX_WAIT
, -1,
210 void wait_for_quiescent_state(void)
212 LIST_HEAD(qsreaders
);
214 struct rcu_reader
*index
, *tmp
;
216 if (list_empty(®istry
))
219 * Wait for each thread rcu_reader.ctr count to become 0.
223 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
224 uatomic_dec(&gp_futex
);
225 /* Write futex before read reader_gp */
229 list_for_each_entry_safe(index
, tmp
, ®istry
, head
) {
230 if (!rcu_old_gp_ongoing(&index
->ctr
))
231 list_move(&index
->head
, &qsreaders
);
234 #ifndef HAS_INCOHERENT_CACHES
235 if (list_empty(®istry
)) {
236 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
237 /* Read reader_gp before write futex */
239 uatomic_set(&gp_futex
, 0);
243 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
248 #else /* #ifndef HAS_INCOHERENT_CACHES */
250 * BUSY-LOOP. Force the reader thread to commit its
251 * rcu_reader.ctr update to memory if we wait for too long.
253 if (list_empty(®istry
)) {
254 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
255 /* Read reader_gp before write futex */
257 uatomic_set(&gp_futex
, 0);
261 switch (wait_loops
) {
262 case RCU_QS_ACTIVE_ATTEMPTS
:
264 break; /* only escape switch */
265 case KICK_READER_LOOPS
:
268 break; /* only escape switch */
273 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
275 /* put back the reader list in the registry */
276 list_splice(&qsreaders
, ®istry
);
279 void synchronize_rcu(void)
281 mutex_lock(&rcu_gp_lock
);
283 /* All threads should read qparity before accessing data structure
284 * where new ptr points to. Must be done within rcu_gp_lock because it
285 * iterates on reader threads.*/
286 /* Write new ptr before changing the qparity */
289 switch_next_rcu_qparity(); /* 0 -> 1 */
292 * Must commit qparity update to memory before waiting for parity
293 * 0 quiescent state. Failure to do so could result in the writer
294 * waiting forever while new readers are always accessing data (no
296 * Ensured by STORE_SHARED and LOAD_SHARED.
300 * Adding a smp_mb() which is _not_ formally required, but makes the
301 * model easier to understand. It does not have a big performance impact
302 * anyway, given this is the write-side.
307 * Wait for previous parity to be empty of readers.
309 wait_for_quiescent_state(); /* Wait readers in parity 0 */
312 * Must finish waiting for quiescent state for parity 0 before
313 * committing qparity update to memory. Failure to do so could result in
314 * the writer waiting forever while new readers are always accessing
315 * data (no progress).
316 * Ensured by STORE_SHARED and LOAD_SHARED.
320 * Adding a smp_mb() which is _not_ formally required, but makes the
321 * model easier to understand. It does not have a big performance impact
322 * anyway, given this is the write-side.
326 switch_next_rcu_qparity(); /* 1 -> 0 */
329 * Must commit qparity update to memory before waiting for parity
330 * 1 quiescent state. Failure to do so could result in the writer
331 * waiting forever while new readers are always accessing data (no
333 * Ensured by STORE_SHARED and LOAD_SHARED.
337 * Adding a smp_mb() which is _not_ formally required, but makes the
338 * model easier to understand. It does not have a big performance impact
339 * anyway, given this is the write-side.
344 * Wait for previous parity to be empty of readers.
346 wait_for_quiescent_state(); /* Wait readers in parity 1 */
348 /* Finish waiting for reader threads before letting the old ptr being
349 * freed. Must be done within rcu_gp_lock because it iterates on reader
353 mutex_unlock(&rcu_gp_lock
);
357 * library wrappers to be used by non-LGPL compatible source code.
360 void rcu_read_lock(void)
365 void rcu_read_unlock(void)
370 void rcu_register_thread(void)
372 rcu_reader
.tid
= pthread_self();
373 assert(rcu_reader
.need_mb
== 0);
374 assert(rcu_reader
.ctr
== 0);
376 mutex_lock(&rcu_gp_lock
);
377 rcu_init(); /* In case gcc does not support constructor attribute */
378 list_add(&rcu_reader
.head
, ®istry
);
379 mutex_unlock(&rcu_gp_lock
);
382 void rcu_unregister_thread(void)
384 mutex_lock(&rcu_gp_lock
);
385 list_del(&rcu_reader
.head
);
386 mutex_unlock(&rcu_gp_lock
);
389 #ifdef RCU_MEMBARRIER
395 if (!membarrier(MEMBARRIER_EXPEDITED
| MEMBARRIER_QUERY
))
396 has_sys_membarrier
= 1;
401 static void sigrcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
404 * Executing this smp_mb() is the only purpose of this signal handler.
405 * It punctually promotes barrier() into smp_mb() on every thread it is
409 rcu_reader
.need_mb
= 0;
414 * rcu_init constructor. Called when the library is linked, but also when
415 * reader threads are calling rcu_register_thread().
416 * Should only be called by a single thread at a given time. This is ensured by
417 * holing the rcu_gp_lock from rcu_register_thread() or by running at library
418 * load time, which should not be executed by multiple threads nor concurrently
419 * with rcu_register_thread() anyway.
423 struct sigaction act
;
430 act
.sa_sigaction
= sigrcu_handler
;
431 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
432 sigemptyset(&act
.sa_mask
);
433 ret
= sigaction(SIGRCU
, &act
, NULL
);
435 perror("Error in sigaction");
442 struct sigaction act
;
445 ret
= sigaction(SIGRCU
, NULL
, &act
);
447 perror("Error in sigaction");
450 assert(act
.sa_sigaction
== sigrcu_handler
);
451 assert(list_empty(®istry
));
453 #endif /* #ifdef RCU_SIGNAL */