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