src: use SPDX identifiers
[urcu.git] / src / urcu-qsbr.c
CommitLineData
acdb82a2
MJ
1// SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2// SPDX-FileCopyrightText: 2009 Paul E. McKenney, IBM Corporation.
3//
4// SPDX-License-Identifier: LGPL-2.1-or-later
5
9f1621ca 6/*
7ac06cef 7 * Userspace RCU QSBR library
9f1621ca 8 *
9f1621ca
MD
9 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
10 */
11
e37faee1 12#define URCU_NO_COMPAT_IDENTIFIERS
71c811bf 13#define _LGPL_SOURCE
9f1621ca
MD
14#include <stdio.h>
15#include <pthread.h>
16#include <signal.h>
9f1621ca 17#include <stdlib.h>
6d841bc2 18#include <stdint.h>
9f1621ca
MD
19#include <string.h>
20#include <errno.h>
21#include <poll.h>
22
01477510 23#include <urcu/assert.h>
4477a870
MD
24#include <urcu/wfcqueue.h>
25#include <urcu/map/urcu-qsbr.h>
727f819d 26#define BUILD_QSBR_LIB
4477a870
MD
27#include <urcu/static/urcu-qsbr.h>
28#include <urcu/pointer.h>
29#include <urcu/tls-compat.h>
71c811bf 30
4a6d7378 31#include "urcu-die.h"
cba82d7b 32#include "urcu-wait.h"
ce28e67a 33#include "urcu-utils.h"
4a6d7378 34
4477a870 35#define URCU_API_MAP
9f1621ca 36/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
71c811bf 37#undef _LGPL_SOURCE
4477a870 38#include <urcu/urcu-qsbr.h>
71c811bf 39#define _LGPL_SOURCE
9f1621ca 40
4477a870 41void __attribute__((destructor)) urcu_qsbr_exit(void);
90f72b8c 42static void urcu_call_rcu_exit(void);
f6d18c64 43
731ccb96
MD
44/*
45 * rcu_gp_lock ensures mutual exclusion between threads calling
46 * synchronize_rcu().
47 */
6abb4bd5 48static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
731ccb96
MD
49/*
50 * rcu_registry_lock ensures mutual exclusion between threads
51 * registering and unregistering themselves to/from the registry, and
52 * with threads reading that registry from synchronize_rcu(). However,
53 * this lock is not held all the way through the completion of awaiting
54 * for the grace period. It is sporadically released between iterations
55 * on the registry.
56 * rcu_registry_lock may nest inside rcu_gp_lock.
57 */
58static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER;
4477a870 59struct urcu_gp urcu_qsbr_gp = { .ctr = URCU_QSBR_GP_ONLINE };
9f1621ca 60
408f6d92
PB
61/*
62 * Active attempts to check for reader Q.S. before calling futex().
63 */
64#define RCU_QS_ACTIVE_ATTEMPTS 100
65
9f1621ca
MD
66/*
67 * Written to only by each individual reader. Read by both the reader and the
68 * writers.
69 */
4477a870 70DEFINE_URCU_TLS(struct urcu_qsbr_reader, urcu_qsbr_reader);
9f1621ca 71
16aa9ee8 72static CDS_LIST_HEAD(registry);
9f1621ca 73
6362f68f 74/*
bf6822a6 75 * Queue keeping threads awaiting to wait for a grace period. Contains
6362f68f
MD
76 * struct gp_waiters_thread objects.
77 */
bf6822a6 78static DEFINE_URCU_WAIT_QUEUE(gp_waiters);
6362f68f 79
6abb4bd5 80static void mutex_lock(pthread_mutex_t *mutex)
9f1621ca
MD
81{
82 int ret;
83
84#ifndef DISTRUST_SIGNALS_EXTREME
6abb4bd5 85 ret = pthread_mutex_lock(mutex);
4a6d7378
MD
86 if (ret)
87 urcu_die(ret);
9f1621ca 88#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
6abb4bd5 89 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
4a6d7378
MD
90 if (ret != EBUSY && ret != EINTR)
91 urcu_die(ret);
9f1621ca
MD
92 poll(NULL,0,10);
93 }
94#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
95}
96
6abb4bd5 97static void mutex_unlock(pthread_mutex_t *mutex)
9f1621ca
MD
98{
99 int ret;
100
6abb4bd5 101 ret = pthread_mutex_unlock(mutex);
4a6d7378
MD
102 if (ret)
103 urcu_die(ret);
9f1621ca
MD
104}
105
bc6c15bb
MD
106/*
107 * synchronize_rcu() waiting. Single thread.
108 */
4d703340 109static void wait_gp(void)
bc6c15bb 110{
4d703340 111 /* Read reader_gp before read futex */
5481ddb3 112 cmm_smp_rmb();
4974ad5f
MD
113 while (uatomic_read(&urcu_qsbr_gp.futex) == -1) {
114 if (!futex_noasync(&urcu_qsbr_gp.futex, FUTEX_WAIT, -1, NULL, NULL, 0)) {
115 /*
116 * Prior queued wakeups queued by unrelated code
117 * using the same address can cause futex wait to
118 * return 0 even through the futex value is still
119 * -1 (spurious wakeups). Check the value again
120 * in user-space to validate whether it really
121 * differs from -1.
122 */
123 continue;
124 }
b0a841b4 125 switch (errno) {
4974ad5f 126 case EAGAIN:
b0a841b4
MD
127 /* Value already changed. */
128 return;
129 case EINTR:
130 /* Retry if interrupted by signal. */
4974ad5f 131 break; /* Get out of switch. Check again. */
b0a841b4
MD
132 default:
133 /* Unexpected error. */
134 urcu_die(errno);
135 }
136 }
bc6c15bb
MD
137}
138
731ccb96
MD
139/*
140 * Always called with rcu_registry lock held. Releases this lock between
141 * iterations and grabs it again. Holds the lock when it returns.
142 */
708d89f0
MD
143static void wait_for_readers(struct cds_list_head *input_readers,
144 struct cds_list_head *cur_snap_readers,
145 struct cds_list_head *qsreaders)
9f1621ca 146{
9340c38d 147 unsigned int wait_loops = 0;
4477a870 148 struct urcu_qsbr_reader *index, *tmp;
9f1621ca 149
9f1621ca 150 /*
4477a870 151 * Wait for each thread URCU_TLS(urcu_qsbr_reader).ctr to either
f6b42f9c 152 * indicate quiescence (offline), or for them to observe the
4477a870 153 * current urcu_qsbr_gp.ctr value.
9f1621ca 154 */
4d703340 155 for (;;) {
5e81fed7
MD
156 if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS)
157 wait_loops++;
83a2c421 158 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
4477a870 159 uatomic_set(&urcu_qsbr_gp.futex, -1);
83a2c421
PB
160 /*
161 * Write futex before write waiting (the other side
162 * reads them in the opposite order).
163 */
164 cmm_smp_wmb();
708d89f0 165 cds_list_for_each_entry(index, input_readers, node) {
83a2c421
PB
166 _CMM_STORE_SHARED(index->waiting, 1);
167 }
4d703340 168 /* Write futex before read reader_gp */
5481ddb3 169 cmm_smp_mb();
4d703340 170 }
708d89f0 171 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
4477a870
MD
172 switch (urcu_qsbr_reader_state(&index->ctr)) {
173 case URCU_READER_ACTIVE_CURRENT:
708d89f0
MD
174 if (cur_snap_readers) {
175 cds_list_move(&index->node,
176 cur_snap_readers);
177 break;
178 }
179 /* Fall-through */
4477a870 180 case URCU_READER_INACTIVE:
708d89f0
MD
181 cds_list_move(&index->node, qsreaders);
182 break;
4477a870 183 case URCU_READER_ACTIVE_OLD:
708d89f0
MD
184 /*
185 * Old snapshot. Leaving node in
186 * input_readers will make us busy-loop
187 * until the snapshot becomes current or
188 * the reader becomes inactive.
189 */
190 break;
191 }
4d703340 192 }
bc6c15bb 193
708d89f0 194 if (cds_list_empty(input_readers)) {
83a2c421 195 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
4d703340 196 /* Read reader_gp before write futex */
5481ddb3 197 cmm_smp_mb();
4477a870 198 uatomic_set(&urcu_qsbr_gp.futex, 0);
4d703340
MD
199 }
200 break;
201 } else {
731ccb96
MD
202 /* Temporarily unlock the registry lock. */
203 mutex_unlock(&rcu_registry_lock);
83a2c421 204 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
4d703340 205 wait_gp();
bc6c15bb 206 } else {
9f1621ca 207#ifndef HAS_INCOHERENT_CACHES
06f22bdb 208 caa_cpu_relax();
9f1621ca 209#else /* #ifndef HAS_INCOHERENT_CACHES */
5481ddb3 210 cmm_smp_mb();
9f1621ca 211#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
bc6c15bb 212 }
731ccb96
MD
213 /* Re-lock the registry lock before the next loop. */
214 mutex_lock(&rcu_registry_lock);
bc6c15bb 215 }
9f1621ca
MD
216 }
217}
218
47d2f29e
MD
219/*
220 * Using a two-subphases algorithm for architectures with smaller than 64-bit
221 * long-size to ensure we do not encounter an overflow bug.
222 */
223
b39e1761 224#if (CAA_BITS_PER_LONG < 64)
4477a870 225void urcu_qsbr_synchronize_rcu(void)
47d2f29e 226{
708d89f0
MD
227 CDS_LIST_HEAD(cur_snap_readers);
228 CDS_LIST_HEAD(qsreaders);
bc49c323 229 unsigned long was_online;
bf6822a6
MD
230 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
231 struct urcu_waiters waiters;
bc49c323 232
4477a870 233 was_online = urcu_qsbr_read_ongoing();
bc49c323 234
47d2f29e 235 /* All threads should read qparity before accessing data structure
27b940e7
PB
236 * where new ptr points to. In the "then" case, rcu_thread_offline
237 * includes a memory barrier.
238 *
bc49c323 239 * Mark the writer thread offline to make sure we don't wait for
5e77fc1f
PM
240 * our own quiescent state. This allows using synchronize_rcu()
241 * in threads registered as readers.
bc49c323 242 */
27b940e7 243 if (was_online)
4477a870 244 urcu_qsbr_thread_offline();
27b940e7
PB
245 else
246 cmm_smp_mb();
bc49c323 247
6362f68f 248 /*
bf6822a6 249 * Add ourself to gp_waiters queue of threads awaiting to wait
6362f68f 250 * for a grace period. Proceed to perform the grace period only
bf6822a6 251 * if we are the first thread added into the queue.
6362f68f 252 */
bf6822a6
MD
253 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
254 /* Not first in queue: will be awakened by another thread. */
255 urcu_adaptative_busy_wait(&wait);
6362f68f
MD
256 goto gp_end;
257 }
bf6822a6
MD
258 /* We won't need to wake ourself up */
259 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
6362f68f 260
6abb4bd5 261 mutex_lock(&rcu_gp_lock);
47d2f29e 262
6362f68f 263 /*
bf6822a6 264 * Move all waiters into our local queue.
6362f68f 265 */
bf6822a6 266 urcu_move_waiters(&waiters, &gp_waiters);
6362f68f 267
731ccb96
MD
268 mutex_lock(&rcu_registry_lock);
269
16aa9ee8 270 if (cds_list_empty(&registry))
2dfb8b5e 271 goto out;
47d2f29e
MD
272
273 /*
f6b42f9c 274 * Wait for readers to observe original parity or be quiescent.
731ccb96 275 * wait_for_readers() can release and grab again rcu_registry_lock
f99c6e92 276 * internally.
47d2f29e 277 */
708d89f0 278 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
47d2f29e
MD
279
280 /*
f6b42f9c 281 * Must finish waiting for quiescent state for original parity
4477a870 282 * before committing next urcu_qsbr_gp.ctr update to memory. Failure
f6b42f9c 283 * to do so could result in the writer waiting forever while new
5e77fc1f 284 * readers are always accessing data (no progress). Enforce
4477a870
MD
285 * compiler-order of load URCU_TLS(urcu_qsbr_reader).ctr before store
286 * to urcu_qsbr_gp.ctr.
47d2f29e 287 */
5481ddb3 288 cmm_barrier();
47d2f29e 289
47d2f29e 290 /*
5481ddb3 291 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
2dfb8b5e
MD
292 * model easier to understand. It does not have a big performance impact
293 * anyway, given this is the write-side.
47d2f29e 294 */
5481ddb3 295 cmm_smp_mb();
47d2f29e 296
f6b42f9c 297 /* Switch parity: 0 -> 1, 1 -> 0 */
4477a870 298 CMM_STORE_SHARED(urcu_qsbr_gp.ctr, urcu_qsbr_gp.ctr ^ URCU_QSBR_GP_CTR);
f6b42f9c 299
47d2f29e 300 /*
4477a870 301 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
f6b42f9c
MD
302 * quiescent state. Failure to do so could result in the writer
303 * waiting forever while new readers are always accessing data
4477a870
MD
304 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
305 * before load URCU_TLS(urcu_qsbr_reader).ctr.
47d2f29e 306 */
f6b42f9c
MD
307 cmm_barrier();
308
309 /*
310 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
311 * model easier to understand. It does not have a big performance impact
312 * anyway, given this is the write-side.
313 */
314 cmm_smp_mb();
315
316 /*
317 * Wait for readers to observe new parity or be quiescent.
731ccb96 318 * wait_for_readers() can release and grab again rcu_registry_lock
f99c6e92 319 * internally.
f6b42f9c 320 */
708d89f0
MD
321 wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
322
323 /*
324 * Put quiescent reader list back into registry.
325 */
326 cds_list_splice(&qsreaders, &registry);
2dfb8b5e 327out:
731ccb96 328 mutex_unlock(&rcu_registry_lock);
6abb4bd5 329 mutex_unlock(&rcu_gp_lock);
bf6822a6 330 urcu_wake_all_waiters(&waiters);
6362f68f 331gp_end:
bc49c323
MD
332 /*
333 * Finish waiting for reader threads before letting the old ptr being
47d2f29e
MD
334 * freed.
335 */
bc49c323 336 if (was_online)
4477a870 337 urcu_qsbr_thread_online();
27b940e7
PB
338 else
339 cmm_smp_mb();
47d2f29e 340}
b39e1761 341#else /* !(CAA_BITS_PER_LONG < 64) */
4477a870 342void urcu_qsbr_synchronize_rcu(void)
9f1621ca 343{
708d89f0 344 CDS_LIST_HEAD(qsreaders);
f0f7dbdd 345 unsigned long was_online;
bf6822a6
MD
346 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
347 struct urcu_waiters waiters;
ff2f67a0 348
4477a870 349 was_online = urcu_qsbr_read_ongoing();
ff2f67a0
MD
350
351 /*
352 * Mark the writer thread offline to make sure we don't wait for
5e77fc1f
PM
353 * our own quiescent state. This allows using synchronize_rcu()
354 * in threads registered as readers.
ff2f67a0 355 */
27b940e7 356 if (was_online)
4477a870 357 urcu_qsbr_thread_offline();
27b940e7
PB
358 else
359 cmm_smp_mb();
ff2f67a0 360
6362f68f 361 /*
bf6822a6 362 * Add ourself to gp_waiters queue of threads awaiting to wait
6362f68f 363 * for a grace period. Proceed to perform the grace period only
bf6822a6 364 * if we are the first thread added into the queue.
6362f68f 365 */
bf6822a6
MD
366 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
367 /* Not first in queue: will be awakened by another thread. */
368 urcu_adaptative_busy_wait(&wait);
6362f68f
MD
369 goto gp_end;
370 }
bf6822a6
MD
371 /* We won't need to wake ourself up */
372 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
6362f68f 373
6abb4bd5 374 mutex_lock(&rcu_gp_lock);
6362f68f
MD
375
376 /*
bf6822a6 377 * Move all waiters into our local queue.
6362f68f 378 */
bf6822a6 379 urcu_move_waiters(&waiters, &gp_waiters);
6362f68f 380
731ccb96
MD
381 mutex_lock(&rcu_registry_lock);
382
16aa9ee8 383 if (cds_list_empty(&registry))
2dfb8b5e 384 goto out;
f6b42f9c
MD
385
386 /* Increment current G.P. */
4477a870 387 CMM_STORE_SHARED(urcu_qsbr_gp.ctr, urcu_qsbr_gp.ctr + URCU_QSBR_GP_CTR);
f6b42f9c
MD
388
389 /*
4477a870 390 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
f6b42f9c
MD
391 * quiescent state. Failure to do so could result in the writer
392 * waiting forever while new readers are always accessing data
4477a870
MD
393 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
394 * before load URCU_TLS(urcu_qsbr_reader).ctr.
f6b42f9c
MD
395 */
396 cmm_barrier();
397
398 /*
399 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
400 * model easier to understand. It does not have a big performance impact
401 * anyway, given this is the write-side.
402 */
403 cmm_smp_mb();
404
405 /*
406 * Wait for readers to observe new count of be quiescent.
731ccb96 407 * wait_for_readers() can release and grab again rcu_registry_lock
f99c6e92 408 * internally.
f6b42f9c 409 */
708d89f0
MD
410 wait_for_readers(&registry, NULL, &qsreaders);
411
412 /*
413 * Put quiescent reader list back into registry.
414 */
415 cds_list_splice(&qsreaders, &registry);
2dfb8b5e 416out:
731ccb96 417 mutex_unlock(&rcu_registry_lock);
6abb4bd5 418 mutex_unlock(&rcu_gp_lock);
bf6822a6 419 urcu_wake_all_waiters(&waiters);
6362f68f 420gp_end:
ff2f67a0 421 if (was_online)
4477a870 422 urcu_qsbr_thread_online();
27b940e7
PB
423 else
424 cmm_smp_mb();
9f1621ca 425}
b39e1761 426#endif /* !(CAA_BITS_PER_LONG < 64) */
9f1621ca
MD
427
428/*
429 * library wrappers to be used by non-LGPL compatible source code.
430 */
431
4477a870 432void urcu_qsbr_read_lock(void)
9f1621ca 433{
4477a870 434 _urcu_qsbr_read_lock();
9f1621ca
MD
435}
436
4477a870 437void urcu_qsbr_read_unlock(void)
9f1621ca 438{
4477a870 439 _urcu_qsbr_read_unlock();
9f1621ca
MD
440}
441
4477a870 442int urcu_qsbr_read_ongoing(void)
882f3357 443{
4477a870 444 return _urcu_qsbr_read_ongoing();
882f3357 445}
4477a870 446void rcu_read_ongoing_qsbr();
882f3357 447
4477a870 448void urcu_qsbr_quiescent_state(void)
7ac06cef 449{
4477a870 450 _urcu_qsbr_quiescent_state();
7ac06cef 451}
4477a870 452void rcu_quiescent_state_qsbr();
7ac06cef 453
4477a870 454void urcu_qsbr_thread_offline(void)
7ac06cef 455{
4477a870 456 _urcu_qsbr_thread_offline();
7ac06cef 457}
4477a870 458void rcu_thread_offline_qsbr();
7ac06cef 459
4477a870 460void urcu_qsbr_thread_online(void)
7ac06cef 461{
4477a870 462 _urcu_qsbr_thread_online();
7ac06cef
MD
463}
464
4477a870 465void urcu_qsbr_register_thread(void)
9f1621ca 466{
4477a870 467 URCU_TLS(urcu_qsbr_reader).tid = pthread_self();
01477510 468 urcu_posix_assert(URCU_TLS(urcu_qsbr_reader).ctr == 0);
4f8e3380 469
731ccb96 470 mutex_lock(&rcu_registry_lock);
01477510 471 urcu_posix_assert(!URCU_TLS(urcu_qsbr_reader).registered);
4477a870
MD
472 URCU_TLS(urcu_qsbr_reader).registered = 1;
473 cds_list_add(&URCU_TLS(urcu_qsbr_reader).node, &registry);
731ccb96 474 mutex_unlock(&rcu_registry_lock);
4477a870 475 _urcu_qsbr_thread_online();
9f1621ca
MD
476}
477
4477a870 478void urcu_qsbr_unregister_thread(void)
9f1621ca 479{
76f3022f
MD
480 /*
481 * We have to make the thread offline otherwise we end up dealocking
482 * with a waiting writer.
483 */
4477a870 484 _urcu_qsbr_thread_offline();
01477510 485 urcu_posix_assert(URCU_TLS(urcu_qsbr_reader).registered);
4477a870 486 URCU_TLS(urcu_qsbr_reader).registered = 0;
731ccb96 487 mutex_lock(&rcu_registry_lock);
4477a870 488 cds_list_del(&URCU_TLS(urcu_qsbr_reader).node);
731ccb96 489 mutex_unlock(&rcu_registry_lock);
9f1621ca 490}
f6d18c64 491
4477a870 492void urcu_qsbr_exit(void)
f6d18c64 493{
01cadde4
MD
494 /*
495 * Assertion disabled because call_rcu threads are now rcu
496 * readers, and left running at exit.
01477510 497 * urcu_posix_assert(cds_list_empty(&registry));
01cadde4 498 */
90f72b8c 499 urcu_call_rcu_exit();
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"
111bda8f 506#include "urcu-poll-impl.h"
This page took 0.073998 seconds and 4 git commands to generate.