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