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