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