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 unsigned 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 (LOAD_SHARED(rcu_reader
.need_mb
)) {
104 _STORE_SHARED(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");
123 #ifdef RCU_MEMBARRIER
124 static void smp_mb_master(int group
)
126 if (likely(has_sys_membarrier
))
127 membarrier(MEMBARRIER_EXPEDITED
);
134 static void smp_mb_master(int group
)
141 static void force_mb_all_readers(void)
143 struct rcu_reader
*index
;
146 * Ask for each threads to execute a smp_mb() so we can consider the
147 * compiler barriers around rcu read lock as real memory barriers.
149 if (list_empty(®istry
))
152 * pthread_kill has a smp_mb(). But beware, we assume it performs
153 * a cache flush on architectures with non-coherent cache. Let's play
154 * safe and don't assume anything : we use smp_mc() to make sure the
155 * cache flush is enforced.
157 list_for_each_entry(index
, ®istry
, head
) {
158 STORE_SHARED(index
->need_mb
, 1);
159 pthread_kill(index
->tid
, SIGRCU
);
162 * Wait for sighandler (and thus mb()) to execute on every thread.
164 * Note that the pthread_kill() will never be executed on systems
165 * that correctly deliver signals in a timely manner. However, it
166 * is not uncommon for kernels to have bugs that can result in
167 * lost or unduly delayed signals.
169 * If you are seeing the below pthread_kill() executing much at
170 * all, we suggest testing the underlying kernel and filing the
171 * relevant bug report. For Linux kernels, we recommend getting
172 * the Linux Test Project (LTP).
174 list_for_each_entry(index
, ®istry
, head
) {
175 while (LOAD_SHARED(index
->need_mb
)) {
176 pthread_kill(index
->tid
, SIGRCU
);
180 smp_mb(); /* read ->need_mb before ending the barrier */
183 static void smp_mb_master(int group
)
185 force_mb_all_readers();
187 #endif /* #ifdef RCU_SIGNAL */
190 * synchronize_rcu() waiting. Single thread.
192 static void wait_gp(void)
194 /* Read reader_gp before read futex */
195 smp_mb_master(RCU_MB_GROUP
);
196 if (uatomic_read(&gp_futex
) == -1)
197 futex_async(&gp_futex
, FUTEX_WAIT
, -1,
201 void update_counter_and_wait(void)
203 LIST_HEAD(qsreaders
);
205 struct rcu_reader
*index
, *tmp
;
207 /* Switch parity: 0 -> 1, 1 -> 0 */
208 STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
211 * Must commit qparity update to memory before waiting for other parity
212 * quiescent state. Failure to do so could result in the writer waiting
213 * forever while new readers are always accessing data (no progress).
214 * Ensured by STORE_SHARED and LOAD_SHARED.
218 * Enforce compiler-order of store to rcu_gp_ctr before before
219 * load rcu_reader ctr.
220 * This ensures synchronize_rcu() cannot be starved by readers.
222 * Adding a smp_mb() which is _not_ formally required, but makes the
223 * model easier to understand. It does not have a big performance impact
224 * anyway, given this is the write-side.
229 * Wait for each thread rcu_reader.ctr count to become 0.
233 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
234 uatomic_dec(&gp_futex
);
235 /* Write futex before read reader_gp */
236 smp_mb_master(RCU_MB_GROUP
);
239 list_for_each_entry_safe(index
, tmp
, ®istry
, head
) {
240 if (!rcu_gp_ongoing(&index
->ctr
))
241 list_move(&index
->head
, &qsreaders
);
244 #ifndef HAS_INCOHERENT_CACHES
245 if (list_empty(®istry
)) {
246 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
247 /* Read reader_gp before write futex */
248 smp_mb_master(RCU_MB_GROUP
);
249 uatomic_set(&gp_futex
, 0);
253 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
258 #else /* #ifndef HAS_INCOHERENT_CACHES */
260 * BUSY-LOOP. Force the reader thread to commit its
261 * rcu_reader.ctr update to memory if we wait for too long.
263 if (list_empty(®istry
)) {
264 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
265 /* Read reader_gp before write futex */
266 smp_mb_master(RCU_MB_GROUP
);
267 uatomic_set(&gp_futex
, 0);
271 switch (wait_loops
) {
272 case RCU_QS_ACTIVE_ATTEMPTS
:
274 break; /* only escape switch */
275 case KICK_READER_LOOPS
:
276 smp_mb_master(RCU_MB_GROUP
);
278 break; /* only escape switch */
283 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
285 /* put back the reader list in the registry */
286 list_splice(&qsreaders
, ®istry
);
289 void synchronize_rcu(void)
291 mutex_lock(&rcu_gp_lock
);
293 if (list_empty(®istry
))
296 /* All threads should read qparity before accessing data structure
297 * where new ptr points to. Must be done within rcu_gp_lock because it
298 * iterates on reader threads.*/
299 /* Write new ptr before changing the qparity */
300 smp_mb_master(RCU_MB_GROUP
);
303 * Wait for previous parity to be empty of readers.
305 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
308 * Must finish waiting for quiescent state for parity 0 before
309 * committing qparity update to memory. Failure to do so could result in
310 * the writer waiting forever while new readers are always accessing
311 * data (no progress).
312 * Ensured by STORE_SHARED and LOAD_SHARED.
316 * Adding a smp_mb() which is _not_ formally required, but makes the
317 * model easier to understand. It does not have a big performance impact
318 * anyway, given this is the write-side.
323 * Wait for previous parity to be empty of readers.
325 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
327 /* Finish waiting for reader threads before letting the old ptr being
328 * freed. Must be done within rcu_gp_lock because it iterates on reader
330 smp_mb_master(RCU_MB_GROUP
);
332 mutex_unlock(&rcu_gp_lock
);
336 * library wrappers to be used by non-LGPL compatible source code.
339 void rcu_read_lock(void)
344 void rcu_read_unlock(void)
349 void rcu_register_thread(void)
351 rcu_reader
.tid
= pthread_self();
352 assert(rcu_reader
.need_mb
== 0);
353 assert(!(rcu_reader
.ctr
& RCU_GP_CTR_NEST_MASK
));
355 mutex_lock(&rcu_gp_lock
);
356 rcu_init(); /* In case gcc does not support constructor attribute */
357 list_add(&rcu_reader
.head
, ®istry
);
358 mutex_unlock(&rcu_gp_lock
);
361 void rcu_unregister_thread(void)
363 mutex_lock(&rcu_gp_lock
);
364 list_del(&rcu_reader
.head
);
365 mutex_unlock(&rcu_gp_lock
);
368 #ifdef RCU_MEMBARRIER
374 if (!membarrier(MEMBARRIER_EXPEDITED
| MEMBARRIER_QUERY
))
375 has_sys_membarrier
= 1;
380 static void sigrcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
383 * Executing this smp_mb() is the only purpose of this signal handler.
384 * It punctually promotes barrier() into smp_mb() on every thread it is
388 _STORE_SHARED(rcu_reader
.need_mb
, 0);
393 * rcu_init constructor. Called when the library is linked, but also when
394 * reader threads are calling rcu_register_thread().
395 * Should only be called by a single thread at a given time. This is ensured by
396 * holing the rcu_gp_lock from rcu_register_thread() or by running at library
397 * load time, which should not be executed by multiple threads nor concurrently
398 * with rcu_register_thread() anyway.
402 struct sigaction act
;
409 act
.sa_sigaction
= sigrcu_handler
;
410 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
411 sigemptyset(&act
.sa_mask
);
412 ret
= sigaction(SIGRCU
, &act
, NULL
);
414 perror("Error in sigaction");
421 struct sigaction act
;
424 ret
= sigaction(SIGRCU
, NULL
, &act
);
426 perror("Error in sigaction");
429 assert(act
.sa_sigaction
== sigrcu_handler
);
430 assert(list_empty(®istry
));
432 #endif /* #ifdef RCU_SIGNAL */