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.
35 #include "urcu-static.h"
36 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
42 void __attribute__((constructor
)) urcu_init(void);
43 void __attribute__((destructor
)) urcu_exit(void);
50 static pthread_mutex_t urcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
55 * Global grace period counter.
56 * Contains the current RCU_GP_CTR_BIT.
57 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
58 * Written to only by writer with mutex taken. Read by both writer and readers.
60 long urcu_gp_ctr
= RCU_GP_COUNT
;
63 * Written to only by each individual reader. Read by both the reader and the
66 struct urcu_reader __thread urcu_reader
;
69 unsigned int yield_active
;
70 unsigned int __thread rand_yield
;
73 static LIST_HEAD(registry
);
75 static void internal_urcu_lock(void)
79 #ifndef DISTRUST_SIGNALS_EXTREME
80 ret
= pthread_mutex_lock(&urcu_mutex
);
82 perror("Error in pthread mutex lock");
85 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
86 while ((ret
= pthread_mutex_trylock(&urcu_mutex
)) != 0) {
87 if (ret
!= EBUSY
&& ret
!= EINTR
) {
88 printf("ret = %d, errno = %d\n", ret
, errno
);
89 perror("Error in pthread mutex lock");
92 if (urcu_reader
.need_mb
) {
94 urcu_reader
.need_mb
= 0;
99 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
102 static void internal_urcu_unlock(void)
106 ret
= pthread_mutex_unlock(&urcu_mutex
);
108 perror("Error in pthread mutex unlock");
114 * called with urcu_mutex held.
116 static void switch_next_urcu_qparity(void)
118 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_CTR_BIT
);
123 static void force_mb_single_thread(struct urcu_reader
*index
)
129 static void force_mb_all_threads(void)
133 #else /* #ifdef URCU_MB */
135 static void force_mb_single_thread(struct urcu_reader
*index
)
137 assert(!list_empty(®istry
));
139 * pthread_kill has a smp_mb(). But beware, we assume it performs
140 * a cache flush on architectures with non-coherent cache. Let's play
141 * safe and don't assume anything : we use smp_mc() to make sure the
142 * cache flush is enforced.
145 smp_mc(); /* write ->need_mb before sending the signals */
146 pthread_kill(index
->tid
, SIGURCU
);
149 * Wait for sighandler (and thus mb()) to execute on every thread.
152 while (index
->need_mb
) {
155 smp_mb(); /* read ->need_mb before ending the barrier */
159 static void force_mb_all_threads(void)
161 struct urcu_reader
*index
;
164 * Ask for each threads to execute a smp_mb() so we can consider the
165 * compiler barriers around rcu read lock as real memory barriers.
167 if (list_empty(®istry
))
170 * pthread_kill has a smp_mb(). But beware, we assume it performs
171 * a cache flush on architectures with non-coherent cache. Let's play
172 * safe and don't assume anything : we use smp_mc() to make sure the
173 * cache flush is enforced.
175 list_for_each_entry(index
, ®istry
, head
) {
177 smp_mc(); /* write need_mb before sending the signal */
178 pthread_kill(index
->tid
, SIGURCU
);
181 * Wait for sighandler (and thus mb()) to execute on every thread.
183 * Note that the pthread_kill() will never be executed on systems
184 * that correctly deliver signals in a timely manner. However, it
185 * is not uncommon for kernels to have bugs that can result in
186 * lost or unduly delayed signals.
188 * If you are seeing the below pthread_kill() executing much at
189 * all, we suggest testing the underlying kernel and filing the
190 * relevant bug report. For Linux kernels, we recommend getting
191 * the Linux Test Project (LTP).
193 list_for_each_entry(index
, ®istry
, head
) {
194 while (index
->need_mb
) {
195 pthread_kill(index
->tid
, SIGURCU
);
199 smp_mb(); /* read ->need_mb before ending the barrier */
201 #endif /* #else #ifdef URCU_MB */
204 * synchronize_rcu() waiting. Single thread.
206 static void wait_gp(void)
208 /* Read reader_gp before read futex */
209 force_mb_all_threads();
210 if (uatomic_read(&gp_futex
) == -1)
211 futex_async(&gp_futex
, FUTEX_WAIT
, -1,
215 void wait_for_quiescent_state(void)
217 LIST_HEAD(qsreaders
);
219 struct urcu_reader
*index
, *tmp
;
221 if (list_empty(®istry
))
224 * Wait for each thread urcu_reader.ctr count to become 0.
228 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
229 uatomic_dec(&gp_futex
);
230 /* Write futex before read reader_gp */
231 force_mb_all_threads();
234 list_for_each_entry_safe(index
, tmp
, ®istry
, head
) {
235 if (!rcu_old_gp_ongoing(&index
->ctr
))
236 list_move(&index
->head
, &qsreaders
);
239 #ifndef HAS_INCOHERENT_CACHES
240 if (list_empty(®istry
)) {
241 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
242 /* Read reader_gp before write futex */
243 force_mb_all_threads();
244 uatomic_set(&gp_futex
, 0);
248 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
)
253 #else /* #ifndef HAS_INCOHERENT_CACHES */
255 * BUSY-LOOP. Force the reader thread to commit its
256 * urcu_reader.ctr update to memory if we wait for too long.
258 if (list_empty(®istry
)) {
259 if (wait_loops
== RCU_QS_ACTIVE_ATTEMPTS
) {
260 /* Read reader_gp before write futex */
261 force_mb_all_threads();
262 uatomic_set(&gp_futex
, 0);
266 switch (wait_loops
) {
267 case RCU_QS_ACTIVE_ATTEMPTS
:
269 break; /* only escape switch */
270 case KICK_READER_LOOPS
:
271 force_mb_all_threads();
273 break; /* only escape switch */
278 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
280 /* put back the reader list in the registry */
281 list_splice(&qsreaders
, ®istry
);
284 void synchronize_rcu(void)
286 internal_urcu_lock();
288 /* All threads should read qparity before accessing data structure
289 * where new ptr points to. Must be done within internal_urcu_lock
290 * because it iterates on reader threads.*/
291 /* Write new ptr before changing the qparity */
292 force_mb_all_threads();
294 switch_next_urcu_qparity(); /* 0 -> 1 */
297 * Must commit qparity update to memory before waiting for parity
298 * 0 quiescent state. Failure to do so could result in the writer
299 * waiting forever while new readers are always accessing data (no
301 * Ensured by STORE_SHARED and LOAD_SHARED.
305 * Adding a smp_mb() which is _not_ formally required, but makes the
306 * model easier to understand. It does not have a big performance impact
307 * anyway, given this is the write-side.
312 * Wait for previous parity to be empty of readers.
314 wait_for_quiescent_state(); /* Wait readers in parity 0 */
317 * Must finish waiting for quiescent state for parity 0 before
318 * committing qparity update to memory. Failure to do so could result in
319 * the writer waiting forever while new readers are always accessing
320 * data (no progress).
321 * Ensured by STORE_SHARED and LOAD_SHARED.
325 * Adding a smp_mb() which is _not_ formally required, but makes the
326 * model easier to understand. It does not have a big performance impact
327 * anyway, given this is the write-side.
331 switch_next_urcu_qparity(); /* 1 -> 0 */
334 * Must commit qparity update to memory before waiting for parity
335 * 1 quiescent state. Failure to do so could result in the writer
336 * waiting forever while new readers are always accessing data (no
338 * Ensured by STORE_SHARED and LOAD_SHARED.
342 * Adding a smp_mb() which is _not_ formally required, but makes the
343 * model easier to understand. It does not have a big performance impact
344 * anyway, given this is the write-side.
349 * Wait for previous parity to be empty of readers.
351 wait_for_quiescent_state(); /* Wait readers in parity 1 */
353 /* Finish waiting for reader threads before letting the old ptr being
354 * freed. Must be done within internal_urcu_lock because it iterates on
356 force_mb_all_threads();
358 internal_urcu_unlock();
362 * library wrappers to be used by non-LGPL compatible source code.
365 void rcu_read_lock(void)
370 void rcu_read_unlock(void)
375 void rcu_register_thread(void)
377 urcu_reader
.tid
= pthread_self();
378 assert(urcu_reader
.need_mb
== 0);
379 assert(urcu_reader
.ctr
== 0);
381 internal_urcu_lock();
382 urcu_init(); /* In case gcc does not support constructor attribute */
383 list_add(&urcu_reader
.head
, ®istry
);
384 internal_urcu_unlock();
387 void rcu_unregister_thread(void)
389 internal_urcu_lock();
390 list_del(&urcu_reader
.head
);
391 internal_urcu_unlock();
395 static void sigurcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
398 * Executing this smp_mb() is the only purpose of this signal handler.
399 * It punctually promotes barrier() into smp_mb() on every thread it is
403 urcu_reader
.need_mb
= 0;
408 * urcu_init constructor. Called when the library is linked, but also when
409 * reader threads are calling rcu_register_thread().
410 * Should only be called by a single thread at a given time. This is ensured by
411 * holing the internal_urcu_lock() from rcu_register_thread() or by running at
412 * library load time, which should not be executed by multiple threads nor
413 * concurrently with rcu_register_thread() anyway.
417 struct sigaction act
;
424 act
.sa_sigaction
= sigurcu_handler
;
425 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
426 sigemptyset(&act
.sa_mask
);
427 ret
= sigaction(SIGURCU
, &act
, NULL
);
429 perror("Error in sigaction");
436 struct sigaction act
;
439 ret
= sigaction(SIGURCU
, NULL
, &act
);
441 perror("Error in sigaction");
444 assert(act
.sa_sigaction
== sigurcu_handler
);
445 assert(list_empty(®istry
));
447 #endif /* #ifndef URCU_MB */