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