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