Fix: deadlock when thread join is issued in read-side C.S.
[urcu.git] / urcu-qsbr.c
1 /*
2 * urcu-qsbr.c
3 *
4 * Userspace RCU QSBR library
5 *
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
26 #define _GNU_SOURCE
27 #define _LGPL_SOURCE
28 #include <stdio.h>
29 #include <pthread.h>
30 #include <signal.h>
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <stdint.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <poll.h>
37
38 #include "urcu/wfcqueue.h"
39 #include "urcu/map/urcu-qsbr.h"
40 #define BUILD_QSBR_LIB
41 #include "urcu/static/urcu-qsbr.h"
42 #include "urcu-pointer.h"
43 #include "urcu/tls-compat.h"
44
45 #include "urcu-die.h"
46 #include "urcu-wait.h"
47
48 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
49 #undef _LGPL_SOURCE
50 #include "urcu-qsbr.h"
51 #define _LGPL_SOURCE
52
53 void __attribute__((destructor)) rcu_exit(void);
54
55 /*
56 * rcu_gp_lock ensures mutual exclusion between threads calling
57 * synchronize_rcu().
58 */
59 static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
60 /*
61 * rcu_registry_lock ensures mutual exclusion between threads
62 * registering and unregistering themselves to/from the registry, and
63 * with threads reading that registry from synchronize_rcu(). However,
64 * this lock is not held all the way through the completion of awaiting
65 * for the grace period. It is sporadically released between iterations
66 * on the registry.
67 * rcu_registry_lock may nest inside rcu_gp_lock.
68 */
69 static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER;
70 struct rcu_gp rcu_gp = { .ctr = RCU_GP_ONLINE };
71
72 /*
73 * Active attempts to check for reader Q.S. before calling futex().
74 */
75 #define RCU_QS_ACTIVE_ATTEMPTS 100
76
77 /*
78 * Written to only by each individual reader. Read by both the reader and the
79 * writers.
80 */
81 __DEFINE_URCU_TLS_GLOBAL(struct rcu_reader, rcu_reader);
82
83 #ifdef DEBUG_YIELD
84 unsigned int rcu_yield_active;
85 __DEFINE_URCU_TLS_GLOBAL(unsigned int, rcu_rand_yield);
86 #endif
87
88 static CDS_LIST_HEAD(registry);
89
90 /*
91 * Queue keeping threads awaiting to wait for a grace period. Contains
92 * struct gp_waiters_thread objects.
93 */
94 static DEFINE_URCU_WAIT_QUEUE(gp_waiters);
95
96 static void mutex_lock(pthread_mutex_t *mutex)
97 {
98 int ret;
99
100 #ifndef DISTRUST_SIGNALS_EXTREME
101 ret = pthread_mutex_lock(mutex);
102 if (ret)
103 urcu_die(ret);
104 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
105 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
106 if (ret != EBUSY && ret != EINTR)
107 urcu_die(ret);
108 poll(NULL,0,10);
109 }
110 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
111 }
112
113 static void mutex_unlock(pthread_mutex_t *mutex)
114 {
115 int ret;
116
117 ret = pthread_mutex_unlock(mutex);
118 if (ret)
119 urcu_die(ret);
120 }
121
122 /*
123 * synchronize_rcu() waiting. Single thread.
124 */
125 static void wait_gp(void)
126 {
127 /* Read reader_gp before read futex */
128 cmm_smp_rmb();
129 if (uatomic_read(&rcu_gp.futex) == -1)
130 futex_noasync(&rcu_gp.futex, FUTEX_WAIT, -1,
131 NULL, NULL, 0);
132 }
133
134 /*
135 * Always called with rcu_registry lock held. Releases this lock between
136 * iterations and grabs it again. Holds the lock when it returns.
137 */
138 static void wait_for_readers(struct cds_list_head *input_readers,
139 struct cds_list_head *cur_snap_readers,
140 struct cds_list_head *qsreaders)
141 {
142 unsigned int wait_loops = 0;
143 struct rcu_reader *index, *tmp;
144
145 /*
146 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
147 * indicate quiescence (offline), or for them to observe the
148 * current rcu_gp.ctr value.
149 */
150 for (;;) {
151 if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS)
152 wait_loops++;
153 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
154 uatomic_set(&rcu_gp.futex, -1);
155 /*
156 * Write futex before write waiting (the other side
157 * reads them in the opposite order).
158 */
159 cmm_smp_wmb();
160 cds_list_for_each_entry(index, input_readers, node) {
161 _CMM_STORE_SHARED(index->waiting, 1);
162 }
163 /* Write futex before read reader_gp */
164 cmm_smp_mb();
165 }
166 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
167 switch (rcu_reader_state(&index->ctr)) {
168 case RCU_READER_ACTIVE_CURRENT:
169 if (cur_snap_readers) {
170 cds_list_move(&index->node,
171 cur_snap_readers);
172 break;
173 }
174 /* Fall-through */
175 case RCU_READER_INACTIVE:
176 cds_list_move(&index->node, qsreaders);
177 break;
178 case RCU_READER_ACTIVE_OLD:
179 /*
180 * Old snapshot. Leaving node in
181 * input_readers will make us busy-loop
182 * until the snapshot becomes current or
183 * the reader becomes inactive.
184 */
185 break;
186 }
187 }
188
189 if (cds_list_empty(input_readers)) {
190 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
191 /* Read reader_gp before write futex */
192 cmm_smp_mb();
193 uatomic_set(&rcu_gp.futex, 0);
194 }
195 break;
196 } else {
197 /* Temporarily unlock the registry lock. */
198 mutex_unlock(&rcu_registry_lock);
199 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
200 wait_gp();
201 } else {
202 #ifndef HAS_INCOHERENT_CACHES
203 caa_cpu_relax();
204 #else /* #ifndef HAS_INCOHERENT_CACHES */
205 cmm_smp_mb();
206 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
207 }
208 /* Re-lock the registry lock before the next loop. */
209 mutex_lock(&rcu_registry_lock);
210 }
211 }
212 }
213
214 /*
215 * Using a two-subphases algorithm for architectures with smaller than 64-bit
216 * long-size to ensure we do not encounter an overflow bug.
217 */
218
219 #if (CAA_BITS_PER_LONG < 64)
220 void synchronize_rcu(void)
221 {
222 CDS_LIST_HEAD(cur_snap_readers);
223 CDS_LIST_HEAD(qsreaders);
224 unsigned long was_online;
225 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
226 struct urcu_waiters waiters;
227
228 was_online = rcu_read_ongoing();
229
230 /* All threads should read qparity before accessing data structure
231 * where new ptr points to. In the "then" case, rcu_thread_offline
232 * includes a memory barrier.
233 *
234 * Mark the writer thread offline to make sure we don't wait for
235 * our own quiescent state. This allows using synchronize_rcu()
236 * in threads registered as readers.
237 */
238 if (was_online)
239 rcu_thread_offline();
240 else
241 cmm_smp_mb();
242
243 /*
244 * Add ourself to gp_waiters queue of threads awaiting to wait
245 * for a grace period. Proceed to perform the grace period only
246 * if we are the first thread added into the queue.
247 */
248 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
249 /* Not first in queue: will be awakened by another thread. */
250 urcu_adaptative_busy_wait(&wait);
251 goto gp_end;
252 }
253 /* We won't need to wake ourself up */
254 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
255
256 mutex_lock(&rcu_gp_lock);
257
258 /*
259 * Move all waiters into our local queue.
260 */
261 urcu_move_waiters(&waiters, &gp_waiters);
262
263 mutex_lock(&rcu_registry_lock);
264
265 if (cds_list_empty(&registry))
266 goto out;
267
268 /*
269 * Wait for readers to observe original parity or be quiescent.
270 * wait_for_readers() can release and grab again rcu_registry_lock
271 * interally.
272 */
273 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
274
275 /*
276 * Must finish waiting for quiescent state for original parity
277 * before committing next rcu_gp.ctr update to memory. Failure
278 * to do so could result in the writer waiting forever while new
279 * readers are always accessing data (no progress). Enforce
280 * compiler-order of load URCU_TLS(rcu_reader).ctr before store
281 * to rcu_gp.ctr.
282 */
283 cmm_barrier();
284
285 /*
286 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
287 * model easier to understand. It does not have a big performance impact
288 * anyway, given this is the write-side.
289 */
290 cmm_smp_mb();
291
292 /* Switch parity: 0 -> 1, 1 -> 0 */
293 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ RCU_GP_CTR);
294
295 /*
296 * Must commit rcu_gp.ctr update to memory before waiting for
297 * quiescent state. Failure to do so could result in the writer
298 * waiting forever while new readers are always accessing data
299 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
300 * before load URCU_TLS(rcu_reader).ctr.
301 */
302 cmm_barrier();
303
304 /*
305 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
306 * model easier to understand. It does not have a big performance impact
307 * anyway, given this is the write-side.
308 */
309 cmm_smp_mb();
310
311 /*
312 * Wait for readers to observe new parity or be quiescent.
313 * wait_for_readers() can release and grab again rcu_registry_lock
314 * interally.
315 */
316 wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
317
318 /*
319 * Put quiescent reader list back into registry.
320 */
321 cds_list_splice(&qsreaders, &registry);
322 out:
323 mutex_unlock(&rcu_registry_lock);
324 mutex_unlock(&rcu_gp_lock);
325 urcu_wake_all_waiters(&waiters);
326 gp_end:
327 /*
328 * Finish waiting for reader threads before letting the old ptr being
329 * freed.
330 */
331 if (was_online)
332 rcu_thread_online();
333 else
334 cmm_smp_mb();
335 }
336 #else /* !(CAA_BITS_PER_LONG < 64) */
337 void synchronize_rcu(void)
338 {
339 CDS_LIST_HEAD(qsreaders);
340 unsigned long was_online;
341 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
342 struct urcu_waiters waiters;
343
344 was_online = rcu_read_ongoing();
345
346 /*
347 * Mark the writer thread offline to make sure we don't wait for
348 * our own quiescent state. This allows using synchronize_rcu()
349 * in threads registered as readers.
350 */
351 if (was_online)
352 rcu_thread_offline();
353 else
354 cmm_smp_mb();
355
356 /*
357 * Add ourself to gp_waiters queue of threads awaiting to wait
358 * for a grace period. Proceed to perform the grace period only
359 * if we are the first thread added into the queue.
360 */
361 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
362 /* Not first in queue: will be awakened by another thread. */
363 urcu_adaptative_busy_wait(&wait);
364 goto gp_end;
365 }
366 /* We won't need to wake ourself up */
367 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
368
369 mutex_lock(&rcu_gp_lock);
370
371 /*
372 * Move all waiters into our local queue.
373 */
374 urcu_move_waiters(&waiters, &gp_waiters);
375
376 mutex_lock(&rcu_registry_lock);
377
378 if (cds_list_empty(&registry))
379 goto out;
380
381 /* Increment current G.P. */
382 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr + RCU_GP_CTR);
383
384 /*
385 * Must commit rcu_gp.ctr update to memory before waiting for
386 * quiescent state. Failure to do so could result in the writer
387 * waiting forever while new readers are always accessing data
388 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
389 * before load URCU_TLS(rcu_reader).ctr.
390 */
391 cmm_barrier();
392
393 /*
394 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
395 * model easier to understand. It does not have a big performance impact
396 * anyway, given this is the write-side.
397 */
398 cmm_smp_mb();
399
400 /*
401 * Wait for readers to observe new count of be quiescent.
402 * wait_for_readers() can release and grab again rcu_registry_lock
403 * interally.
404 */
405 wait_for_readers(&registry, NULL, &qsreaders);
406
407 /*
408 * Put quiescent reader list back into registry.
409 */
410 cds_list_splice(&qsreaders, &registry);
411 out:
412 mutex_unlock(&rcu_registry_lock);
413 mutex_unlock(&rcu_gp_lock);
414 urcu_wake_all_waiters(&waiters);
415 gp_end:
416 if (was_online)
417 rcu_thread_online();
418 else
419 cmm_smp_mb();
420 }
421 #endif /* !(CAA_BITS_PER_LONG < 64) */
422
423 /*
424 * library wrappers to be used by non-LGPL compatible source code.
425 */
426
427 void rcu_read_lock(void)
428 {
429 _rcu_read_lock();
430 }
431
432 void rcu_read_unlock(void)
433 {
434 _rcu_read_unlock();
435 }
436
437 int rcu_read_ongoing(void)
438 {
439 return _rcu_read_ongoing();
440 }
441
442 void rcu_quiescent_state(void)
443 {
444 _rcu_quiescent_state();
445 }
446
447 void rcu_thread_offline(void)
448 {
449 _rcu_thread_offline();
450 }
451
452 void rcu_thread_online(void)
453 {
454 _rcu_thread_online();
455 }
456
457 void rcu_register_thread(void)
458 {
459 URCU_TLS(rcu_reader).tid = pthread_self();
460 assert(URCU_TLS(rcu_reader).ctr == 0);
461
462 mutex_lock(&rcu_registry_lock);
463 cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
464 mutex_unlock(&rcu_registry_lock);
465 _rcu_thread_online();
466 }
467
468 void rcu_unregister_thread(void)
469 {
470 /*
471 * We have to make the thread offline otherwise we end up dealocking
472 * with a waiting writer.
473 */
474 _rcu_thread_offline();
475 mutex_lock(&rcu_registry_lock);
476 cds_list_del(&URCU_TLS(rcu_reader).node);
477 mutex_unlock(&rcu_registry_lock);
478 }
479
480 void rcu_exit(void)
481 {
482 /*
483 * Assertion disabled because call_rcu threads are now rcu
484 * readers, and left running at exit.
485 * assert(cds_list_empty(&registry));
486 */
487 }
488
489 DEFINE_RCU_FLAVOR(rcu_flavor);
490
491 #include "urcu-call-rcu-impl.h"
492 #include "urcu-defer-impl.h"
This page took 0.041044 seconds and 4 git commands to generate.