urcu: declare test_urcu_multiflavor functions
[urcu.git] / urcu.c
CommitLineData
b257a10b
MD
1/*
2 * urcu.c
3 *
4 * Userspace RCU library
5 *
6982d6d7 6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
af02d47e 7 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
b257a10b 8 *
af02d47e
MD
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.
13 *
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.
18 *
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
54843abc
PM
22 *
23 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
b257a10b
MD
24 */
25
fdf01eed 26#define _BSD_SOURCE
c1d2c60b 27#define _GNU_SOURCE
71c811bf 28#define _LGPL_SOURCE
27b012e2
MD
29#include <stdio.h>
30#include <pthread.h>
31#include <signal.h>
32#include <assert.h>
f69f195a 33#include <stdlib.h>
6d841bc2 34#include <stdint.h>
f69f195a 35#include <string.h>
09a9f986 36#include <errno.h>
e8043c1b 37#include <poll.h>
27b012e2 38
d73fb81f 39#include "urcu/wfcqueue.h"
57760d44 40#include "urcu/map/urcu.h"
af7c2dbe 41#include "urcu/static/urcu.h"
618b2595 42#include "urcu-pointer.h"
bd252a04 43#include "urcu/tls-compat.h"
71c811bf 44
4a6d7378 45#include "urcu-die.h"
5bffdd5d 46#include "urcu-wait.h"
4a6d7378 47
121a5d44 48/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
71c811bf 49#undef _LGPL_SOURCE
27b012e2 50#include "urcu.h"
71c811bf 51#define _LGPL_SOURCE
27b012e2 52
3a71751e
PB
53/*
54 * If a reader is really non-cooperative and refuses to commit its
55 * rcu_active_readers count to memory (there is no barrier in the reader
56 * per-se), kick it after a few loops waiting for it.
57 */
58#define KICK_READER_LOOPS 10000
59
60/*
61 * Active attempts to check for reader Q.S. before calling futex().
62 */
63#define RCU_QS_ACTIVE_ATTEMPTS 100
64
fdf01eed 65#ifdef RCU_MEMBARRIER
834a45ba 66static int init_done;
1de4df4b 67int rcu_has_sys_membarrier;
834a45ba 68
02be5561 69void __attribute__((constructor)) rcu_init(void);
fdf01eed
MD
70#endif
71
72#ifdef RCU_MB
02be5561 73void rcu_init(void)
e90a6e9c
MD
74{
75}
76#endif
8a5fb4c9 77
fdf01eed
MD
78#ifdef RCU_SIGNAL
79static int init_done;
80
81void __attribute__((constructor)) rcu_init(void);
82void __attribute__((destructor)) rcu_exit(void);
83#endif
84
6abb4bd5 85static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
27b012e2 86
1de4df4b 87int32_t rcu_gp_futex;
bc6c15bb 88
128166c9
MD
89/*
90 * Global grace period counter.
02be5561 91 * Contains the current RCU_GP_CTR_PHASE.
afb8f2c9 92 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
b0d5e790 93 * Written to only by writer with mutex taken. Read by both writer and readers.
128166c9 94 */
27d65bc5 95unsigned long rcu_gp_ctr = RCU_GP_COUNT;
27b012e2 96
b0d5e790
MD
97/*
98 * Written to only by each individual reader. Read by both the reader and the
99 * writers.
100 */
bd252a04 101DEFINE_URCU_TLS(struct rcu_reader, rcu_reader);
27b012e2 102
cf380c2f 103#ifdef DEBUG_YIELD
1de4df4b
MD
104unsigned int rcu_yield_active;
105DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
cf380c2f
MD
106#endif
107
16aa9ee8 108static CDS_LIST_HEAD(registry);
27b012e2 109
5bffdd5d
MD
110/*
111 * Queue keeping threads awaiting to wait for a grace period. Contains
112 * struct gp_waiters_thread objects.
113 */
114static DEFINE_URCU_WAIT_QUEUE(gp_waiters);
115
6abb4bd5 116static void mutex_lock(pthread_mutex_t *mutex)
41718ff9
MD
117{
118 int ret;
09a9f986
PM
119
120#ifndef DISTRUST_SIGNALS_EXTREME
6abb4bd5 121 ret = pthread_mutex_lock(mutex);
4a6d7378
MD
122 if (ret)
123 urcu_die(ret);
09a9f986 124#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
6abb4bd5 125 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
4a6d7378
MD
126 if (ret != EBUSY && ret != EINTR)
127 urcu_die(ret);
bd252a04 128 if (CMM_LOAD_SHARED(URCU_TLS(rcu_reader).need_mb)) {
5481ddb3 129 cmm_smp_mb();
bd252a04 130 _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
5481ddb3 131 cmm_smp_mb();
09a9f986
PM
132 }
133 poll(NULL,0,10);
134 }
135#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
41718ff9
MD
136}
137
6abb4bd5 138static void mutex_unlock(pthread_mutex_t *mutex)
41718ff9
MD
139{
140 int ret;
141
6abb4bd5 142 ret = pthread_mutex_unlock(mutex);
4a6d7378
MD
143 if (ret)
144 urcu_die(ret);
41718ff9
MD
145}
146
fdf01eed 147#ifdef RCU_MEMBARRIER
25cc6d18 148static void smp_mb_master(int group)
fdf01eed 149{
1de4df4b 150 if (caa_likely(rcu_has_sys_membarrier))
f0708810 151 membarrier(MEMBARRIER_EXPEDITED);
fdf01eed 152 else
5481ddb3 153 cmm_smp_mb();
fdf01eed
MD
154}
155#endif
156
02be5561 157#ifdef RCU_MB
25cc6d18 158static void smp_mb_master(int group)
40e140c9 159{
5481ddb3 160 cmm_smp_mb();
40e140c9 161}
fdf01eed
MD
162#endif
163
164#ifdef RCU_SIGNAL
78ff9419 165static void force_mb_all_readers(void)
27b012e2 166{
02be5561 167 struct rcu_reader *index;
e3b0cef0 168
27b012e2 169 /*
5481ddb3 170 * Ask for each threads to execute a cmm_smp_mb() so we can consider the
27b012e2
MD
171 * compiler barriers around rcu read lock as real memory barriers.
172 */
16aa9ee8 173 if (cds_list_empty(&registry))
27b012e2 174 return;
3a86deba 175 /*
5481ddb3 176 * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs
157dca95 177 * a cache flush on architectures with non-coherent cache. Let's play
5481ddb3 178 * safe and don't assume anything : we use cmm_smp_mc() to make sure the
157dca95 179 * cache flush is enforced.
3a86deba 180 */
16aa9ee8 181 cds_list_for_each_entry(index, &registry, node) {
6cf3827c 182 CMM_STORE_SHARED(index->need_mb, 1);
02be5561 183 pthread_kill(index->tid, SIGRCU);
09a9f986 184 }
27b012e2
MD
185 /*
186 * Wait for sighandler (and thus mb()) to execute on every thread.
09a9f986
PM
187 *
188 * Note that the pthread_kill() will never be executed on systems
189 * that correctly deliver signals in a timely manner. However, it
190 * is not uncommon for kernels to have bugs that can result in
191 * lost or unduly delayed signals.
192 *
193 * If you are seeing the below pthread_kill() executing much at
194 * all, we suggest testing the underlying kernel and filing the
195 * relevant bug report. For Linux kernels, we recommend getting
196 * the Linux Test Project (LTP).
27b012e2 197 */
16aa9ee8 198 cds_list_for_each_entry(index, &registry, node) {
6cf3827c 199 while (CMM_LOAD_SHARED(index->need_mb)) {
02be5561 200 pthread_kill(index->tid, SIGRCU);
09a9f986
PM
201 poll(NULL, 0, 1);
202 }
203 }
5481ddb3 204 cmm_smp_mb(); /* read ->need_mb before ending the barrier */
27b012e2 205}
9d7e3f89 206
25cc6d18 207static void smp_mb_master(int group)
9d7e3f89
MD
208{
209 force_mb_all_readers();
210}
fdf01eed 211#endif /* #ifdef RCU_SIGNAL */
27b012e2 212
bc6c15bb
MD
213/*
214 * synchronize_rcu() waiting. Single thread.
215 */
cfe78e25 216static void wait_gp(void)
bc6c15bb 217{
cfe78e25 218 /* Read reader_gp before read futex */
25cc6d18 219 smp_mb_master(RCU_MB_GROUP);
1de4df4b
MD
220 if (uatomic_read(&rcu_gp_futex) == -1)
221 futex_async(&rcu_gp_futex, FUTEX_WAIT, -1,
cfe78e25 222 NULL, NULL, 0);
bc6c15bb
MD
223}
224
fd189fa5
MD
225static void wait_for_readers(struct cds_list_head *input_readers,
226 struct cds_list_head *cur_snap_readers,
227 struct cds_list_head *qsreaders)
27b012e2 228{
cfe78e25 229 int wait_loops = 0;
02be5561 230 struct rcu_reader *index, *tmp;
27b012e2 231
40e140c9 232 /*
c9488684
MD
233 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
234 * indicate quiescence (not nested), or observe the current
235 * rcu_gp_ctr value.
27b012e2 236 */
cfe78e25
MD
237 for (;;) {
238 wait_loops++;
239 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
1de4df4b 240 uatomic_dec(&rcu_gp_futex);
cfe78e25 241 /* Write futex before read reader_gp */
25cc6d18 242 smp_mb_master(RCU_MB_GROUP);
cfe78e25
MD
243 }
244
fd189fa5
MD
245 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
246 switch (rcu_reader_state(&index->ctr)) {
247 case RCU_READER_ACTIVE_CURRENT:
248 if (cur_snap_readers) {
249 cds_list_move(&index->node,
250 cur_snap_readers);
251 break;
252 }
253 /* Fall-through */
254 case RCU_READER_INACTIVE:
255 cds_list_move(&index->node, qsreaders);
256 break;
257 case RCU_READER_ACTIVE_OLD:
258 /*
259 * Old snapshot. Leaving node in
260 * input_readers will make us busy-loop
261 * until the snapshot becomes current or
262 * the reader becomes inactive.
263 */
264 break;
265 }
cfe78e25
MD
266 }
267
e8043c1b 268#ifndef HAS_INCOHERENT_CACHES
fd189fa5 269 if (cds_list_empty(input_readers)) {
cfe78e25
MD
270 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
271 /* Read reader_gp before write futex */
25cc6d18 272 smp_mb_master(RCU_MB_GROUP);
1de4df4b 273 uatomic_set(&rcu_gp_futex, 0);
bc6c15bb 274 }
cfe78e25
MD
275 break;
276 } else {
277 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS)
278 wait_gp();
279 else
06f22bdb 280 caa_cpu_relax();
bc6c15bb 281 }
e8043c1b 282#else /* #ifndef HAS_INCOHERENT_CACHES */
27b012e2 283 /*
40e140c9 284 * BUSY-LOOP. Force the reader thread to commit its
bd252a04
MD
285 * URCU_TLS(rcu_reader).ctr update to memory if we wait
286 * for too long.
27b012e2 287 */
fd189fa5 288 if (cds_list_empty(input_readers)) {
cfe78e25
MD
289 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
290 /* Read reader_gp before write futex */
25cc6d18 291 smp_mb_master(RCU_MB_GROUP);
1de4df4b 292 uatomic_set(&rcu_gp_futex, 0);
cfe78e25
MD
293 }
294 break;
295 } else {
296 switch (wait_loops) {
bc6c15bb 297 case RCU_QS_ACTIVE_ATTEMPTS:
cfe78e25
MD
298 wait_gp();
299 break; /* only escape switch */
bc6c15bb 300 case KICK_READER_LOOPS:
25cc6d18 301 smp_mb_master(RCU_MB_GROUP);
40e140c9 302 wait_loops = 0;
cfe78e25 303 break; /* only escape switch */
bc6c15bb 304 default:
06f22bdb 305 caa_cpu_relax();
40e140c9
MD
306 }
307 }
e8043c1b 308#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
27b012e2 309 }
27b012e2
MD
310}
311
9598a481 312void synchronize_rcu(void)
2bc59bd7 313{
fd189fa5
MD
314 CDS_LIST_HEAD(cur_snap_readers);
315 CDS_LIST_HEAD(qsreaders);
5bffdd5d
MD
316 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
317 struct urcu_waiters waiters;
318
319 /*
320 * Add ourself to gp_waiters queue of threads awaiting to wait
321 * for a grace period. Proceed to perform the grace period only
322 * if we are the first thread added into the queue.
323 * The implicit memory barrier before urcu_wait_add()
324 * orders prior memory accesses of threads put into the wait
325 * queue before their insertion into the wait queue.
326 */
327 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
328 /* Not first in queue: will be awakened by another thread. */
329 urcu_adaptative_busy_wait(&wait);
330 /* Order following memory accesses after grace period. */
331 cmm_smp_mb();
332 return;
333 }
334 /* We won't need to wake ourself up */
335 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
fd189fa5 336
6abb4bd5 337 mutex_lock(&rcu_gp_lock);
135530fd 338
5bffdd5d
MD
339 /*
340 * Move all waiters into our local queue.
341 */
342 urcu_move_waiters(&waiters, &gp_waiters);
343
16aa9ee8 344 if (cds_list_empty(&registry))
2dfb8b5e
MD
345 goto out;
346
9598a481 347 /* All threads should read qparity before accessing data structure
6abb4bd5
MD
348 * where new ptr points to. Must be done within rcu_gp_lock because it
349 * iterates on reader threads.*/
9598a481 350 /* Write new ptr before changing the qparity */
25cc6d18 351 smp_mb_master(RCU_MB_GROUP);
9598a481 352
9598a481 353 /*
c9488684 354 * Wait for readers to observe original parity or be quiescent.
9598a481 355 */
fd189fa5 356 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
9598a481
MD
357
358 /*
c9488684 359 * Must finish waiting for quiescent state for original parity before
d40fde2c
MD
360 * committing next rcu_gp_ctr update to memory. Failure to do so could
361 * result in the writer waiting forever while new readers are always
362 * accessing data (no progress). Enforce compiler-order of load
bd252a04 363 * URCU_TLS(rcu_reader).ctr before store to rcu_gp_ctr.
9598a481 364 */
5481ddb3 365 cmm_barrier();
9598a481 366
5dba80f9 367 /*
5481ddb3 368 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
5dba80f9
MD
369 * model easier to understand. It does not have a big performance impact
370 * anyway, given this is the write-side.
371 */
5481ddb3 372 cmm_smp_mb();
67c2d80b 373
c9488684
MD
374 /* Switch parity: 0 -> 1, 1 -> 0 */
375 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE);
376
377 /*
378 * Must commit rcu_gp_ctr update to memory before waiting for quiescent
379 * state. Failure to do so could result in the writer waiting forever
380 * while new readers are always accessing data (no progress). Enforce
381 * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr.
382 */
383 cmm_barrier();
384
385 /*
386 *
387 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
388 * model easier to understand. It does not have a big performance impact
389 * anyway, given this is the write-side.
390 */
391 cmm_smp_mb();
392
9598a481 393 /*
c9488684 394 * Wait for readers to observe new parity or be quiescent.
9598a481 395 */
fd189fa5
MD
396 wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
397
398 /*
399 * Put quiescent reader list back into registry.
400 */
401 cds_list_splice(&qsreaders, &registry);
9598a481 402
9598a481 403 /* Finish waiting for reader threads before letting the old ptr being
6abb4bd5
MD
404 * freed. Must be done within rcu_gp_lock because it iterates on reader
405 * threads. */
25cc6d18 406 smp_mb_master(RCU_MB_GROUP);
2dfb8b5e 407out:
6abb4bd5 408 mutex_unlock(&rcu_gp_lock);
5bffdd5d
MD
409
410 /*
411 * Wakeup waiters only after we have completed the grace period
412 * and have ensured the memory barriers at the end of the grace
413 * period have been issued.
414 */
415 urcu_wake_all_waiters(&waiters);
2bc59bd7
PM
416}
417
121a5d44
MD
418/*
419 * library wrappers to be used by non-LGPL compatible source code.
420 */
421
422void rcu_read_lock(void)
423{
424 _rcu_read_lock();
425}
426
427void rcu_read_unlock(void)
428{
429 _rcu_read_unlock();
430}
431
121a5d44 432void rcu_register_thread(void)
27b012e2 433{
bd252a04
MD
434 URCU_TLS(rcu_reader).tid = pthread_self();
435 assert(URCU_TLS(rcu_reader).need_mb == 0);
436 assert(!(URCU_TLS(rcu_reader).ctr & RCU_GP_CTR_NEST_MASK));
02be5561 437
6abb4bd5 438 mutex_lock(&rcu_gp_lock);
02be5561 439 rcu_init(); /* In case gcc does not support constructor attribute */
bd252a04 440 cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
6abb4bd5 441 mutex_unlock(&rcu_gp_lock);
27b012e2
MD
442}
443
121a5d44 444void rcu_unregister_thread(void)
27b012e2 445{
6abb4bd5 446 mutex_lock(&rcu_gp_lock);
bd252a04 447 cds_list_del(&URCU_TLS(rcu_reader).node);
6abb4bd5 448 mutex_unlock(&rcu_gp_lock);
27b012e2
MD
449}
450
fdf01eed
MD
451#ifdef RCU_MEMBARRIER
452void rcu_init(void)
453{
454 if (init_done)
455 return;
456 init_done = 1;
cf5271ee 457 if (!membarrier(MEMBARRIER_EXPEDITED | MEMBARRIER_QUERY))
1de4df4b 458 rcu_has_sys_membarrier = 1;
fdf01eed
MD
459}
460#endif
461
462#ifdef RCU_SIGNAL
02be5561 463static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context)
27b012e2 464{
40e140c9 465 /*
5481ddb3
DG
466 * Executing this cmm_smp_mb() is the only purpose of this signal handler.
467 * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
40e140c9
MD
468 * executed on.
469 */
5481ddb3 470 cmm_smp_mb();
bd252a04 471 _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
5481ddb3 472 cmm_smp_mb();
27b012e2
MD
473}
474
8a5fb4c9 475/*
02be5561 476 * rcu_init constructor. Called when the library is linked, but also when
8a5fb4c9
MD
477 * reader threads are calling rcu_register_thread().
478 * Should only be called by a single thread at a given time. This is ensured by
6abb4bd5
MD
479 * holing the rcu_gp_lock from rcu_register_thread() or by running at library
480 * load time, which should not be executed by multiple threads nor concurrently
481 * with rcu_register_thread() anyway.
8a5fb4c9 482 */
02be5561 483void rcu_init(void)
27b012e2
MD
484{
485 struct sigaction act;
486 int ret;
487
8a5fb4c9
MD
488 if (init_done)
489 return;
490 init_done = 1;
491
02be5561 492 act.sa_sigaction = sigrcu_handler;
dd052bd3 493 act.sa_flags = SA_SIGINFO | SA_RESTART;
c297c21c 494 sigemptyset(&act.sa_mask);
02be5561 495 ret = sigaction(SIGRCU, &act, NULL);
4a6d7378
MD
496 if (ret)
497 urcu_die(errno);
27b012e2
MD
498}
499
02be5561 500void rcu_exit(void)
27b012e2
MD
501{
502 struct sigaction act;
503 int ret;
504
02be5561 505 ret = sigaction(SIGRCU, NULL, &act);
4a6d7378
MD
506 if (ret)
507 urcu_die(errno);
02be5561 508 assert(act.sa_sigaction == sigrcu_handler);
16aa9ee8 509 assert(cds_list_empty(&registry));
27b012e2 510}
5e77fc1f 511
fdf01eed 512#endif /* #ifdef RCU_SIGNAL */
5e77fc1f 513
5e6b23a6 514DEFINE_RCU_FLAVOR(rcu_flavor);
541d828d 515
5e77fc1f 516#include "urcu-call-rcu-impl.h"
0376e7b2 517#include "urcu-defer-impl.h"
This page took 0.063706 seconds and 4 git commands to generate.