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