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
;
53 * Global grace period counter.
54 * Contains the current RCU_GP_CTR_BIT.
55 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
56 * Written to only by writer with mutex taken. Read by both writer and readers.
58 long urcu_gp_ctr
= RCU_GP_COUNT
;
61 * Written to only by each individual reader. Read by both the reader and the
64 long __thread urcu_active_readers
;
66 /* Thread IDs of registered readers */
67 #define INIT_NUM_THREADS 4
69 struct reader_registry
{
71 long *urcu_active_readers
;
76 unsigned int yield_active
;
77 unsigned int __thread rand_yield
;
80 static struct reader_registry
*registry
;
81 static char __thread need_mb
;
82 static int num_readers
, alloc_readers
;
84 static void internal_urcu_lock(void)
88 #ifndef DISTRUST_SIGNALS_EXTREME
89 ret
= pthread_mutex_lock(&urcu_mutex
);
91 perror("Error in pthread mutex lock");
94 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
95 while ((ret
= pthread_mutex_trylock(&urcu_mutex
)) != 0) {
96 if (ret
!= EBUSY
&& ret
!= EINTR
) {
97 printf("ret = %d, errno = %d\n", ret
, errno
);
98 perror("Error in pthread mutex lock");
108 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
111 static void internal_urcu_unlock(void)
115 ret
= pthread_mutex_unlock(&urcu_mutex
);
117 perror("Error in pthread mutex unlock");
123 * called with urcu_mutex held.
125 static void switch_next_urcu_qparity(void)
127 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_CTR_BIT
);
131 #ifdef HAS_INCOHERENT_CACHES
132 static void force_mb_single_thread(struct reader_registry
*index
)
136 #endif /* #ifdef HAS_INCOHERENT_CACHES */
138 static void force_mb_all_threads(void)
142 #else /* #ifdef URCU_MB */
143 #ifdef HAS_INCOHERENT_CACHES
144 static void force_mb_single_thread(struct reader_registry
*index
)
148 * pthread_kill has a smp_mb(). But beware, we assume it performs
149 * a cache flush on architectures with non-coherent cache. Let's play
150 * safe and don't assume anything : we use smp_mc() to make sure the
151 * cache flush is enforced.
154 smp_mc(); /* write ->need_mb before sending the signals */
155 pthread_kill(index
->tid
, SIGURCU
);
158 * Wait for sighandler (and thus mb()) to execute on every thread.
161 while (*index
->need_mb
) {
164 smp_mb(); /* read ->need_mb before ending the barrier */
166 #endif /* #ifdef HAS_INCOHERENT_CACHES */
168 static void force_mb_all_threads(void)
170 struct reader_registry
*index
;
172 * Ask for each threads to execute a smp_mb() so we can consider the
173 * compiler barriers around rcu read lock as real memory barriers.
178 * pthread_kill has a smp_mb(). But beware, we assume it performs
179 * a cache flush on architectures with non-coherent cache. Let's play
180 * safe and don't assume anything : we use smp_mc() to make sure the
181 * cache flush is enforced.
183 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
185 smp_mc(); /* write need_mb before sending the signal */
186 pthread_kill(index
->tid
, SIGURCU
);
189 * Wait for sighandler (and thus mb()) to execute on every thread.
191 * Note that the pthread_kill() will never be executed on systems
192 * that correctly deliver signals in a timely manner. However, it
193 * is not uncommon for kernels to have bugs that can result in
194 * lost or unduly delayed signals.
196 * If you are seeing the below pthread_kill() executing much at
197 * all, we suggest testing the underlying kernel and filing the
198 * relevant bug report. For Linux kernels, we recommend getting
199 * the Linux Test Project (LTP).
201 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
202 while (*index
->need_mb
) {
203 pthread_kill(index
->tid
, SIGURCU
);
207 smp_mb(); /* read ->need_mb before ending the barrier */
209 #endif /* #else #ifdef URCU_MB */
211 void wait_for_quiescent_state(void)
213 struct reader_registry
*index
;
218 * Wait for each thread urcu_active_readers count to become 0.
220 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
221 #ifndef HAS_INCOHERENT_CACHES
222 while (rcu_old_gp_ongoing(index
->urcu_active_readers
))
224 #else /* #ifndef HAS_INCOHERENT_CACHES */
227 * BUSY-LOOP. Force the reader thread to commit its
228 * urcu_active_readers update to memory if we wait for too long.
230 while (rcu_old_gp_ongoing(index
->urcu_active_readers
)) {
231 if (wait_loops
++ == KICK_READER_LOOPS
) {
232 force_mb_single_thread(index
);
238 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
242 void synchronize_rcu(void)
244 internal_urcu_lock();
246 /* All threads should read qparity before accessing data structure
247 * where new ptr points to. Must be done within internal_urcu_lock
248 * because it iterates on reader threads.*/
249 /* Write new ptr before changing the qparity */
250 force_mb_all_threads();
252 switch_next_urcu_qparity(); /* 0 -> 1 */
255 * Must commit qparity update to memory before waiting for parity
256 * 0 quiescent state. Failure to do so could result in the writer
257 * waiting forever while new readers are always accessing data (no
259 * Ensured by STORE_SHARED and LOAD_SHARED.
263 * Adding a smp_mb() which is _not_ formally required, but makes the
264 * model easier to understand. It does not have a big performance impact
265 * anyway, given this is the write-side.
270 * Wait for previous parity to be empty of readers.
272 wait_for_quiescent_state(); /* Wait readers in parity 0 */
275 * Must finish waiting for quiescent state for parity 0 before
276 * committing qparity update to memory. Failure to do so could result in
277 * the writer waiting forever while new readers are always accessing
278 * data (no progress).
279 * Ensured by STORE_SHARED and LOAD_SHARED.
283 * Adding a smp_mb() which is _not_ formally required, but makes the
284 * model easier to understand. It does not have a big performance impact
285 * anyway, given this is the write-side.
289 switch_next_urcu_qparity(); /* 1 -> 0 */
292 * Must commit qparity update to memory before waiting for parity
293 * 1 quiescent state. Failure to do so could result in the writer
294 * waiting forever while new readers are always accessing data (no
296 * Ensured by STORE_SHARED and LOAD_SHARED.
300 * Adding a smp_mb() which is _not_ formally required, but makes the
301 * model easier to understand. It does not have a big performance impact
302 * anyway, given this is the write-side.
307 * Wait for previous parity to be empty of readers.
309 wait_for_quiescent_state(); /* Wait readers in parity 1 */
311 /* Finish waiting for reader threads before letting the old ptr being
312 * freed. Must be done within internal_urcu_lock because it iterates on
314 force_mb_all_threads();
316 internal_urcu_unlock();
320 * library wrappers to be used by non-LGPL compatible source code.
323 void rcu_read_lock(void)
328 void rcu_read_unlock(void)
333 void *rcu_dereference(void *p
)
335 return _rcu_dereference(p
);
338 void *rcu_assign_pointer_sym(void **p
, void *v
)
341 return STORE_SHARED(p
, v
);
344 void *rcu_xchg_pointer_sym(void **p
, void *v
)
350 void *rcu_cmpxchg_pointer_sym(void **p
, void *old
, void *_new
)
353 return cmpxchg(p
, old
, _new
);
356 void *rcu_publish_content_sym(void **p
, void *v
)
360 oldptr
= _rcu_xchg_pointer(p
, v
);
365 static void rcu_add_reader(pthread_t id
)
367 struct reader_registry
*oldarray
;
370 alloc_readers
= INIT_NUM_THREADS
;
373 malloc(sizeof(struct reader_registry
) * alloc_readers
);
375 if (alloc_readers
< num_readers
+ 1) {
377 registry
= malloc(sizeof(struct reader_registry
)
378 * (alloc_readers
<< 1));
379 memcpy(registry
, oldarray
,
380 sizeof(struct reader_registry
) * alloc_readers
);
384 registry
[num_readers
].tid
= id
;
385 /* reference to the TLS of _this_ reader thread. */
386 registry
[num_readers
].urcu_active_readers
= &urcu_active_readers
;
387 registry
[num_readers
].need_mb
= &need_mb
;
392 * Never shrink (implementation limitation).
393 * This is O(nb threads). Eventually use a hash table.
395 static void rcu_remove_reader(pthread_t id
)
397 struct reader_registry
*index
;
399 assert(registry
!= NULL
);
400 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
401 if (pthread_equal(index
->tid
, id
)) {
402 memcpy(index
, ®istry
[num_readers
- 1],
403 sizeof(struct reader_registry
));
404 registry
[num_readers
- 1].tid
= 0;
405 registry
[num_readers
- 1].urcu_active_readers
= NULL
;
410 /* Hrm not found, forgot to register ? */
414 void rcu_register_thread(void)
416 internal_urcu_lock();
417 urcu_init(); /* In case gcc does not support constructor attribute */
418 rcu_add_reader(pthread_self());
419 internal_urcu_unlock();
422 void rcu_unregister_thread(void)
424 internal_urcu_lock();
425 rcu_remove_reader(pthread_self());
426 internal_urcu_unlock();
430 static void sigurcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
433 * Executing this smp_mb() is the only purpose of this signal handler.
434 * It punctually promotes barrier() into smp_mb() on every thread it is
443 * urcu_init constructor. Called when the library is linked, but also when
444 * reader threads are calling rcu_register_thread().
445 * Should only be called by a single thread at a given time. This is ensured by
446 * holing the internal_urcu_lock() from rcu_register_thread() or by running at
447 * library load time, which should not be executed by multiple threads nor
448 * concurrently with rcu_register_thread() anyway.
452 struct sigaction act
;
459 act
.sa_sigaction
= sigurcu_handler
;
460 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
461 sigemptyset(&act
.sa_mask
);
462 ret
= sigaction(SIGURCU
, &act
, NULL
);
464 perror("Error in sigaction");
471 struct sigaction act
;
474 ret
= sigaction(SIGURCU
, NULL
, &act
);
476 perror("Error in sigaction");
479 assert(act
.sa_sigaction
== sigurcu_handler
);
482 #endif /* #ifndef URCU_MB */