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