uatomic: Specify complete types for atomic function calls
[userspace-rcu.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
ed42f676 29#define _DEFAULT_SOURCE
27b012e2
MD
30#include <stdio.h>
31#include <pthread.h>
32#include <signal.h>
33#include <assert.h>
f69f195a 34#include <stdlib.h>
6d841bc2 35#include <stdint.h>
f69f195a 36#include <string.h>
09a9f986 37#include <errno.h>
e8043c1b 38#include <poll.h>
27b012e2 39
71c811bf 40#include "urcu/wfqueue.h"
57760d44 41#include "urcu/map/urcu.h"
af7c2dbe 42#include "urcu/static/urcu.h"
618b2595 43#include "urcu-pointer.h"
bd252a04 44#include "urcu/tls-compat.h"
71c811bf 45
4a6d7378
MD
46#include "urcu-die.h"
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
6b702fa4 56 * per-se), kick it after 10 loops waiting for it.
3a71751e 57 */
6b702fa4 58#define KICK_READER_LOOPS 10
3a71751e
PB
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;
fdf01eed 67int 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
66bc4dcd
MD
85/*
86 * rcu_gp_lock ensures mutual exclusion between threads calling
87 * synchronize_rcu().
88 */
6abb4bd5 89static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
66bc4dcd
MD
90/*
91 * rcu_registry_lock ensures mutual exclusion between threads
92 * registering and unregistering themselves to/from the registry, and
93 * with threads reading that registry from synchronize_rcu(). However,
94 * this lock is not held all the way through the completion of awaiting
95 * for the grace period. It is sporadically released between iterations
96 * on the registry.
97 * rcu_registry_lock may nest inside rcu_gp_lock.
98 */
99static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER;
27b012e2 100
6d841bc2 101int32_t gp_futex;
bc6c15bb 102
128166c9
MD
103/*
104 * Global grace period counter.
02be5561 105 * Contains the current RCU_GP_CTR_PHASE.
afb8f2c9 106 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
b0d5e790 107 * Written to only by writer with mutex taken. Read by both writer and readers.
128166c9 108 */
27d65bc5 109unsigned long rcu_gp_ctr = RCU_GP_COUNT;
b0d5e790
MD
110/*
111 * Written to only by each individual reader. Read by both the reader and the
112 * writers.
113 */
1745be1a 114__DEFINE_URCU_TLS_GLOBAL(struct rcu_reader, rcu_reader);
27b012e2 115
cf380c2f 116#ifdef DEBUG_YIELD
9d335088 117unsigned int yield_active;
1745be1a 118__DEFINE_URCU_TLS_GLOBAL(unsigned int, rand_yield);
cf380c2f
MD
119#endif
120
16aa9ee8 121static CDS_LIST_HEAD(registry);
27b012e2 122
6abb4bd5 123static void mutex_lock(pthread_mutex_t *mutex)
41718ff9
MD
124{
125 int ret;
09a9f986
PM
126
127#ifndef DISTRUST_SIGNALS_EXTREME
6abb4bd5 128 ret = pthread_mutex_lock(mutex);
4a6d7378
MD
129 if (ret)
130 urcu_die(ret);
09a9f986 131#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
6abb4bd5 132 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
4a6d7378
MD
133 if (ret != EBUSY && ret != EINTR)
134 urcu_die(ret);
bd252a04 135 if (CMM_LOAD_SHARED(URCU_TLS(rcu_reader).need_mb)) {
5481ddb3 136 cmm_smp_mb();
bd252a04 137 _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
5481ddb3 138 cmm_smp_mb();
09a9f986
PM
139 }
140 poll(NULL,0,10);
141 }
142#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
41718ff9
MD
143}
144
6abb4bd5 145static void mutex_unlock(pthread_mutex_t *mutex)
41718ff9
MD
146{
147 int ret;
148
6abb4bd5 149 ret = pthread_mutex_unlock(mutex);
4a6d7378
MD
150 if (ret)
151 urcu_die(ret);
41718ff9
MD
152}
153
fdf01eed 154#ifdef RCU_MEMBARRIER
25cc6d18 155static void smp_mb_master(int group)
fdf01eed 156{
a0b7f7ea 157 if (caa_likely(has_sys_membarrier))
f0708810 158 membarrier(MEMBARRIER_EXPEDITED);
fdf01eed 159 else
5481ddb3 160 cmm_smp_mb();
fdf01eed
MD
161}
162#endif
163
02be5561 164#ifdef RCU_MB
25cc6d18 165static void smp_mb_master(int group)
40e140c9 166{
5481ddb3 167 cmm_smp_mb();
40e140c9 168}
fdf01eed
MD
169#endif
170
171#ifdef RCU_SIGNAL
78ff9419 172static void force_mb_all_readers(void)
27b012e2 173{
02be5561 174 struct rcu_reader *index;
e3b0cef0 175
27b012e2 176 /*
5481ddb3 177 * Ask for each threads to execute a cmm_smp_mb() so we can consider the
27b012e2
MD
178 * compiler barriers around rcu read lock as real memory barriers.
179 */
16aa9ee8 180 if (cds_list_empty(&registry))
27b012e2 181 return;
3a86deba 182 /*
5481ddb3 183 * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs
157dca95 184 * a cache flush on architectures with non-coherent cache. Let's play
5481ddb3 185 * safe and don't assume anything : we use cmm_smp_mc() to make sure the
157dca95 186 * cache flush is enforced.
3a86deba 187 */
16aa9ee8 188 cds_list_for_each_entry(index, &registry, node) {
6cf3827c 189 CMM_STORE_SHARED(index->need_mb, 1);
02be5561 190 pthread_kill(index->tid, SIGRCU);
09a9f986 191 }
27b012e2
MD
192 /*
193 * Wait for sighandler (and thus mb()) to execute on every thread.
09a9f986
PM
194 *
195 * Note that the pthread_kill() will never be executed on systems
196 * that correctly deliver signals in a timely manner. However, it
197 * is not uncommon for kernels to have bugs that can result in
198 * lost or unduly delayed signals.
199 *
200 * If you are seeing the below pthread_kill() executing much at
201 * all, we suggest testing the underlying kernel and filing the
202 * relevant bug report. For Linux kernels, we recommend getting
203 * the Linux Test Project (LTP).
27b012e2 204 */
16aa9ee8 205 cds_list_for_each_entry(index, &registry, node) {
6cf3827c 206 while (CMM_LOAD_SHARED(index->need_mb)) {
02be5561 207 pthread_kill(index->tid, SIGRCU);
09a9f986
PM
208 poll(NULL, 0, 1);
209 }
210 }
5481ddb3 211 cmm_smp_mb(); /* read ->need_mb before ending the barrier */
27b012e2 212}
9d7e3f89 213
25cc6d18 214static void smp_mb_master(int group)
9d7e3f89
MD
215{
216 force_mb_all_readers();
217}
fdf01eed 218#endif /* #ifdef RCU_SIGNAL */
27b012e2 219
bc6c15bb
MD
220/*
221 * synchronize_rcu() waiting. Single thread.
222 */
cfe78e25 223static void wait_gp(void)
bc6c15bb 224{
cfe78e25 225 /* Read reader_gp before read futex */
25cc6d18 226 smp_mb_master(RCU_MB_GROUP);
11eb040f
MD
227 if (uatomic_read(&gp_futex) != -1)
228 return;
229 while (futex_async(&gp_futex, FUTEX_WAIT, -1,
230 NULL, NULL, 0)) {
231 switch (errno) {
232 case EWOULDBLOCK:
233 /* Value already changed. */
234 return;
235 case EINTR:
236 /* Retry if interrupted by signal. */
237 break; /* Get out of switch. */
238 default:
239 /* Unexpected error. */
240 urcu_die(errno);
241 }
242 }
bc6c15bb
MD
243}
244
66bc4dcd
MD
245/*
246 * Always called with rcu_registry lock held. Releases this lock between
247 * iterations and grabs it again. Holds the lock when it returns.
248 */
2dfb8b5e 249void update_counter_and_wait(void)
27b012e2 250{
16aa9ee8 251 CDS_LIST_HEAD(qsreaders);
6b702fa4 252 unsigned int wait_loops = 0;
02be5561 253 struct rcu_reader *index, *tmp;
6b702fa4
MD
254#ifdef HAS_INCOHERENT_CACHES
255 unsigned int wait_gp_loops = 0;
256#endif /* HAS_INCOHERENT_CACHES */
27b012e2 257
32c15e4e 258 /* Switch parity: 0 -> 1, 1 -> 0 */
6cf3827c 259 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE);
2dfb8b5e
MD
260
261 /*
d40fde2c
MD
262 * Must commit rcu_gp_ctr update to memory before waiting for quiescent
263 * state. Failure to do so could result in the writer waiting forever
264 * while new readers are always accessing data (no progress). Enforce
265 * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr.
2dfb8b5e 266 */
5481ddb3 267 cmm_barrier();
2dfb8b5e
MD
268
269 /*
935b11ff 270 *
5481ddb3 271 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
2dfb8b5e
MD
272 * model easier to understand. It does not have a big performance impact
273 * anyway, given this is the write-side.
274 */
5481ddb3 275 cmm_smp_mb();
2dfb8b5e 276
40e140c9 277 /*
bd252a04 278 * Wait for each thread URCU_TLS(rcu_reader).ctr count to become 0.
27b012e2 279 */
cfe78e25 280 for (;;) {
cca4c8dc
MD
281 if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS)
282 wait_loops++;
6b702fa4 283 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
cfe78e25
MD
284 uatomic_dec(&gp_futex);
285 /* Write futex before read reader_gp */
25cc6d18 286 smp_mb_master(RCU_MB_GROUP);
cfe78e25
MD
287 }
288
16aa9ee8 289 cds_list_for_each_entry_safe(index, tmp, &registry, node) {
b95a001f 290 if (!rcu_gp_ongoing(&index->ctr))
16aa9ee8 291 cds_list_move(&index->node, &qsreaders);
cfe78e25
MD
292 }
293
e8043c1b 294#ifndef HAS_INCOHERENT_CACHES
16aa9ee8 295 if (cds_list_empty(&registry)) {
6b702fa4 296 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
cfe78e25 297 /* Read reader_gp before write futex */
25cc6d18 298 smp_mb_master(RCU_MB_GROUP);
cfe78e25 299 uatomic_set(&gp_futex, 0);
bc6c15bb 300 }
cfe78e25
MD
301 break;
302 } else {
66bc4dcd
MD
303 /* Temporarily unlock the registry lock. */
304 mutex_unlock(&rcu_registry_lock);
6b702fa4 305 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS)
cfe78e25
MD
306 wait_gp();
307 else
06f22bdb 308 caa_cpu_relax();
66bc4dcd
MD
309 /* Re-lock the registry lock before the next loop. */
310 mutex_lock(&rcu_registry_lock);
bc6c15bb 311 }
e8043c1b 312#else /* #ifndef HAS_INCOHERENT_CACHES */
27b012e2 313 /*
40e140c9 314 * BUSY-LOOP. Force the reader thread to commit its
bd252a04
MD
315 * URCU_TLS(rcu_reader).ctr update to memory if we wait
316 * for too long.
27b012e2 317 */
16aa9ee8 318 if (cds_list_empty(&registry)) {
6b702fa4 319 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
cfe78e25 320 /* Read reader_gp before write futex */
25cc6d18 321 smp_mb_master(RCU_MB_GROUP);
cfe78e25
MD
322 uatomic_set(&gp_futex, 0);
323 }
324 break;
325 } else {
6b702fa4 326 if (wait_gp_loops == KICK_READER_LOOPS) {
25cc6d18 327 smp_mb_master(RCU_MB_GROUP);
6b702fa4
MD
328 wait_gp_loops = 0;
329 }
66bc4dcd
MD
330 /* Temporarily unlock the registry lock. */
331 mutex_unlock(&rcu_registry_lock);
6b702fa4
MD
332 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
333 wait_gp();
334 wait_gp_loops++;
335 } else {
06f22bdb 336 caa_cpu_relax();
40e140c9 337 }
66bc4dcd
MD
338 /* Re-lock the registry lock before the next loop. */
339 mutex_lock(&rcu_registry_lock);
40e140c9 340 }
e8043c1b 341#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
27b012e2 342 }
cfe78e25 343 /* put back the reader list in the registry */
16aa9ee8 344 cds_list_splice(&qsreaders, &registry);
27b012e2
MD
345}
346
9598a481 347void synchronize_rcu(void)
2bc59bd7 348{
6abb4bd5 349 mutex_lock(&rcu_gp_lock);
66bc4dcd 350 mutex_lock(&rcu_registry_lock);
135530fd 351
16aa9ee8 352 if (cds_list_empty(&registry))
2dfb8b5e
MD
353 goto out;
354
66bc4dcd
MD
355 /*
356 * All threads should read qparity before accessing data structure
357 * where new ptr points to. Must be done within rcu_registry_lock
358 * because it iterates on reader threads.
359 */
9598a481 360 /* Write new ptr before changing the qparity */
25cc6d18 361 smp_mb_master(RCU_MB_GROUP);
9598a481 362
9598a481
MD
363 /*
364 * Wait for previous parity to be empty of readers.
66bc4dcd
MD
365 * update_counter_and_wait() can release and grab again
366 * rcu_registry_lock interally.
9598a481 367 */
2dfb8b5e 368 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
9598a481
MD
369
370 /*
371 * Must finish waiting for quiescent state for parity 0 before
d40fde2c
MD
372 * committing next rcu_gp_ctr update to memory. Failure to do so could
373 * result in the writer waiting forever while new readers are always
374 * accessing data (no progress). Enforce compiler-order of load
bd252a04 375 * URCU_TLS(rcu_reader).ctr before store to rcu_gp_ctr.
9598a481 376 */
5481ddb3 377 cmm_barrier();
9598a481 378
5dba80f9 379 /*
5481ddb3 380 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
5dba80f9
MD
381 * model easier to understand. It does not have a big performance impact
382 * anyway, given this is the write-side.
383 */
5481ddb3 384 cmm_smp_mb();
67c2d80b 385
9598a481
MD
386 /*
387 * Wait for previous parity to be empty of readers.
66bc4dcd
MD
388 * update_counter_and_wait() can release and grab again
389 * rcu_registry_lock interally.
9598a481 390 */
2dfb8b5e 391 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
9598a481 392
66bc4dcd
MD
393 /*
394 * Finish waiting for reader threads before letting the old ptr
395 * being freed. Must be done within rcu_registry_lock because it
396 * iterates on reader threads.
397 */
25cc6d18 398 smp_mb_master(RCU_MB_GROUP);
2dfb8b5e 399out:
66bc4dcd 400 mutex_unlock(&rcu_registry_lock);
6abb4bd5 401 mutex_unlock(&rcu_gp_lock);
2bc59bd7
PM
402}
403
121a5d44
MD
404/*
405 * library wrappers to be used by non-LGPL compatible source code.
406 */
407
408void rcu_read_lock(void)
409{
410 _rcu_read_lock();
411}
412
413void rcu_read_unlock(void)
414{
415 _rcu_read_unlock();
416}
417
121a5d44 418void rcu_register_thread(void)
27b012e2 419{
bd252a04
MD
420 URCU_TLS(rcu_reader).tid = pthread_self();
421 assert(URCU_TLS(rcu_reader).need_mb == 0);
422 assert(!(URCU_TLS(rcu_reader).ctr & RCU_GP_CTR_NEST_MASK));
02be5561 423
66bc4dcd 424 mutex_lock(&rcu_registry_lock);
02be5561 425 rcu_init(); /* In case gcc does not support constructor attribute */
bd252a04 426 cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
66bc4dcd 427 mutex_unlock(&rcu_registry_lock);
27b012e2
MD
428}
429
121a5d44 430void rcu_unregister_thread(void)
27b012e2 431{
66bc4dcd 432 mutex_lock(&rcu_registry_lock);
bd252a04 433 cds_list_del(&URCU_TLS(rcu_reader).node);
66bc4dcd 434 mutex_unlock(&rcu_registry_lock);
27b012e2
MD
435}
436
fdf01eed
MD
437#ifdef RCU_MEMBARRIER
438void rcu_init(void)
439{
440 if (init_done)
441 return;
442 init_done = 1;
cf5271ee 443 if (!membarrier(MEMBARRIER_EXPEDITED | MEMBARRIER_QUERY))
fdf01eed
MD
444 has_sys_membarrier = 1;
445}
446#endif
447
448#ifdef RCU_SIGNAL
02be5561 449static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context)
27b012e2 450{
40e140c9 451 /*
5481ddb3
DG
452 * Executing this cmm_smp_mb() is the only purpose of this signal handler.
453 * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
40e140c9
MD
454 * executed on.
455 */
5481ddb3 456 cmm_smp_mb();
bd252a04 457 _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
5481ddb3 458 cmm_smp_mb();
27b012e2
MD
459}
460
8a5fb4c9 461/*
02be5561 462 * rcu_init constructor. Called when the library is linked, but also when
8a5fb4c9
MD
463 * reader threads are calling rcu_register_thread().
464 * Should only be called by a single thread at a given time. This is ensured by
66bc4dcd
MD
465 * holing the rcu_registry_lock from rcu_register_thread() or by running
466 * at library load time, which should not be executed by multiple
467 * threads nor concurrently with rcu_register_thread() anyway.
8a5fb4c9 468 */
02be5561 469void rcu_init(void)
27b012e2
MD
470{
471 struct sigaction act;
472 int ret;
473
8a5fb4c9
MD
474 if (init_done)
475 return;
476 init_done = 1;
477
02be5561 478 act.sa_sigaction = sigrcu_handler;
dd052bd3 479 act.sa_flags = SA_SIGINFO | SA_RESTART;
c297c21c 480 sigemptyset(&act.sa_mask);
02be5561 481 ret = sigaction(SIGRCU, &act, NULL);
4a6d7378
MD
482 if (ret)
483 urcu_die(errno);
27b012e2
MD
484}
485
02be5561 486void rcu_exit(void)
27b012e2
MD
487{
488 struct sigaction act;
489 int ret;
490
02be5561 491 ret = sigaction(SIGRCU, NULL, &act);
4a6d7378
MD
492 if (ret)
493 urcu_die(errno);
02be5561 494 assert(act.sa_sigaction == sigrcu_handler);
16aa9ee8 495 assert(cds_list_empty(&registry));
27b012e2 496}
5e77fc1f 497
fdf01eed 498#endif /* #ifdef RCU_SIGNAL */
5e77fc1f 499
5e6b23a6 500DEFINE_RCU_FLAVOR(rcu_flavor);
541d828d 501
5e77fc1f 502#include "urcu-call-rcu-impl.h"
0376e7b2 503#include "urcu-defer-impl.h"
This page took 0.059731 seconds and 4 git commands to generate.