uatomic/generic: Fix redundant declaration warning
[urcu.git] / src / urcu.c
CommitLineData
acdb82a2
MJ
1// SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2// SPDX-FileCopyrightText: 2009 Paul E. McKenney, IBM Corporation.
3//
4// SPDX-License-Identifier: LGPL-2.1-or-later
5
b257a10b 6/*
b257a10b
MD
7 * Userspace RCU library
8 *
54843abc 9 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
b257a10b
MD
10 */
11
e37faee1 12#define URCU_NO_COMPAT_IDENTIFIERS
fdf01eed 13#define _BSD_SOURCE
71c811bf 14#define _LGPL_SOURCE
82d50e1a 15#define _DEFAULT_SOURCE
27b012e2
MD
16#include <stdio.h>
17#include <pthread.h>
18#include <signal.h>
f69f195a 19#include <stdlib.h>
6d841bc2 20#include <stdint.h>
f69f195a 21#include <string.h>
09a9f986 22#include <errno.h>
c0bb9f69 23#include <stdbool.h>
e8043c1b 24#include <poll.h>
27b012e2 25
375db287 26#include <urcu/config.h>
601922a8 27#include <urcu/annotate.h>
01477510 28#include <urcu/assert.h>
4477a870
MD
29#include <urcu/arch.h>
30#include <urcu/wfcqueue.h>
31#include <urcu/map/urcu.h>
32#include <urcu/static/urcu.h>
33#include <urcu/pointer.h>
34#include <urcu/tls-compat.h>
71c811bf 35
4a6d7378 36#include "urcu-die.h"
5bffdd5d 37#include "urcu-wait.h"
4477a870 38#include "urcu-utils.h"
4a6d7378 39
4477a870 40#define URCU_API_MAP
121a5d44 41/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
71c811bf 42#undef _LGPL_SOURCE
4477a870 43#include <urcu/urcu.h>
71c811bf 44#define _LGPL_SOURCE
27b012e2 45
3a71751e
PB
46/*
47 * If a reader is really non-cooperative and refuses to commit its
48 * rcu_active_readers count to memory (there is no barrier in the reader
9340c38d 49 * per-se), kick it after 10 loops waiting for it.
3a71751e 50 */
9340c38d 51#define KICK_READER_LOOPS 10
3a71751e
PB
52
53/*
54 * Active attempts to check for reader Q.S. before calling futex().
55 */
56#define RCU_QS_ACTIVE_ATTEMPTS 100
57
999991c6
MD
58/* If the headers do not support membarrier system call, fall back on RCU_MB */
59#ifdef __NR_membarrier
60# define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
553b7eb9
MD
61#else
62# define membarrier(...) -ENOSYS
63#endif
64
64f469e6 65enum membarrier_cmd {
c0bb9f69
MD
66 MEMBARRIER_CMD_QUERY = 0,
67 MEMBARRIER_CMD_SHARED = (1 << 0),
68 /* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */
69 /* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */
70 MEMBARRIER_CMD_PRIVATE_EXPEDITED = (1 << 3),
71 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED = (1 << 4),
64f469e6 72};
553b7eb9 73
fdf01eed 74#ifdef RCU_MEMBARRIER
834a45ba 75static int init_done;
4477a870 76static int urcu_memb_has_sys_membarrier_private_expedited;
c0bb9f69 77
d8d9a340 78#ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER
4477a870
MD
79/*
80 * Explicitly initialize to zero because we can't alias a non-static
81 * uninitialized variable.
82 */
83int urcu_memb_has_sys_membarrier = 0;
d8d9a340 84#endif
834a45ba 85
02be5561 86void __attribute__((constructor)) rcu_init(void);
fdf01eed
MD
87#endif
88
89#ifdef RCU_MB
02be5561 90void rcu_init(void)
e90a6e9c
MD
91{
92}
93#endif
8a5fb4c9 94
fdf01eed
MD
95#ifdef RCU_SIGNAL
96static int init_done;
97
98void __attribute__((constructor)) rcu_init(void);
ea3a28a3
MD
99
100static DEFINE_URCU_TLS(int, rcu_signal_was_blocked);
fdf01eed
MD
101#endif
102
90f72b8c
MD
103void __attribute__((destructor)) rcu_exit(void);
104static void urcu_call_rcu_exit(void);
105
731ccb96
MD
106/*
107 * rcu_gp_lock ensures mutual exclusion between threads calling
108 * synchronize_rcu().
109 */
6abb4bd5 110static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
731ccb96
MD
111/*
112 * rcu_registry_lock ensures mutual exclusion between threads
113 * registering and unregistering themselves to/from the registry, and
114 * with threads reading that registry from synchronize_rcu(). However,
115 * this lock is not held all the way through the completion of awaiting
116 * for the grace period. It is sporadically released between iterations
117 * on the registry.
118 * rcu_registry_lock may nest inside rcu_gp_lock.
119 */
120static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER;
4477a870 121struct urcu_gp rcu_gp = { .ctr = URCU_GP_COUNT };
27b012e2 122
b0d5e790
MD
123/*
124 * Written to only by each individual reader. Read by both the reader and the
125 * writers.
126 */
4477a870 127DEFINE_URCU_TLS(struct urcu_reader, rcu_reader);
27b012e2 128
16aa9ee8 129static CDS_LIST_HEAD(registry);
27b012e2 130
5bffdd5d
MD
131/*
132 * Queue keeping threads awaiting to wait for a grace period. Contains
133 * struct gp_waiters_thread objects.
134 */
135static DEFINE_URCU_WAIT_QUEUE(gp_waiters);
136
6abb4bd5 137static void mutex_lock(pthread_mutex_t *mutex)
41718ff9
MD
138{
139 int ret;
09a9f986
PM
140
141#ifndef DISTRUST_SIGNALS_EXTREME
6abb4bd5 142 ret = pthread_mutex_lock(mutex);
4a6d7378
MD
143 if (ret)
144 urcu_die(ret);
09a9f986 145#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
6abb4bd5 146 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
4a6d7378
MD
147 if (ret != EBUSY && ret != EINTR)
148 urcu_die(ret);
bd252a04 149 if (CMM_LOAD_SHARED(URCU_TLS(rcu_reader).need_mb)) {
5481ddb3 150 cmm_smp_mb();
bd252a04 151 _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
5481ddb3 152 cmm_smp_mb();
09a9f986 153 }
8999a9ee 154 (void) poll(NULL, 0, 10);
09a9f986
PM
155 }
156#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
41718ff9
MD
157}
158
6abb4bd5 159static void mutex_unlock(pthread_mutex_t *mutex)
41718ff9
MD
160{
161 int ret;
162
6abb4bd5 163 ret = pthread_mutex_unlock(mutex);
4a6d7378
MD
164 if (ret)
165 urcu_die(ret);
41718ff9
MD
166}
167
fdf01eed 168#ifdef RCU_MEMBARRIER
a4922ed9 169static void smp_mb_master(void)
fdf01eed 170{
4477a870
MD
171 if (caa_likely(urcu_memb_has_sys_membarrier)) {
172 if (membarrier(urcu_memb_has_sys_membarrier_private_expedited ?
c0bb9f69
MD
173 MEMBARRIER_CMD_PRIVATE_EXPEDITED :
174 MEMBARRIER_CMD_SHARED, 0))
175 urcu_die(errno);
176 } else {
5481ddb3 177 cmm_smp_mb();
c0bb9f69 178 }
fdf01eed
MD
179}
180#endif
181
02be5561 182#ifdef RCU_MB
a4922ed9 183static void smp_mb_master(void)
40e140c9 184{
5481ddb3 185 cmm_smp_mb();
40e140c9 186}
fdf01eed
MD
187#endif
188
189#ifdef RCU_SIGNAL
78ff9419 190static void force_mb_all_readers(void)
27b012e2 191{
4477a870 192 struct urcu_reader *index;
e3b0cef0 193
27b012e2 194 /*
5481ddb3 195 * Ask for each threads to execute a cmm_smp_mb() so we can consider the
27b012e2
MD
196 * compiler barriers around rcu read lock as real memory barriers.
197 */
16aa9ee8 198 if (cds_list_empty(&registry))
27b012e2 199 return;
3a86deba 200 /*
5481ddb3 201 * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs
157dca95 202 * a cache flush on architectures with non-coherent cache. Let's play
5481ddb3 203 * safe and don't assume anything : we use cmm_smp_mc() to make sure the
157dca95 204 * cache flush is enforced.
3a86deba 205 */
16aa9ee8 206 cds_list_for_each_entry(index, &registry, node) {
6cf3827c 207 CMM_STORE_SHARED(index->need_mb, 1);
02be5561 208 pthread_kill(index->tid, SIGRCU);
09a9f986 209 }
27b012e2
MD
210 /*
211 * Wait for sighandler (and thus mb()) to execute on every thread.
09a9f986
PM
212 *
213 * Note that the pthread_kill() will never be executed on systems
214 * that correctly deliver signals in a timely manner. However, it
215 * is not uncommon for kernels to have bugs that can result in
216 * lost or unduly delayed signals.
217 *
218 * If you are seeing the below pthread_kill() executing much at
219 * all, we suggest testing the underlying kernel and filing the
220 * relevant bug report. For Linux kernels, we recommend getting
221 * the Linux Test Project (LTP).
27b012e2 222 */
16aa9ee8 223 cds_list_for_each_entry(index, &registry, node) {
6cf3827c 224 while (CMM_LOAD_SHARED(index->need_mb)) {
02be5561 225 pthread_kill(index->tid, SIGRCU);
8999a9ee 226 (void) poll(NULL, 0, 1);
09a9f986
PM
227 }
228 }
5481ddb3 229 cmm_smp_mb(); /* read ->need_mb before ending the barrier */
27b012e2 230}
9d7e3f89 231
a4922ed9 232static void smp_mb_master(void)
9d7e3f89
MD
233{
234 force_mb_all_readers();
235}
fdf01eed 236#endif /* #ifdef RCU_SIGNAL */
27b012e2 237
bc6c15bb
MD
238/*
239 * synchronize_rcu() waiting. Single thread.
fca9fb96
MD
240 * Always called with rcu_registry lock held. Releases this lock and
241 * grabs it again. Holds the lock when it returns.
bc6c15bb 242 */
cfe78e25 243static void wait_gp(void)
bc6c15bb 244{
fca9fb96
MD
245 /*
246 * Read reader_gp before read futex. smp_mb_master() needs to
247 * be called with the rcu registry lock held in RCU_SIGNAL
248 * flavor.
249 */
a4922ed9 250 smp_mb_master();
fca9fb96
MD
251 /* Temporarily unlock the registry lock. */
252 mutex_unlock(&rcu_registry_lock);
a18d0428
MD
253 while (uatomic_read(&rcu_gp.futex) == -1) {
254 if (!futex_async(&rcu_gp.futex, FUTEX_WAIT, -1, NULL, NULL, 0)) {
255 /*
256 * Prior queued wakeups queued by unrelated code
257 * using the same address can cause futex wait to
258 * return 0 even through the futex value is still
259 * -1 (spurious wakeups). Check the value again
260 * in user-space to validate whether it really
261 * differs from -1.
262 */
263 continue;
264 }
b0a841b4 265 switch (errno) {
a18d0428 266 case EAGAIN:
b0a841b4 267 /* Value already changed. */
fca9fb96 268 goto end;
b0a841b4
MD
269 case EINTR:
270 /* Retry if interrupted by signal. */
a18d0428 271 break; /* Get out of switch. Check again. */
b0a841b4
MD
272 default:
273 /* Unexpected error. */
274 urcu_die(errno);
275 }
276 }
fca9fb96
MD
277end:
278 /*
279 * Re-lock the registry lock before the next loop.
280 */
281 mutex_lock(&rcu_registry_lock);
bc6c15bb
MD
282}
283
731ccb96
MD
284/*
285 * Always called with rcu_registry lock held. Releases this lock between
286 * iterations and grabs it again. Holds the lock when it returns.
287 */
fd189fa5
MD
288static void wait_for_readers(struct cds_list_head *input_readers,
289 struct cds_list_head *cur_snap_readers,
601922a8
OD
290 struct cds_list_head *qsreaders,
291 cmm_annotate_t *group)
27b012e2 292{
9340c38d 293 unsigned int wait_loops = 0;
4477a870 294 struct urcu_reader *index, *tmp;
9340c38d
MD
295#ifdef HAS_INCOHERENT_CACHES
296 unsigned int wait_gp_loops = 0;
297#endif /* HAS_INCOHERENT_CACHES */
27b012e2 298
40e140c9 299 /*
c9488684
MD
300 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
301 * indicate quiescence (not nested), or observe the current
ed1b099e 302 * rcu_gp.ctr value.
27b012e2 303 */
cfe78e25 304 for (;;) {
5e81fed7
MD
305 if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS)
306 wait_loops++;
9340c38d 307 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
ed1b099e 308 uatomic_dec(&rcu_gp.futex);
cfe78e25 309 /* Write futex before read reader_gp */
a4922ed9 310 smp_mb_master();
cfe78e25
MD
311 }
312
fd189fa5 313 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
601922a8 314 switch (urcu_common_reader_state(&rcu_gp, &index->ctr, group)) {
4477a870 315 case URCU_READER_ACTIVE_CURRENT:
fd189fa5
MD
316 if (cur_snap_readers) {
317 cds_list_move(&index->node,
318 cur_snap_readers);
319 break;
320 }
321 /* Fall-through */
4477a870 322 case URCU_READER_INACTIVE:
fd189fa5
MD
323 cds_list_move(&index->node, qsreaders);
324 break;
4477a870 325 case URCU_READER_ACTIVE_OLD:
fd189fa5
MD
326 /*
327 * Old snapshot. Leaving node in
328 * input_readers will make us busy-loop
329 * until the snapshot becomes current or
330 * the reader becomes inactive.
331 */
332 break;
333 }
cfe78e25
MD
334 }
335
e8043c1b 336#ifndef HAS_INCOHERENT_CACHES
fd189fa5 337 if (cds_list_empty(input_readers)) {
9340c38d 338 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
cfe78e25 339 /* Read reader_gp before write futex */
a4922ed9 340 smp_mb_master();
ed1b099e 341 uatomic_set(&rcu_gp.futex, 0);
bc6c15bb 342 }
cfe78e25
MD
343 break;
344 } else {
fca9fb96
MD
345 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
346 /* wait_gp unlocks/locks registry lock. */
cfe78e25 347 wait_gp();
fca9fb96
MD
348 } else {
349 /* Temporarily unlock the registry lock. */
350 mutex_unlock(&rcu_registry_lock);
06f22bdb 351 caa_cpu_relax();
fca9fb96
MD
352 /*
353 * Re-lock the registry lock before the
354 * next loop.
355 */
356 mutex_lock(&rcu_registry_lock);
357 }
bc6c15bb 358 }
e8043c1b 359#else /* #ifndef HAS_INCOHERENT_CACHES */
27b012e2 360 /*
40e140c9 361 * BUSY-LOOP. Force the reader thread to commit its
bd252a04
MD
362 * URCU_TLS(rcu_reader).ctr update to memory if we wait
363 * for too long.
27b012e2 364 */
fd189fa5 365 if (cds_list_empty(input_readers)) {
9340c38d 366 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
cfe78e25 367 /* Read reader_gp before write futex */
a4922ed9 368 smp_mb_master();
ed1b099e 369 uatomic_set(&rcu_gp.futex, 0);
cfe78e25
MD
370 }
371 break;
372 } else {
9340c38d 373 if (wait_gp_loops == KICK_READER_LOOPS) {
a4922ed9 374 smp_mb_master();
9340c38d
MD
375 wait_gp_loops = 0;
376 }
377 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
fca9fb96 378 /* wait_gp unlocks/locks registry lock. */
9340c38d
MD
379 wait_gp();
380 wait_gp_loops++;
381 } else {
fca9fb96
MD
382 /* Temporarily unlock the registry lock. */
383 mutex_unlock(&rcu_registry_lock);
06f22bdb 384 caa_cpu_relax();
fca9fb96
MD
385 /*
386 * Re-lock the registry lock before the
387 * next loop.
388 */
389 mutex_lock(&rcu_registry_lock);
40e140c9
MD
390 }
391 }
e8043c1b 392#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
27b012e2 393 }
27b012e2
MD
394}
395
9598a481 396void synchronize_rcu(void)
2bc59bd7 397{
601922a8
OD
398 cmm_annotate_define(acquire_group);
399 cmm_annotate_define(release_group);
fd189fa5
MD
400 CDS_LIST_HEAD(cur_snap_readers);
401 CDS_LIST_HEAD(qsreaders);
5bffdd5d
MD
402 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
403 struct urcu_waiters waiters;
404
405 /*
406 * Add ourself to gp_waiters queue of threads awaiting to wait
407 * for a grace period. Proceed to perform the grace period only
408 * if we are the first thread added into the queue.
409 * The implicit memory barrier before urcu_wait_add()
410 * orders prior memory accesses of threads put into the wait
411 * queue before their insertion into the wait queue.
412 */
413 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
601922a8
OD
414 /*
415 * Not first in queue: will be awakened by another thread.
416 * Implies a memory barrier after grace period.
417 */
5bffdd5d 418 urcu_adaptative_busy_wait(&wait);
5bffdd5d
MD
419 return;
420 }
421 /* We won't need to wake ourself up */
422 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
fd189fa5 423
6abb4bd5 424 mutex_lock(&rcu_gp_lock);
135530fd 425
5bffdd5d
MD
426 /*
427 * Move all waiters into our local queue.
428 */
429 urcu_move_waiters(&waiters, &gp_waiters);
430
731ccb96
MD
431 mutex_lock(&rcu_registry_lock);
432
16aa9ee8 433 if (cds_list_empty(&registry))
2dfb8b5e
MD
434 goto out;
435
731ccb96
MD
436 /*
437 * All threads should read qparity before accessing data structure
438 * where new ptr points to. Must be done within rcu_registry_lock
439 * because it iterates on reader threads.
440 */
9598a481 441 /* Write new ptr before changing the qparity */
a4922ed9 442 smp_mb_master();
601922a8 443 cmm_annotate_group_mb_release(&release_group);
9598a481 444
9598a481 445 /*
c9488684 446 * Wait for readers to observe original parity or be quiescent.
731ccb96 447 * wait_for_readers() can release and grab again rcu_registry_lock
f99c6e92 448 * internally.
9598a481 449 */
601922a8 450 wait_for_readers(&registry, &cur_snap_readers, &qsreaders, &acquire_group);
9598a481
MD
451
452 /*
c9488684 453 * Must finish waiting for quiescent state for original parity before
ed1b099e 454 * committing next rcu_gp.ctr update to memory. Failure to do so could
d40fde2c
MD
455 * result in the writer waiting forever while new readers are always
456 * accessing data (no progress). Enforce compiler-order of load
ed1b099e 457 * URCU_TLS(rcu_reader).ctr before store to rcu_gp.ctr.
9598a481 458 */
5481ddb3 459 cmm_barrier();
9598a481 460
5dba80f9 461 /*
5481ddb3 462 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
5dba80f9
MD
463 * model easier to understand. It does not have a big performance impact
464 * anyway, given this is the write-side.
465 */
5481ddb3 466 cmm_smp_mb();
67c2d80b 467
c9488684 468 /* Switch parity: 0 -> 1, 1 -> 0 */
601922a8
OD
469 cmm_annotate_group_mem_release(&release_group, &rcu_gp.ctr);
470 uatomic_store(&rcu_gp.ctr, rcu_gp.ctr ^ URCU_GP_CTR_PHASE, CMM_RELAXED);
c9488684
MD
471
472 /*
ed1b099e 473 * Must commit rcu_gp.ctr update to memory before waiting for quiescent
c9488684
MD
474 * state. Failure to do so could result in the writer waiting forever
475 * while new readers are always accessing data (no progress). Enforce
ed1b099e 476 * compiler-order of store to rcu_gp.ctr before load rcu_reader ctr.
c9488684
MD
477 */
478 cmm_barrier();
479
480 /*
481 *
482 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
483 * model easier to understand. It does not have a big performance impact
484 * anyway, given this is the write-side.
485 */
486 cmm_smp_mb();
487
9598a481 488 /*
c9488684 489 * Wait for readers to observe new parity or be quiescent.
731ccb96 490 * wait_for_readers() can release and grab again rcu_registry_lock
f99c6e92 491 * internally.
9598a481 492 */
601922a8 493 wait_for_readers(&cur_snap_readers, NULL, &qsreaders, &acquire_group);
fd189fa5
MD
494
495 /*
496 * Put quiescent reader list back into registry.
497 */
498 cds_list_splice(&qsreaders, &registry);
9598a481 499
731ccb96
MD
500 /*
501 * Finish waiting for reader threads before letting the old ptr
502 * being freed. Must be done within rcu_registry_lock because it
503 * iterates on reader threads.
504 */
a4922ed9 505 smp_mb_master();
601922a8 506 cmm_annotate_group_mb_acquire(&acquire_group);
2dfb8b5e 507out:
731ccb96 508 mutex_unlock(&rcu_registry_lock);
6abb4bd5 509 mutex_unlock(&rcu_gp_lock);
5bffdd5d
MD
510
511 /*
512 * Wakeup waiters only after we have completed the grace period
513 * and have ensured the memory barriers at the end of the grace
514 * period have been issued.
515 */
516 urcu_wake_all_waiters(&waiters);
2bc59bd7
PM
517}
518
121a5d44
MD
519/*
520 * library wrappers to be used by non-LGPL compatible source code.
521 */
522
523void rcu_read_lock(void)
524{
525 _rcu_read_lock();
526}
527
528void rcu_read_unlock(void)
529{
530 _rcu_read_unlock();
531}
532
882f3357
MD
533int rcu_read_ongoing(void)
534{
535 return _rcu_read_ongoing();
536}
537
ea3a28a3
MD
538#ifdef RCU_SIGNAL
539/*
540 * Make sure the signal used by the urcu-signal flavor is unblocked
541 * while the thread is registered.
542 */
543static
544void urcu_signal_unblock(void)
545{
546 sigset_t mask, oldmask;
547 int ret;
548
549 ret = sigemptyset(&mask);
550 urcu_posix_assert(!ret);
551 ret = sigaddset(&mask, SIGRCU);
552 urcu_posix_assert(!ret);
553 ret = pthread_sigmask(SIG_UNBLOCK, &mask, &oldmask);
554 urcu_posix_assert(!ret);
555 URCU_TLS(rcu_signal_was_blocked) = sigismember(&oldmask, SIGRCU);
556}
557
558static
559void urcu_signal_restore(void)
560{
561 sigset_t mask;
562 int ret;
563
564 if (!URCU_TLS(rcu_signal_was_blocked))
565 return;
566 ret = sigemptyset(&mask);
567 urcu_posix_assert(!ret);
568 ret = sigaddset(&mask, SIGRCU);
569 urcu_posix_assert(!ret);
570 ret = pthread_sigmask(SIG_BLOCK, &mask, NULL);
571 urcu_posix_assert(!ret);
572}
573#else
574static
575void urcu_signal_unblock(void) { }
576static
577void urcu_signal_restore(void) { }
578#endif
579
121a5d44 580void rcu_register_thread(void)
27b012e2 581{
ea3a28a3
MD
582 urcu_signal_unblock();
583
bd252a04 584 URCU_TLS(rcu_reader).tid = pthread_self();
01477510
FD
585 urcu_posix_assert(URCU_TLS(rcu_reader).need_mb == 0);
586 urcu_posix_assert(!(URCU_TLS(rcu_reader).ctr & URCU_GP_CTR_NEST_MASK));
02be5561 587
731ccb96 588 mutex_lock(&rcu_registry_lock);
01477510 589 urcu_posix_assert(!URCU_TLS(rcu_reader).registered);
a77f7d82 590 URCU_TLS(rcu_reader).registered = 1;
02be5561 591 rcu_init(); /* In case gcc does not support constructor attribute */
bd252a04 592 cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
731ccb96 593 mutex_unlock(&rcu_registry_lock);
27b012e2
MD
594}
595
121a5d44 596void rcu_unregister_thread(void)
27b012e2 597{
731ccb96 598 mutex_lock(&rcu_registry_lock);
01477510 599 urcu_posix_assert(URCU_TLS(rcu_reader).registered);
a77f7d82 600 URCU_TLS(rcu_reader).registered = 0;
bd252a04 601 cds_list_del(&URCU_TLS(rcu_reader).node);
731ccb96 602 mutex_unlock(&rcu_registry_lock);
ea3a28a3
MD
603
604 urcu_signal_restore();
27b012e2
MD
605}
606
fdf01eed 607#ifdef RCU_MEMBARRIER
d8d9a340
MD
608
609#ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
610static
c0bb9f69 611void rcu_sys_membarrier_status(bool available)
d8d9a340
MD
612{
613 if (!available)
614 abort();
615}
616#else
617static
c0bb9f69 618void rcu_sys_membarrier_status(bool available)
d8d9a340 619{
c0bb9f69
MD
620 if (!available)
621 return;
4477a870 622 urcu_memb_has_sys_membarrier = 1;
d8d9a340
MD
623}
624#endif
625
c0bb9f69
MD
626static
627void rcu_sys_membarrier_init(void)
fdf01eed 628{
c0bb9f69
MD
629 bool available = false;
630 int mask;
631
632 mask = membarrier(MEMBARRIER_CMD_QUERY, 0);
633 if (mask >= 0) {
634 if (mask & MEMBARRIER_CMD_PRIVATE_EXPEDITED) {
635 if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0))
636 urcu_die(errno);
4477a870 637 urcu_memb_has_sys_membarrier_private_expedited = 1;
c0bb9f69
MD
638 available = true;
639 } else if (mask & MEMBARRIER_CMD_SHARED) {
640 available = true;
641 }
642 }
643 rcu_sys_membarrier_status(available);
644}
64f469e6 645
c0bb9f69
MD
646void rcu_init(void)
647{
fdf01eed
MD
648 if (init_done)
649 return;
650 init_done = 1;
c0bb9f69 651 rcu_sys_membarrier_init();
fdf01eed
MD
652}
653#endif
654
655#ifdef RCU_SIGNAL
70469b43
MJ
656static void sigrcu_handler(int signo __attribute__((unused)),
657 siginfo_t *siginfo __attribute__((unused)),
658 void *context __attribute__((unused)))
27b012e2 659{
40e140c9 660 /*
5481ddb3
DG
661 * Executing this cmm_smp_mb() is the only purpose of this signal handler.
662 * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
40e140c9
MD
663 * executed on.
664 */
5481ddb3 665 cmm_smp_mb();
bd252a04 666 _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
5481ddb3 667 cmm_smp_mb();
27b012e2
MD
668}
669
8a5fb4c9 670/*
02be5561 671 * rcu_init constructor. Called when the library is linked, but also when
8a5fb4c9
MD
672 * reader threads are calling rcu_register_thread().
673 * Should only be called by a single thread at a given time. This is ensured by
731ccb96
MD
674 * holing the rcu_registry_lock from rcu_register_thread() or by running
675 * at library load time, which should not be executed by multiple
676 * threads nor concurrently with rcu_register_thread() anyway.
8a5fb4c9 677 */
02be5561 678void rcu_init(void)
27b012e2
MD
679{
680 struct sigaction act;
681 int ret;
682
8a5fb4c9
MD
683 if (init_done)
684 return;
685 init_done = 1;
686
02be5561 687 act.sa_sigaction = sigrcu_handler;
dd052bd3 688 act.sa_flags = SA_SIGINFO | SA_RESTART;
c297c21c 689 sigemptyset(&act.sa_mask);
02be5561 690 ret = sigaction(SIGRCU, &act, NULL);
4a6d7378
MD
691 if (ret)
692 urcu_die(errno);
27b012e2
MD
693}
694
90f72b8c
MD
695/*
696 * Don't unregister the SIGRCU signal handler anymore, because
697 * call_rcu threads could still be using it shortly before the
698 * application exits.
699 * Assertion disabled because call_rcu threads are now rcu
700 * readers, and left running at exit.
701 * urcu_posix_assert(cds_list_empty(&registry));
702 */
703
704#endif /* #ifdef RCU_SIGNAL */
705
02be5561 706void rcu_exit(void)
27b012e2 707{
90f72b8c 708 urcu_call_rcu_exit();
27b012e2 709}
5e77fc1f 710
5e6b23a6 711DEFINE_RCU_FLAVOR(rcu_flavor);
541d828d 712
5e77fc1f 713#include "urcu-call-rcu-impl.h"
0376e7b2 714#include "urcu-defer-impl.h"
111bda8f 715#include "urcu-poll-impl.h"
This page took 0.087051 seconds and 4 git commands to generate.