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