urcu-bp: improve 2-phase wait scheme
[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
MD
45#include "urcu-die.h"
46
9f1621ca 47/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
71c811bf 48#undef _LGPL_SOURCE
7ac06cef 49#include "urcu-qsbr.h"
71c811bf 50#define _LGPL_SOURCE
9f1621ca 51
f6d18c64
MD
52void __attribute__((destructor)) rcu_exit(void);
53
6abb4bd5 54static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
9f1621ca 55
1de4df4b 56int32_t rcu_gp_futex;
bc6c15bb 57
9f1621ca
MD
58/*
59 * Global grace period counter.
60 */
02be5561 61unsigned long rcu_gp_ctr = RCU_GP_ONLINE;
9f1621ca 62
408f6d92
PB
63/*
64 * Active attempts to check for reader Q.S. before calling futex().
65 */
66#define RCU_QS_ACTIVE_ATTEMPTS 100
67
9f1621ca
MD
68/*
69 * Written to only by each individual reader. Read by both the reader and the
70 * writers.
71 */
bd252a04 72DEFINE_URCU_TLS(struct rcu_reader, rcu_reader);
9f1621ca
MD
73
74#ifdef DEBUG_YIELD
1de4df4b
MD
75unsigned int rcu_yield_active;
76DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
9f1621ca
MD
77#endif
78
16aa9ee8 79static CDS_LIST_HEAD(registry);
9f1621ca 80
6abb4bd5 81static void mutex_lock(pthread_mutex_t *mutex)
9f1621ca
MD
82{
83 int ret;
84
85#ifndef DISTRUST_SIGNALS_EXTREME
6abb4bd5 86 ret = pthread_mutex_lock(mutex);
4a6d7378
MD
87 if (ret)
88 urcu_die(ret);
9f1621ca 89#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
6abb4bd5 90 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
4a6d7378
MD
91 if (ret != EBUSY && ret != EINTR)
92 urcu_die(ret);
9f1621ca
MD
93 poll(NULL,0,10);
94 }
95#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
96}
97
6abb4bd5 98static void mutex_unlock(pthread_mutex_t *mutex)
9f1621ca
MD
99{
100 int ret;
101
6abb4bd5 102 ret = pthread_mutex_unlock(mutex);
4a6d7378
MD
103 if (ret)
104 urcu_die(ret);
9f1621ca
MD
105}
106
bc6c15bb
MD
107/*
108 * synchronize_rcu() waiting. Single thread.
109 */
4d703340 110static void wait_gp(void)
bc6c15bb 111{
4d703340 112 /* Read reader_gp before read futex */
5481ddb3 113 cmm_smp_rmb();
1de4df4b
MD
114 if (uatomic_read(&rcu_gp_futex) == -1)
115 futex_noasync(&rcu_gp_futex, FUTEX_WAIT, -1,
4d703340 116 NULL, NULL, 0);
bc6c15bb
MD
117}
118
f6b42f9c 119static void wait_for_readers(void)
9f1621ca 120{
16aa9ee8 121 CDS_LIST_HEAD(qsreaders);
4d703340 122 int wait_loops = 0;
02be5561 123 struct rcu_reader *index, *tmp;
9f1621ca 124
9f1621ca 125 /*
f6b42f9c
MD
126 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
127 * indicate quiescence (offline), or for them to observe the
128 * current rcu_gp_ctr value.
9f1621ca 129 */
4d703340
MD
130 for (;;) {
131 wait_loops++;
83a2c421 132 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
1de4df4b 133 uatomic_set(&rcu_gp_futex, -1);
83a2c421
PB
134 /*
135 * Write futex before write waiting (the other side
136 * reads them in the opposite order).
137 */
138 cmm_smp_wmb();
139 cds_list_for_each_entry(index, &registry, node) {
140 _CMM_STORE_SHARED(index->waiting, 1);
141 }
4d703340 142 /* Write futex before read reader_gp */
5481ddb3 143 cmm_smp_mb();
4d703340 144 }
16aa9ee8 145 cds_list_for_each_entry_safe(index, tmp, &registry, node) {
4d703340 146 if (!rcu_gp_ongoing(&index->ctr))
16aa9ee8 147 cds_list_move(&index->node, &qsreaders);
4d703340 148 }
bc6c15bb 149
16aa9ee8 150 if (cds_list_empty(&registry)) {
83a2c421 151 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
4d703340 152 /* Read reader_gp before write futex */
5481ddb3 153 cmm_smp_mb();
1de4df4b 154 uatomic_set(&rcu_gp_futex, 0);
4d703340
MD
155 }
156 break;
157 } else {
83a2c421 158 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
4d703340 159 wait_gp();
bc6c15bb 160 } else {
9f1621ca 161#ifndef HAS_INCOHERENT_CACHES
06f22bdb 162 caa_cpu_relax();
9f1621ca 163#else /* #ifndef HAS_INCOHERENT_CACHES */
5481ddb3 164 cmm_smp_mb();
9f1621ca 165#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
bc6c15bb
MD
166 }
167 }
9f1621ca 168 }
4d703340 169 /* put back the reader list in the registry */
16aa9ee8 170 cds_list_splice(&qsreaders, &registry);
9f1621ca
MD
171}
172
47d2f29e
MD
173/*
174 * Using a two-subphases algorithm for architectures with smaller than 64-bit
175 * long-size to ensure we do not encounter an overflow bug.
176 */
177
b39e1761 178#if (CAA_BITS_PER_LONG < 64)
47d2f29e
MD
179void synchronize_rcu(void)
180{
bc49c323
MD
181 unsigned long was_online;
182
bd252a04 183 was_online = URCU_TLS(rcu_reader).ctr;
bc49c323 184
47d2f29e 185 /* All threads should read qparity before accessing data structure
27b940e7
PB
186 * where new ptr points to. In the "then" case, rcu_thread_offline
187 * includes a memory barrier.
188 *
bc49c323 189 * Mark the writer thread offline to make sure we don't wait for
5e77fc1f
PM
190 * our own quiescent state. This allows using synchronize_rcu()
191 * in threads registered as readers.
bc49c323 192 */
27b940e7
PB
193 if (was_online)
194 rcu_thread_offline();
195 else
196 cmm_smp_mb();
bc49c323 197
6abb4bd5 198 mutex_lock(&rcu_gp_lock);
47d2f29e 199
16aa9ee8 200 if (cds_list_empty(&registry))
2dfb8b5e 201 goto out;
47d2f29e
MD
202
203 /*
f6b42f9c 204 * Wait for readers to observe original parity or be quiescent.
47d2f29e 205 */
f6b42f9c 206 wait_for_readers();
47d2f29e
MD
207
208 /*
f6b42f9c
MD
209 * Must finish waiting for quiescent state for original parity
210 * before committing next rcu_gp_ctr update to memory. Failure
211 * to do so could result in the writer waiting forever while new
5e77fc1f 212 * readers are always accessing data (no progress). Enforce
f6b42f9c
MD
213 * compiler-order of load URCU_TLS(rcu_reader).ctr before store
214 * to rcu_gp_ctr.
47d2f29e 215 */
5481ddb3 216 cmm_barrier();
47d2f29e 217
47d2f29e 218 /*
5481ddb3 219 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
2dfb8b5e
MD
220 * model easier to understand. It does not have a big performance impact
221 * anyway, given this is the write-side.
47d2f29e 222 */
5481ddb3 223 cmm_smp_mb();
47d2f29e 224
f6b42f9c
MD
225 /* Switch parity: 0 -> 1, 1 -> 0 */
226 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR);
227
47d2f29e 228 /*
f6b42f9c
MD
229 * Must commit rcu_gp_ctr update to memory before waiting for
230 * quiescent state. Failure to do so could result in the writer
231 * waiting forever while new readers are always accessing data
232 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
233 * before load URCU_TLS(rcu_reader).ctr.
47d2f29e 234 */
f6b42f9c
MD
235 cmm_barrier();
236
237 /*
238 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
239 * model easier to understand. It does not have a big performance impact
240 * anyway, given this is the write-side.
241 */
242 cmm_smp_mb();
243
244 /*
245 * Wait for readers to observe new parity or be quiescent.
246 */
247 wait_for_readers();
2dfb8b5e 248out:
6abb4bd5 249 mutex_unlock(&rcu_gp_lock);
47d2f29e 250
bc49c323
MD
251 /*
252 * Finish waiting for reader threads before letting the old ptr being
47d2f29e
MD
253 * freed.
254 */
bc49c323 255 if (was_online)
27b940e7
PB
256 rcu_thread_online();
257 else
258 cmm_smp_mb();
47d2f29e 259}
b39e1761 260#else /* !(CAA_BITS_PER_LONG < 64) */
9f1621ca
MD
261void synchronize_rcu(void)
262{
f0f7dbdd 263 unsigned long was_online;
ff2f67a0 264
bd252a04 265 was_online = URCU_TLS(rcu_reader).ctr;
ff2f67a0
MD
266
267 /*
268 * Mark the writer thread offline to make sure we don't wait for
5e77fc1f
PM
269 * our own quiescent state. This allows using synchronize_rcu()
270 * in threads registered as readers.
ff2f67a0 271 */
27b940e7
PB
272 if (was_online)
273 rcu_thread_offline();
274 else
275 cmm_smp_mb();
ff2f67a0 276
6abb4bd5 277 mutex_lock(&rcu_gp_lock);
16aa9ee8 278 if (cds_list_empty(&registry))
2dfb8b5e 279 goto out;
f6b42f9c
MD
280
281 /* Increment current G.P. */
282 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr + RCU_GP_CTR);
283
284 /*
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.
290 */
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 count of be quiescent.
302 */
303 wait_for_readers();
2dfb8b5e 304out:
6abb4bd5 305 mutex_unlock(&rcu_gp_lock);
ff2f67a0
MD
306
307 if (was_online)
27b940e7
PB
308 rcu_thread_online();
309 else
310 cmm_smp_mb();
9f1621ca 311}
b39e1761 312#endif /* !(CAA_BITS_PER_LONG < 64) */
9f1621ca
MD
313
314/*
315 * library wrappers to be used by non-LGPL compatible source code.
316 */
317
318void rcu_read_lock(void)
319{
320 _rcu_read_lock();
321}
322
323void rcu_read_unlock(void)
324{
325 _rcu_read_unlock();
326}
327
7ac06cef
MD
328void rcu_quiescent_state(void)
329{
330 _rcu_quiescent_state();
331}
332
333void rcu_thread_offline(void)
334{
335 _rcu_thread_offline();
336}
337
338void rcu_thread_online(void)
339{
340 _rcu_thread_online();
341}
342
9f1621ca
MD
343void rcu_register_thread(void)
344{
bd252a04
MD
345 URCU_TLS(rcu_reader).tid = pthread_self();
346 assert(URCU_TLS(rcu_reader).ctr == 0);
4f8e3380 347
6abb4bd5 348 mutex_lock(&rcu_gp_lock);
bd252a04 349 cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
6abb4bd5 350 mutex_unlock(&rcu_gp_lock);
5f373c84 351 _rcu_thread_online();
9f1621ca
MD
352}
353
354void rcu_unregister_thread(void)
355{
76f3022f
MD
356 /*
357 * We have to make the thread offline otherwise we end up dealocking
358 * with a waiting writer.
359 */
360 _rcu_thread_offline();
6abb4bd5 361 mutex_lock(&rcu_gp_lock);
bd252a04 362 cds_list_del(&URCU_TLS(rcu_reader).node);
6abb4bd5 363 mutex_unlock(&rcu_gp_lock);
9f1621ca 364}
f6d18c64
MD
365
366void rcu_exit(void)
367{
01cadde4
MD
368 /*
369 * Assertion disabled because call_rcu threads are now rcu
370 * readers, and left running at exit.
371 * assert(cds_list_empty(&registry));
372 */
f6d18c64 373}
5e77fc1f 374
5e6b23a6 375DEFINE_RCU_FLAVOR(rcu_flavor);
541d828d 376
5e77fc1f 377#include "urcu-call-rcu-impl.h"
0376e7b2 378#include "urcu-defer-impl.h"
This page took 0.078036 seconds and 4 git commands to generate.