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.
38 #include "urcu/map/urcu.h"
40 #include "urcu/static/urcu.h"
41 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
46 int has_sys_membarrier
;
48 void __attribute__((constructor
)) rcu_init(void);
60 void __attribute__((constructor
)) rcu_init(void);
61 void __attribute__((destructor
)) rcu_exit(void);
64 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
69 * Global grace period counter.
70 * Contains the current RCU_GP_CTR_PHASE.
71 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
72 * Written to only by writer with mutex taken. Read by both writer and readers.
74 unsigned long rcu_gp_ctr
= RCU_GP_COUNT
;
77 * Written to only by each individual reader. Read by both the reader and the
80 struct rcu_reader __thread rcu_reader
;
83 unsigned int yield_active
;
84 unsigned int __thread rand_yield
;
87 static CDS_LIST_HEAD(registry
);
89 static void mutex_lock(pthread_mutex_t
*mutex
)
93 #ifndef DISTRUST_SIGNALS_EXTREME
94 ret
= pthread_mutex_lock(mutex
);
96 perror("Error in pthread mutex lock");
99 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
100 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
101 if (ret
!= EBUSY
&& ret
!= EINTR
) {
102 printf("ret = %d, errno = %d\n", ret
, errno
);
103 perror("Error in pthread mutex lock");
106 if (CMM_LOAD_SHARED(rcu_reader
.need_mb
)) {
108 _CMM_STORE_SHARED(rcu_reader
.need_mb
, 0);
113 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
116 static void mutex_unlock(pthread_mutex_t
*mutex
)
120 ret
= pthread_mutex_unlock(mutex
);
122 perror("Error in pthread mutex unlock");
127 #ifdef RCU_MEMBARRIER
128 static void smp_mb_master(int group
)
130 if (likely(has_sys_membarrier
))
131 membarrier(MEMBARRIER_EXPEDITED
);
138 static void smp_mb_master(int group
)
145 static void force_mb_all_readers(void)
147 struct rcu_reader
*index
;
150 * Ask for each threads to execute a cmm_smp_mb() so we can consider the
151 * compiler barriers around rcu read lock as real memory barriers.
153 if (cds_list_empty(®istry
))
156 * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs
157 * a cache flush on architectures with non-coherent cache. Let's play
158 * safe and don't assume anything : we use cmm_smp_mc() to make sure the
159 * cache flush is enforced.
161 cds_list_for_each_entry(index
, ®istry
, node
) {
162 CMM_STORE_SHARED(index
->need_mb
, 1);
163 pthread_kill(index
->tid
, SIGRCU
);
166 * Wait for sighandler (and thus mb()) to execute on every thread.
168 * Note that the pthread_kill() will never be executed on systems
169 * that correctly deliver signals in a timely manner. However, it
170 * is not uncommon for kernels to have bugs that can result in
171 * lost or unduly delayed signals.
173 * If you are seeing the below pthread_kill() executing much at
174 * all, we suggest testing the underlying kernel and filing the
175 * relevant bug report. For Linux kernels, we recommend getting
176 * the Linux Test Project (LTP).
178 cds_list_for_each_entry(index
, ®istry
, node
) {
179 while (CMM_LOAD_SHARED(index
->need_mb
)) {
180 pthread_kill(index
->tid
, SIGRCU
);
184 cmm_smp_mb(); /* read ->need_mb before ending the barrier */
187 static void smp_mb_master(int group
)
189 force_mb_all_readers();
191 #endif /* #ifdef RCU_SIGNAL */
194 * synchronize_rcu() waiting. Single thread.
196 static void wait_gp(void)
198 /* Read reader_gp before read futex */
199 smp_mb_master(RCU_MB_GROUP
);
200 if (uatomic_read(&gp_futex
) == -1)
201 futex_async(&gp_futex
, FUTEX_WAIT
, -1,
205 void update_counter_and_wait(void)
207 CDS_LIST_HEAD(qsreaders
);
209 struct rcu_reader
*index
, *tmp
;
211 /* Switch parity: 0 -> 1, 1 -> 0 */
212 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
215 * Must commit rcu_gp_ctr update to memory before waiting for quiescent
216 * state. Failure to do so could result in the writer waiting forever
217 * while new readers are always accessing data (no progress). Enforce
218 * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr.
224 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
225 * model easier to understand. It does not have a big performance impact
226 * anyway, given this is the write-side.
231 * Wait for each thread rcu_reader.ctr count to become 0.
235 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
236 uatomic_dec(&gp_futex
);
237 /* Write futex before read reader_gp */
238 smp_mb_master(RCU_MB_GROUP
);
241 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
242 if (!rcu_gp_ongoing(&index
->ctr
))
243 cds_list_move(&index
->node
, &qsreaders
);
246 #ifndef HAS_INCOHERENT_CACHES
247 if (cds_list_empty(®istry
)) {
248 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
249 /* Read reader_gp before write futex */
250 smp_mb_master(RCU_MB_GROUP
);
251 uatomic_set(&gp_futex
, 0);
255 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
260 #else /* #ifndef HAS_INCOHERENT_CACHES */
262 * BUSY-LOOP. Force the reader thread to commit its
263 * rcu_reader.ctr update to memory if we wait for too long.
265 if (cds_list_empty(®istry
)) {
266 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
267 /* Read reader_gp before write futex */
268 smp_mb_master(RCU_MB_GROUP
);
269 uatomic_set(&gp_futex
, 0);
273 switch (wait_loops
) {
274 case RCU_QS_ACTIVE_ATTEMPTS
:
276 break; /* only escape switch */
277 case KICK_READER_LOOPS
:
278 smp_mb_master(RCU_MB_GROUP
);
280 break; /* only escape switch */
285 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
287 /* put back the reader list in the registry */
288 cds_list_splice(&qsreaders
, ®istry
);
291 void synchronize_rcu(void)
293 mutex_lock(&rcu_gp_lock
);
295 if (cds_list_empty(®istry
))
298 /* All threads should read qparity before accessing data structure
299 * where new ptr points to. Must be done within rcu_gp_lock because it
300 * iterates on reader threads.*/
301 /* Write new ptr before changing the qparity */
302 smp_mb_master(RCU_MB_GROUP
);
305 * Wait for previous parity to be empty of readers.
307 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
310 * Must finish waiting for quiescent state for parity 0 before
311 * committing next rcu_gp_ctr update to memory. Failure to do so could
312 * result in the writer waiting forever while new readers are always
313 * accessing data (no progress). Enforce compiler-order of load
314 * rcu_reader ctr before store to rcu_gp_ctr.
319 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
320 * model easier to understand. It does not have a big performance impact
321 * anyway, given this is the write-side.
326 * Wait for previous parity to be empty of readers.
328 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
330 /* Finish waiting for reader threads before letting the old ptr being
331 * freed. Must be done within rcu_gp_lock because it iterates on reader
333 smp_mb_master(RCU_MB_GROUP
);
335 mutex_unlock(&rcu_gp_lock
);
339 * library wrappers to be used by non-LGPL compatible source code.
342 void rcu_read_lock(void)
347 void rcu_read_unlock(void)
352 void rcu_register_thread(void)
354 rcu_reader
.tid
= pthread_self();
355 assert(rcu_reader
.need_mb
== 0);
356 assert(!(rcu_reader
.ctr
& RCU_GP_CTR_NEST_MASK
));
358 mutex_lock(&rcu_gp_lock
);
359 rcu_init(); /* In case gcc does not support constructor attribute */
360 cds_list_add(&rcu_reader
.node
, ®istry
);
361 mutex_unlock(&rcu_gp_lock
);
364 void rcu_unregister_thread(void)
366 mutex_lock(&rcu_gp_lock
);
367 cds_list_del(&rcu_reader
.node
);
368 mutex_unlock(&rcu_gp_lock
);
371 #ifdef RCU_MEMBARRIER
377 if (!membarrier(MEMBARRIER_EXPEDITED
| MEMBARRIER_QUERY
))
378 has_sys_membarrier
= 1;
383 static void sigrcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
386 * Executing this cmm_smp_mb() is the only purpose of this signal handler.
387 * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
391 _CMM_STORE_SHARED(rcu_reader
.need_mb
, 0);
396 * rcu_init constructor. Called when the library is linked, but also when
397 * reader threads are calling rcu_register_thread().
398 * Should only be called by a single thread at a given time. This is ensured by
399 * holing the rcu_gp_lock from rcu_register_thread() or by running at library
400 * load time, which should not be executed by multiple threads nor concurrently
401 * with rcu_register_thread() anyway.
405 struct sigaction act
;
412 act
.sa_sigaction
= sigrcu_handler
;
413 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
414 sigemptyset(&act
.sa_mask
);
415 ret
= sigaction(SIGRCU
, &act
, NULL
);
417 perror("Error in sigaction");
424 struct sigaction act
;
427 ret
= sigaction(SIGRCU
, NULL
, &act
);
429 perror("Error in sigaction");
432 assert(act
.sa_sigaction
== sigrcu_handler
);
433 assert(cds_list_empty(®istry
));
436 #endif /* #ifdef RCU_SIGNAL */
438 #include "urcu-call-rcu-impl.h"
439 #include "urcu-defer-impl.h"