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