4 * Userspace RCU library
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.h"
41 #include "urcu/static/urcu.h"
42 #include "urcu-pointer.h"
44 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
50 * If a reader is really non-cooperative and refuses to commit its
51 * rcu_active_readers count to memory (there is no barrier in the reader
52 * per-se), kick it after a few loops waiting for it.
54 #define KICK_READER_LOOPS 10000
57 * Active attempts to check for reader Q.S. before calling futex().
59 #define RCU_QS_ACTIVE_ATTEMPTS 100
63 int has_sys_membarrier
;
65 void __attribute__((constructor
)) rcu_init(void);
77 void __attribute__((constructor
)) rcu_init(void);
78 void __attribute__((destructor
)) rcu_exit(void);
81 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
86 * Global grace period counter.
87 * Contains the current RCU_GP_CTR_PHASE.
88 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
89 * Written to only by writer with mutex taken. Read by both writer and readers.
91 unsigned long rcu_gp_ctr
= RCU_GP_COUNT
;
94 * Written to only by each individual reader. Read by both the reader and the
97 struct rcu_reader __thread rcu_reader
;
100 unsigned int yield_active
;
101 unsigned int __thread rand_yield
;
104 static CDS_LIST_HEAD(registry
);
106 static void mutex_lock(pthread_mutex_t
*mutex
)
110 #ifndef DISTRUST_SIGNALS_EXTREME
111 ret
= pthread_mutex_lock(mutex
);
113 perror("Error in pthread mutex lock");
116 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
117 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
118 if (ret
!= EBUSY
&& ret
!= EINTR
) {
119 printf("ret = %d, errno = %d\n", ret
, errno
);
120 perror("Error in pthread mutex lock");
123 if (CMM_LOAD_SHARED(rcu_reader
.need_mb
)) {
125 _CMM_STORE_SHARED(rcu_reader
.need_mb
, 0);
130 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
133 static void mutex_unlock(pthread_mutex_t
*mutex
)
137 ret
= pthread_mutex_unlock(mutex
);
139 perror("Error in pthread mutex unlock");
144 #ifdef RCU_MEMBARRIER
145 static void smp_mb_master(int group
)
147 if (likely(has_sys_membarrier
))
148 membarrier(MEMBARRIER_EXPEDITED
);
155 static void smp_mb_master(int group
)
162 static void force_mb_all_readers(void)
164 struct rcu_reader
*index
;
167 * Ask for each threads to execute a cmm_smp_mb() so we can consider the
168 * compiler barriers around rcu read lock as real memory barriers.
170 if (cds_list_empty(®istry
))
173 * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs
174 * a cache flush on architectures with non-coherent cache. Let's play
175 * safe and don't assume anything : we use cmm_smp_mc() to make sure the
176 * cache flush is enforced.
178 cds_list_for_each_entry(index
, ®istry
, node
) {
179 CMM_STORE_SHARED(index
->need_mb
, 1);
180 pthread_kill(index
->tid
, SIGRCU
);
183 * Wait for sighandler (and thus mb()) to execute on every thread.
185 * Note that the pthread_kill() will never be executed on systems
186 * that correctly deliver signals in a timely manner. However, it
187 * is not uncommon for kernels to have bugs that can result in
188 * lost or unduly delayed signals.
190 * If you are seeing the below pthread_kill() executing much at
191 * all, we suggest testing the underlying kernel and filing the
192 * relevant bug report. For Linux kernels, we recommend getting
193 * the Linux Test Project (LTP).
195 cds_list_for_each_entry(index
, ®istry
, node
) {
196 while (CMM_LOAD_SHARED(index
->need_mb
)) {
197 pthread_kill(index
->tid
, SIGRCU
);
201 cmm_smp_mb(); /* read ->need_mb before ending the barrier */
204 static void smp_mb_master(int group
)
206 force_mb_all_readers();
208 #endif /* #ifdef RCU_SIGNAL */
211 * synchronize_rcu() waiting. Single thread.
213 static void wait_gp(void)
215 /* Read reader_gp before read futex */
216 smp_mb_master(RCU_MB_GROUP
);
217 if (uatomic_read(&gp_futex
) == -1)
218 futex_async(&gp_futex
, FUTEX_WAIT
, -1,
222 void update_counter_and_wait(void)
224 CDS_LIST_HEAD(qsreaders
);
226 struct rcu_reader
*index
, *tmp
;
228 /* Switch parity: 0 -> 1, 1 -> 0 */
229 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
232 * Must commit rcu_gp_ctr update to memory before waiting for quiescent
233 * state. Failure to do so could result in the writer waiting forever
234 * while new readers are always accessing data (no progress). Enforce
235 * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr.
241 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
242 * model easier to understand. It does not have a big performance impact
243 * anyway, given this is the write-side.
248 * Wait for each thread rcu_reader.ctr count to become 0.
252 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
253 uatomic_dec(&gp_futex
);
254 /* Write futex before read reader_gp */
255 smp_mb_master(RCU_MB_GROUP
);
258 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
259 if (!rcu_gp_ongoing(&index
->ctr
))
260 cds_list_move(&index
->node
, &qsreaders
);
263 #ifndef HAS_INCOHERENT_CACHES
264 if (cds_list_empty(®istry
)) {
265 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
266 /* Read reader_gp before write futex */
267 smp_mb_master(RCU_MB_GROUP
);
268 uatomic_set(&gp_futex
, 0);
272 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
277 #else /* #ifndef HAS_INCOHERENT_CACHES */
279 * BUSY-LOOP. Force the reader thread to commit its
280 * rcu_reader.ctr update to memory if we wait for too long.
282 if (cds_list_empty(®istry
)) {
283 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
284 /* Read reader_gp before write futex */
285 smp_mb_master(RCU_MB_GROUP
);
286 uatomic_set(&gp_futex
, 0);
290 switch (wait_loops
) {
291 case RCU_QS_ACTIVE_ATTEMPTS
:
293 break; /* only escape switch */
294 case KICK_READER_LOOPS
:
295 smp_mb_master(RCU_MB_GROUP
);
297 break; /* only escape switch */
302 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
304 /* put back the reader list in the registry */
305 cds_list_splice(&qsreaders
, ®istry
);
308 void synchronize_rcu(void)
310 mutex_lock(&rcu_gp_lock
);
312 if (cds_list_empty(®istry
))
315 /* All threads should read qparity before accessing data structure
316 * where new ptr points to. Must be done within rcu_gp_lock because it
317 * iterates on reader threads.*/
318 /* Write new ptr before changing the qparity */
319 smp_mb_master(RCU_MB_GROUP
);
322 * Wait for previous parity to be empty of readers.
324 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
327 * Must finish waiting for quiescent state for parity 0 before
328 * committing next rcu_gp_ctr update to memory. Failure to do so could
329 * result in the writer waiting forever while new readers are always
330 * accessing data (no progress). Enforce compiler-order of load
331 * rcu_reader ctr before store to rcu_gp_ctr.
336 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
337 * model easier to understand. It does not have a big performance impact
338 * anyway, given this is the write-side.
343 * Wait for previous parity to be empty of readers.
345 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
347 /* Finish waiting for reader threads before letting the old ptr being
348 * freed. Must be done within rcu_gp_lock because it iterates on reader
350 smp_mb_master(RCU_MB_GROUP
);
352 mutex_unlock(&rcu_gp_lock
);
356 * library wrappers to be used by non-LGPL compatible source code.
359 void rcu_read_lock(void)
364 void rcu_read_unlock(void)
369 void rcu_register_thread(void)
371 rcu_reader
.tid
= pthread_self();
372 assert(rcu_reader
.need_mb
== 0);
373 assert(!(rcu_reader
.ctr
& RCU_GP_CTR_NEST_MASK
));
375 mutex_lock(&rcu_gp_lock
);
376 rcu_init(); /* In case gcc does not support constructor attribute */
377 cds_list_add(&rcu_reader
.node
, ®istry
);
378 mutex_unlock(&rcu_gp_lock
);
381 void rcu_unregister_thread(void)
383 mutex_lock(&rcu_gp_lock
);
384 cds_list_del(&rcu_reader
.node
);
385 mutex_unlock(&rcu_gp_lock
);
388 #ifdef RCU_MEMBARRIER
394 if (!membarrier(MEMBARRIER_EXPEDITED
| MEMBARRIER_QUERY
))
395 has_sys_membarrier
= 1;
400 static void sigrcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
403 * Executing this cmm_smp_mb() is the only purpose of this signal handler.
404 * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
408 _CMM_STORE_SHARED(rcu_reader
.need_mb
, 0);
413 * rcu_init constructor. Called when the library is linked, but also when
414 * reader threads are calling rcu_register_thread().
415 * Should only be called by a single thread at a given time. This is ensured by
416 * holing the rcu_gp_lock from rcu_register_thread() or by running at library
417 * load time, which should not be executed by multiple threads nor concurrently
418 * with rcu_register_thread() anyway.
422 struct sigaction act
;
429 act
.sa_sigaction
= sigrcu_handler
;
430 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
431 sigemptyset(&act
.sa_mask
);
432 ret
= sigaction(SIGRCU
, &act
, NULL
);
434 perror("Error in sigaction");
441 struct sigaction act
;
444 ret
= sigaction(SIGRCU
, NULL
, &act
);
446 perror("Error in sigaction");
449 assert(act
.sa_sigaction
== sigrcu_handler
);
450 assert(cds_list_empty(®istry
));
453 #endif /* #ifdef RCU_SIGNAL */
455 #include "urcu-call-rcu-impl.h"
456 #include "urcu-defer-impl.h"