doc/examples: automake stop on error
[urcu.git] / urcu-qsbr.c
CommitLineData
9f1621ca 1/*
7ac06cef 2 * urcu-qsbr.c
9f1621ca 3 *
7ac06cef 4 * Userspace RCU QSBR library
9f1621ca 5 *
6982d6d7 6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9f1621ca
MD
7 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
8 *
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
22 *
23 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
24 */
25
c1d2c60b 26#define _GNU_SOURCE
71c811bf 27#define _LGPL_SOURCE
9f1621ca
MD
28#include <stdio.h>
29#include <pthread.h>
30#include <signal.h>
31#include <assert.h>
32#include <stdlib.h>
6d841bc2 33#include <stdint.h>
9f1621ca
MD
34#include <string.h>
35#include <errno.h>
36#include <poll.h>
37
d73fb81f 38#include "urcu/wfcqueue.h"
57760d44 39#include "urcu/map/urcu-qsbr.h"
727f819d 40#define BUILD_QSBR_LIB
af7c2dbe 41#include "urcu/static/urcu-qsbr.h"
618b2595 42#include "urcu-pointer.h"
bd252a04 43#include "urcu/tls-compat.h"
71c811bf 44
4a6d7378 45#include "urcu-die.h"
cba82d7b 46#include "urcu-wait.h"
4a6d7378 47
9f1621ca 48/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
71c811bf 49#undef _LGPL_SOURCE
7ac06cef 50#include "urcu-qsbr.h"
71c811bf 51#define _LGPL_SOURCE
9f1621ca 52
f6d18c64
MD
53void __attribute__((destructor)) rcu_exit(void);
54
6abb4bd5 55static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
4de0cd31 56struct rcu_gp rcu_gp = { .ctr = RCU_GP_ONLINE };
9f1621ca 57
408f6d92
PB
58/*
59 * Active attempts to check for reader Q.S. before calling futex().
60 */
61#define RCU_QS_ACTIVE_ATTEMPTS 100
62
9f1621ca
MD
63/*
64 * Written to only by each individual reader. Read by both the reader and the
65 * writers.
66 */
bd252a04 67DEFINE_URCU_TLS(struct rcu_reader, rcu_reader);
9f1621ca
MD
68
69#ifdef DEBUG_YIELD
1de4df4b
MD
70unsigned int rcu_yield_active;
71DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
9f1621ca
MD
72#endif
73
16aa9ee8 74static CDS_LIST_HEAD(registry);
9f1621ca 75
6362f68f 76/*
bf6822a6 77 * Queue keeping threads awaiting to wait for a grace period. Contains
6362f68f
MD
78 * struct gp_waiters_thread objects.
79 */
bf6822a6 80static DEFINE_URCU_WAIT_QUEUE(gp_waiters);
6362f68f 81
6abb4bd5 82static void mutex_lock(pthread_mutex_t *mutex)
9f1621ca
MD
83{
84 int ret;
85
86#ifndef DISTRUST_SIGNALS_EXTREME
6abb4bd5 87 ret = pthread_mutex_lock(mutex);
4a6d7378
MD
88 if (ret)
89 urcu_die(ret);
9f1621ca 90#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
6abb4bd5 91 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
4a6d7378
MD
92 if (ret != EBUSY && ret != EINTR)
93 urcu_die(ret);
9f1621ca
MD
94 poll(NULL,0,10);
95 }
96#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
97}
98
6abb4bd5 99static void mutex_unlock(pthread_mutex_t *mutex)
9f1621ca
MD
100{
101 int ret;
102
6abb4bd5 103 ret = pthread_mutex_unlock(mutex);
4a6d7378
MD
104 if (ret)
105 urcu_die(ret);
9f1621ca
MD
106}
107
bc6c15bb
MD
108/*
109 * synchronize_rcu() waiting. Single thread.
110 */
4d703340 111static void wait_gp(void)
bc6c15bb 112{
4d703340 113 /* Read reader_gp before read futex */
5481ddb3 114 cmm_smp_rmb();
15302e28
LJ
115 if (uatomic_read(&rcu_gp.futex) == -1)
116 futex_noasync(&rcu_gp.futex, FUTEX_WAIT, -1,
4d703340 117 NULL, NULL, 0);
bc6c15bb
MD
118}
119
708d89f0
MD
120static void wait_for_readers(struct cds_list_head *input_readers,
121 struct cds_list_head *cur_snap_readers,
122 struct cds_list_head *qsreaders)
9f1621ca 123{
4d703340 124 int wait_loops = 0;
02be5561 125 struct rcu_reader *index, *tmp;
9f1621ca 126
9f1621ca 127 /*
f6b42f9c
MD
128 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
129 * indicate quiescence (offline), or for them to observe the
15302e28 130 * current rcu_gp.ctr value.
9f1621ca 131 */
4d703340
MD
132 for (;;) {
133 wait_loops++;
83a2c421 134 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
15302e28 135 uatomic_set(&rcu_gp.futex, -1);
83a2c421
PB
136 /*
137 * Write futex before write waiting (the other side
138 * reads them in the opposite order).
139 */
140 cmm_smp_wmb();
708d89f0 141 cds_list_for_each_entry(index, input_readers, node) {
83a2c421
PB
142 _CMM_STORE_SHARED(index->waiting, 1);
143 }
4d703340 144 /* Write futex before read reader_gp */
5481ddb3 145 cmm_smp_mb();
4d703340 146 }
708d89f0
MD
147 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
148 switch (rcu_reader_state(&index->ctr)) {
149 case RCU_READER_ACTIVE_CURRENT:
150 if (cur_snap_readers) {
151 cds_list_move(&index->node,
152 cur_snap_readers);
153 break;
154 }
155 /* Fall-through */
156 case RCU_READER_INACTIVE:
157 cds_list_move(&index->node, qsreaders);
158 break;
159 case RCU_READER_ACTIVE_OLD:
160 /*
161 * Old snapshot. Leaving node in
162 * input_readers will make us busy-loop
163 * until the snapshot becomes current or
164 * the reader becomes inactive.
165 */
166 break;
167 }
4d703340 168 }
bc6c15bb 169
708d89f0 170 if (cds_list_empty(input_readers)) {
83a2c421 171 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
4d703340 172 /* Read reader_gp before write futex */
5481ddb3 173 cmm_smp_mb();
15302e28 174 uatomic_set(&rcu_gp.futex, 0);
4d703340
MD
175 }
176 break;
177 } else {
83a2c421 178 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
4d703340 179 wait_gp();
bc6c15bb 180 } else {
9f1621ca 181#ifndef HAS_INCOHERENT_CACHES
06f22bdb 182 caa_cpu_relax();
9f1621ca 183#else /* #ifndef HAS_INCOHERENT_CACHES */
5481ddb3 184 cmm_smp_mb();
9f1621ca 185#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
bc6c15bb
MD
186 }
187 }
9f1621ca
MD
188 }
189}
190
47d2f29e
MD
191/*
192 * Using a two-subphases algorithm for architectures with smaller than 64-bit
193 * long-size to ensure we do not encounter an overflow bug.
194 */
195
b39e1761 196#if (CAA_BITS_PER_LONG < 64)
47d2f29e
MD
197void synchronize_rcu(void)
198{
708d89f0
MD
199 CDS_LIST_HEAD(cur_snap_readers);
200 CDS_LIST_HEAD(qsreaders);
bc49c323 201 unsigned long was_online;
bf6822a6
MD
202 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
203 struct urcu_waiters waiters;
bc49c323 204
882f3357 205 was_online = rcu_read_ongoing();
bc49c323 206
47d2f29e 207 /* All threads should read qparity before accessing data structure
27b940e7
PB
208 * where new ptr points to. In the "then" case, rcu_thread_offline
209 * includes a memory barrier.
210 *
bc49c323 211 * Mark the writer thread offline to make sure we don't wait for
5e77fc1f
PM
212 * our own quiescent state. This allows using synchronize_rcu()
213 * in threads registered as readers.
bc49c323 214 */
27b940e7
PB
215 if (was_online)
216 rcu_thread_offline();
217 else
218 cmm_smp_mb();
bc49c323 219
6362f68f 220 /*
bf6822a6 221 * Add ourself to gp_waiters queue of threads awaiting to wait
6362f68f 222 * for a grace period. Proceed to perform the grace period only
bf6822a6 223 * if we are the first thread added into the queue.
6362f68f 224 */
bf6822a6
MD
225 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
226 /* Not first in queue: will be awakened by another thread. */
227 urcu_adaptative_busy_wait(&wait);
6362f68f
MD
228 goto gp_end;
229 }
bf6822a6
MD
230 /* We won't need to wake ourself up */
231 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
6362f68f 232
6abb4bd5 233 mutex_lock(&rcu_gp_lock);
47d2f29e 234
6362f68f 235 /*
bf6822a6 236 * Move all waiters into our local queue.
6362f68f 237 */
bf6822a6 238 urcu_move_waiters(&waiters, &gp_waiters);
6362f68f 239
16aa9ee8 240 if (cds_list_empty(&registry))
2dfb8b5e 241 goto out;
47d2f29e
MD
242
243 /*
f6b42f9c 244 * Wait for readers to observe original parity or be quiescent.
47d2f29e 245 */
708d89f0 246 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
47d2f29e
MD
247
248 /*
f6b42f9c 249 * Must finish waiting for quiescent state for original parity
15302e28 250 * before committing next rcu_gp.ctr update to memory. Failure
f6b42f9c 251 * to do so could result in the writer waiting forever while new
5e77fc1f 252 * readers are always accessing data (no progress). Enforce
f6b42f9c 253 * compiler-order of load URCU_TLS(rcu_reader).ctr before store
15302e28 254 * to rcu_gp.ctr.
47d2f29e 255 */
5481ddb3 256 cmm_barrier();
47d2f29e 257
47d2f29e 258 /*
5481ddb3 259 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
2dfb8b5e
MD
260 * model easier to understand. It does not have a big performance impact
261 * anyway, given this is the write-side.
47d2f29e 262 */
5481ddb3 263 cmm_smp_mb();
47d2f29e 264
f6b42f9c 265 /* Switch parity: 0 -> 1, 1 -> 0 */
15302e28 266 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ RCU_GP_CTR);
f6b42f9c 267
47d2f29e 268 /*
15302e28 269 * Must commit rcu_gp.ctr update to memory before waiting for
f6b42f9c
MD
270 * quiescent state. Failure to do so could result in the writer
271 * waiting forever while new readers are always accessing data
15302e28 272 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
f6b42f9c 273 * before load URCU_TLS(rcu_reader).ctr.
47d2f29e 274 */
f6b42f9c
MD
275 cmm_barrier();
276
277 /*
278 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
279 * model easier to understand. It does not have a big performance impact
280 * anyway, given this is the write-side.
281 */
282 cmm_smp_mb();
283
284 /*
285 * Wait for readers to observe new parity or be quiescent.
286 */
708d89f0
MD
287 wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
288
289 /*
290 * Put quiescent reader list back into registry.
291 */
292 cds_list_splice(&qsreaders, &registry);
2dfb8b5e 293out:
6abb4bd5 294 mutex_unlock(&rcu_gp_lock);
bf6822a6 295 urcu_wake_all_waiters(&waiters);
6362f68f 296gp_end:
bc49c323
MD
297 /*
298 * Finish waiting for reader threads before letting the old ptr being
47d2f29e
MD
299 * freed.
300 */
bc49c323 301 if (was_online)
27b940e7
PB
302 rcu_thread_online();
303 else
304 cmm_smp_mb();
47d2f29e 305}
b39e1761 306#else /* !(CAA_BITS_PER_LONG < 64) */
9f1621ca
MD
307void synchronize_rcu(void)
308{
708d89f0 309 CDS_LIST_HEAD(qsreaders);
f0f7dbdd 310 unsigned long was_online;
bf6822a6
MD
311 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
312 struct urcu_waiters waiters;
ff2f67a0 313
882f3357 314 was_online = rcu_read_ongoing();
ff2f67a0
MD
315
316 /*
317 * Mark the writer thread offline to make sure we don't wait for
5e77fc1f
PM
318 * our own quiescent state. This allows using synchronize_rcu()
319 * in threads registered as readers.
ff2f67a0 320 */
27b940e7
PB
321 if (was_online)
322 rcu_thread_offline();
323 else
324 cmm_smp_mb();
ff2f67a0 325
6362f68f 326 /*
bf6822a6 327 * Add ourself to gp_waiters queue of threads awaiting to wait
6362f68f 328 * for a grace period. Proceed to perform the grace period only
bf6822a6 329 * if we are the first thread added into the queue.
6362f68f 330 */
bf6822a6
MD
331 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
332 /* Not first in queue: will be awakened by another thread. */
333 urcu_adaptative_busy_wait(&wait);
6362f68f
MD
334 goto gp_end;
335 }
bf6822a6
MD
336 /* We won't need to wake ourself up */
337 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
6362f68f 338
6abb4bd5 339 mutex_lock(&rcu_gp_lock);
6362f68f
MD
340
341 /*
bf6822a6 342 * Move all waiters into our local queue.
6362f68f 343 */
bf6822a6 344 urcu_move_waiters(&waiters, &gp_waiters);
6362f68f 345
16aa9ee8 346 if (cds_list_empty(&registry))
2dfb8b5e 347 goto out;
f6b42f9c
MD
348
349 /* Increment current G.P. */
15302e28 350 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr + RCU_GP_CTR);
f6b42f9c
MD
351
352 /*
15302e28 353 * Must commit rcu_gp.ctr update to memory before waiting for
f6b42f9c
MD
354 * quiescent state. Failure to do so could result in the writer
355 * waiting forever while new readers are always accessing data
15302e28 356 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
f6b42f9c
MD
357 * before load URCU_TLS(rcu_reader).ctr.
358 */
359 cmm_barrier();
360
361 /*
362 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
363 * model easier to understand. It does not have a big performance impact
364 * anyway, given this is the write-side.
365 */
366 cmm_smp_mb();
367
368 /*
369 * Wait for readers to observe new count of be quiescent.
370 */
708d89f0
MD
371 wait_for_readers(&registry, NULL, &qsreaders);
372
373 /*
374 * Put quiescent reader list back into registry.
375 */
376 cds_list_splice(&qsreaders, &registry);
2dfb8b5e 377out:
6abb4bd5 378 mutex_unlock(&rcu_gp_lock);
bf6822a6 379 urcu_wake_all_waiters(&waiters);
6362f68f 380gp_end:
ff2f67a0 381 if (was_online)
27b940e7
PB
382 rcu_thread_online();
383 else
384 cmm_smp_mb();
9f1621ca 385}
b39e1761 386#endif /* !(CAA_BITS_PER_LONG < 64) */
9f1621ca
MD
387
388/*
389 * library wrappers to be used by non-LGPL compatible source code.
390 */
391
392void rcu_read_lock(void)
393{
394 _rcu_read_lock();
395}
396
397void rcu_read_unlock(void)
398{
399 _rcu_read_unlock();
400}
401
882f3357
MD
402int rcu_read_ongoing(void)
403{
404 return _rcu_read_ongoing();
405}
406
7ac06cef
MD
407void rcu_quiescent_state(void)
408{
409 _rcu_quiescent_state();
410}
411
412void rcu_thread_offline(void)
413{
414 _rcu_thread_offline();
415}
416
417void rcu_thread_online(void)
418{
419 _rcu_thread_online();
420}
421
9f1621ca
MD
422void rcu_register_thread(void)
423{
bd252a04
MD
424 URCU_TLS(rcu_reader).tid = pthread_self();
425 assert(URCU_TLS(rcu_reader).ctr == 0);
4f8e3380 426
6abb4bd5 427 mutex_lock(&rcu_gp_lock);
bd252a04 428 cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
6abb4bd5 429 mutex_unlock(&rcu_gp_lock);
5f373c84 430 _rcu_thread_online();
9f1621ca
MD
431}
432
433void rcu_unregister_thread(void)
434{
76f3022f
MD
435 /*
436 * We have to make the thread offline otherwise we end up dealocking
437 * with a waiting writer.
438 */
439 _rcu_thread_offline();
6abb4bd5 440 mutex_lock(&rcu_gp_lock);
bd252a04 441 cds_list_del(&URCU_TLS(rcu_reader).node);
6abb4bd5 442 mutex_unlock(&rcu_gp_lock);
9f1621ca 443}
f6d18c64
MD
444
445void rcu_exit(void)
446{
01cadde4
MD
447 /*
448 * Assertion disabled because call_rcu threads are now rcu
449 * readers, and left running at exit.
450 * assert(cds_list_empty(&registry));
451 */
f6d18c64 452}
5e77fc1f 453
5e6b23a6 454DEFINE_RCU_FLAVOR(rcu_flavor);
541d828d 455
5e77fc1f 456#include "urcu-call-rcu-impl.h"
0376e7b2 457#include "urcu-defer-impl.h"
This page took 0.053444 seconds and 4 git commands to generate.