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