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"
43 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
49 * If a reader is really non-cooperative and refuses to commit its
50 * rcu_active_readers count to memory (there is no barrier in the reader
51 * per-se), kick it after a few loops waiting for it.
53 #define KICK_READER_LOOPS 10000
56 * Active attempts to check for reader Q.S. before calling futex().
58 #define RCU_QS_ACTIVE_ATTEMPTS 100
62 int has_sys_membarrier
;
64 void __attribute__((constructor
)) rcu_init(void);
76 void __attribute__((constructor
)) rcu_init(void);
77 void __attribute__((destructor
)) rcu_exit(void);
80 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
85 * Global grace period counter.
86 * Contains the current RCU_GP_CTR_PHASE.
87 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
88 * Written to only by writer with mutex taken. Read by both writer and readers.
90 unsigned long rcu_gp_ctr
= RCU_GP_COUNT
;
93 * Written to only by each individual reader. Read by both the reader and the
96 struct rcu_reader __thread rcu_reader
;
99 unsigned int yield_active
;
100 unsigned int __thread rand_yield
;
103 static CDS_LIST_HEAD(registry
);
105 static void mutex_lock(pthread_mutex_t
*mutex
)
109 #ifndef DISTRUST_SIGNALS_EXTREME
110 ret
= pthread_mutex_lock(mutex
);
112 perror("Error in pthread mutex lock");
115 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
116 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
117 if (ret
!= EBUSY
&& ret
!= EINTR
) {
118 printf("ret = %d, errno = %d\n", ret
, errno
);
119 perror("Error in pthread mutex lock");
122 if (CMM_LOAD_SHARED(rcu_reader
.need_mb
)) {
124 _CMM_STORE_SHARED(rcu_reader
.need_mb
, 0);
129 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
132 static void mutex_unlock(pthread_mutex_t
*mutex
)
136 ret
= pthread_mutex_unlock(mutex
);
138 perror("Error in pthread mutex unlock");
143 #ifdef RCU_MEMBARRIER
144 static void smp_mb_master(int group
)
146 if (likely(has_sys_membarrier
))
147 membarrier(MEMBARRIER_EXPEDITED
);
154 static void smp_mb_master(int group
)
161 static void force_mb_all_readers(void)
163 struct rcu_reader
*index
;
166 * Ask for each threads to execute a cmm_smp_mb() so we can consider the
167 * compiler barriers around rcu read lock as real memory barriers.
169 if (cds_list_empty(®istry
))
172 * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs
173 * a cache flush on architectures with non-coherent cache. Let's play
174 * safe and don't assume anything : we use cmm_smp_mc() to make sure the
175 * cache flush is enforced.
177 cds_list_for_each_entry(index
, ®istry
, node
) {
178 CMM_STORE_SHARED(index
->need_mb
, 1);
179 pthread_kill(index
->tid
, SIGRCU
);
182 * Wait for sighandler (and thus mb()) to execute on every thread.
184 * Note that the pthread_kill() will never be executed on systems
185 * that correctly deliver signals in a timely manner. However, it
186 * is not uncommon for kernels to have bugs that can result in
187 * lost or unduly delayed signals.
189 * If you are seeing the below pthread_kill() executing much at
190 * all, we suggest testing the underlying kernel and filing the
191 * relevant bug report. For Linux kernels, we recommend getting
192 * the Linux Test Project (LTP).
194 cds_list_for_each_entry(index
, ®istry
, node
) {
195 while (CMM_LOAD_SHARED(index
->need_mb
)) {
196 pthread_kill(index
->tid
, SIGRCU
);
200 cmm_smp_mb(); /* read ->need_mb before ending the barrier */
203 static void smp_mb_master(int group
)
205 force_mb_all_readers();
207 #endif /* #ifdef RCU_SIGNAL */
210 * synchronize_rcu() waiting. Single thread.
212 static void wait_gp(void)
214 /* Read reader_gp before read futex */
215 smp_mb_master(RCU_MB_GROUP
);
216 if (uatomic_read(&gp_futex
) == -1)
217 futex_async(&gp_futex
, FUTEX_WAIT
, -1,
221 void update_counter_and_wait(void)
223 CDS_LIST_HEAD(qsreaders
);
225 struct rcu_reader
*index
, *tmp
;
227 /* Switch parity: 0 -> 1, 1 -> 0 */
228 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
231 * Must commit rcu_gp_ctr update to memory before waiting for quiescent
232 * state. Failure to do so could result in the writer waiting forever
233 * while new readers are always accessing data (no progress). Enforce
234 * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr.
240 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
241 * model easier to understand. It does not have a big performance impact
242 * anyway, given this is the write-side.
247 * Wait for each thread rcu_reader.ctr count to become 0.
251 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
252 uatomic_dec(&gp_futex
);
253 /* Write futex before read reader_gp */
254 smp_mb_master(RCU_MB_GROUP
);
257 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
258 if (!rcu_gp_ongoing(&index
->ctr
))
259 cds_list_move(&index
->node
, &qsreaders
);
262 #ifndef HAS_INCOHERENT_CACHES
263 if (cds_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 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
276 #else /* #ifndef HAS_INCOHERENT_CACHES */
278 * BUSY-LOOP. Force the reader thread to commit its
279 * rcu_reader.ctr update to memory if we wait for too long.
281 if (cds_list_empty(®istry
)) {
282 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
283 /* Read reader_gp before write futex */
284 smp_mb_master(RCU_MB_GROUP
);
285 uatomic_set(&gp_futex
, 0);
289 switch (wait_loops
) {
290 case RCU_QS_ACTIVE_ATTEMPTS
:
292 break; /* only escape switch */
293 case KICK_READER_LOOPS
:
294 smp_mb_master(RCU_MB_GROUP
);
296 break; /* only escape switch */
301 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
303 /* put back the reader list in the registry */
304 cds_list_splice(&qsreaders
, ®istry
);
307 void synchronize_rcu(void)
309 mutex_lock(&rcu_gp_lock
);
311 if (cds_list_empty(®istry
))
314 /* All threads should read qparity before accessing data structure
315 * where new ptr points to. Must be done within rcu_gp_lock because it
316 * iterates on reader threads.*/
317 /* Write new ptr before changing the qparity */
318 smp_mb_master(RCU_MB_GROUP
);
321 * Wait for previous parity to be empty of readers.
323 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
326 * Must finish waiting for quiescent state for parity 0 before
327 * committing next rcu_gp_ctr update to memory. Failure to do so could
328 * result in the writer waiting forever while new readers are always
329 * accessing data (no progress). Enforce compiler-order of load
330 * rcu_reader ctr before store to rcu_gp_ctr.
335 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
336 * model easier to understand. It does not have a big performance impact
337 * anyway, given this is the write-side.
342 * Wait for previous parity to be empty of readers.
344 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
346 /* Finish waiting for reader threads before letting the old ptr being
347 * freed. Must be done within rcu_gp_lock because it iterates on reader
349 smp_mb_master(RCU_MB_GROUP
);
351 mutex_unlock(&rcu_gp_lock
);
355 * library wrappers to be used by non-LGPL compatible source code.
358 void rcu_read_lock(void)
363 void rcu_read_unlock(void)
368 void rcu_register_thread(void)
370 rcu_reader
.tid
= pthread_self();
371 assert(rcu_reader
.need_mb
== 0);
372 assert(!(rcu_reader
.ctr
& RCU_GP_CTR_NEST_MASK
));
374 mutex_lock(&rcu_gp_lock
);
375 rcu_init(); /* In case gcc does not support constructor attribute */
376 cds_list_add(&rcu_reader
.node
, ®istry
);
377 mutex_unlock(&rcu_gp_lock
);
380 void rcu_unregister_thread(void)
382 mutex_lock(&rcu_gp_lock
);
383 cds_list_del(&rcu_reader
.node
);
384 mutex_unlock(&rcu_gp_lock
);
387 #ifdef RCU_MEMBARRIER
393 if (!membarrier(MEMBARRIER_EXPEDITED
| MEMBARRIER_QUERY
))
394 has_sys_membarrier
= 1;
399 static void sigrcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
402 * Executing this cmm_smp_mb() is the only purpose of this signal handler.
403 * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
407 _CMM_STORE_SHARED(rcu_reader
.need_mb
, 0);
412 * rcu_init constructor. Called when the library is linked, but also when
413 * reader threads are calling rcu_register_thread().
414 * Should only be called by a single thread at a given time. This is ensured by
415 * holing the rcu_gp_lock from rcu_register_thread() or by running at library
416 * load time, which should not be executed by multiple threads nor concurrently
417 * with rcu_register_thread() anyway.
421 struct sigaction act
;
428 act
.sa_sigaction
= sigrcu_handler
;
429 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
430 sigemptyset(&act
.sa_mask
);
431 ret
= sigaction(SIGRCU
, &act
, NULL
);
433 perror("Error in sigaction");
440 struct sigaction act
;
443 ret
= sigaction(SIGRCU
, NULL
, &act
);
445 perror("Error in sigaction");
448 assert(act
.sa_sigaction
== sigrcu_handler
);
449 assert(cds_list_empty(®istry
));
452 #endif /* #ifdef RCU_SIGNAL */
454 #include "urcu-call-rcu-impl.h"
455 #include "urcu-defer-impl.h"