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