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