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