Version 0.8.11
[userspace-rcu.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 return;
131 while (futex_noasync(&rcu_gp.futex, FUTEX_WAIT, -1,
132 NULL, NULL, 0)) {
133 switch (errno) {
134 case EWOULDBLOCK:
135 /* Value already changed. */
136 return;
137 case EINTR:
138 /* Retry if interrupted by signal. */
139 break; /* Get out of switch. */
140 default:
141 /* Unexpected error. */
142 urcu_die(errno);
143 }
144 }
145 }
146
147 /*
148 * Always called with rcu_registry lock held. Releases this lock between
149 * iterations and grabs it again. Holds the lock when it returns.
150 */
151 static void wait_for_readers(struct cds_list_head *input_readers,
152 struct cds_list_head *cur_snap_readers,
153 struct cds_list_head *qsreaders)
154 {
155 unsigned int wait_loops = 0;
156 struct rcu_reader *index, *tmp;
157
158 /*
159 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
160 * indicate quiescence (offline), or for them to observe the
161 * current rcu_gp.ctr value.
162 */
163 for (;;) {
164 if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS)
165 wait_loops++;
166 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
167 uatomic_set(&rcu_gp.futex, -1);
168 /*
169 * Write futex before write waiting (the other side
170 * reads them in the opposite order).
171 */
172 cmm_smp_wmb();
173 cds_list_for_each_entry(index, input_readers, node) {
174 _CMM_STORE_SHARED(index->waiting, 1);
175 }
176 /* Write futex before read reader_gp */
177 cmm_smp_mb();
178 }
179 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
180 switch (rcu_reader_state(&index->ctr)) {
181 case RCU_READER_ACTIVE_CURRENT:
182 if (cur_snap_readers) {
183 cds_list_move(&index->node,
184 cur_snap_readers);
185 break;
186 }
187 /* Fall-through */
188 case RCU_READER_INACTIVE:
189 cds_list_move(&index->node, qsreaders);
190 break;
191 case RCU_READER_ACTIVE_OLD:
192 /*
193 * Old snapshot. Leaving node in
194 * input_readers will make us busy-loop
195 * until the snapshot becomes current or
196 * the reader becomes inactive.
197 */
198 break;
199 }
200 }
201
202 if (cds_list_empty(input_readers)) {
203 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
204 /* Read reader_gp before write futex */
205 cmm_smp_mb();
206 uatomic_set(&rcu_gp.futex, 0);
207 }
208 break;
209 } else {
210 /* Temporarily unlock the registry lock. */
211 mutex_unlock(&rcu_registry_lock);
212 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
213 wait_gp();
214 } else {
215 #ifndef HAS_INCOHERENT_CACHES
216 caa_cpu_relax();
217 #else /* #ifndef HAS_INCOHERENT_CACHES */
218 cmm_smp_mb();
219 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
220 }
221 /* Re-lock the registry lock before the next loop. */
222 mutex_lock(&rcu_registry_lock);
223 }
224 }
225 }
226
227 /*
228 * Using a two-subphases algorithm for architectures with smaller than 64-bit
229 * long-size to ensure we do not encounter an overflow bug.
230 */
231
232 #if (CAA_BITS_PER_LONG < 64)
233 void synchronize_rcu(void)
234 {
235 CDS_LIST_HEAD(cur_snap_readers);
236 CDS_LIST_HEAD(qsreaders);
237 unsigned long was_online;
238 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
239 struct urcu_waiters waiters;
240
241 was_online = rcu_read_ongoing();
242
243 /* All threads should read qparity before accessing data structure
244 * where new ptr points to. In the "then" case, rcu_thread_offline
245 * includes a memory barrier.
246 *
247 * Mark the writer thread offline to make sure we don't wait for
248 * our own quiescent state. This allows using synchronize_rcu()
249 * in threads registered as readers.
250 */
251 if (was_online)
252 rcu_thread_offline();
253 else
254 cmm_smp_mb();
255
256 /*
257 * Add ourself to gp_waiters queue of threads awaiting to wait
258 * for a grace period. Proceed to perform the grace period only
259 * if we are the first thread added into the queue.
260 */
261 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
262 /* Not first in queue: will be awakened by another thread. */
263 urcu_adaptative_busy_wait(&wait);
264 goto gp_end;
265 }
266 /* We won't need to wake ourself up */
267 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
268
269 mutex_lock(&rcu_gp_lock);
270
271 /*
272 * Move all waiters into our local queue.
273 */
274 urcu_move_waiters(&waiters, &gp_waiters);
275
276 mutex_lock(&rcu_registry_lock);
277
278 if (cds_list_empty(&registry))
279 goto out;
280
281 /*
282 * Wait for readers to observe original parity or be quiescent.
283 * wait_for_readers() can release and grab again rcu_registry_lock
284 * interally.
285 */
286 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
287
288 /*
289 * Must finish waiting for quiescent state for original parity
290 * before committing next rcu_gp.ctr update to memory. Failure
291 * to do so could result in the writer waiting forever while new
292 * readers are always accessing data (no progress). Enforce
293 * compiler-order of load URCU_TLS(rcu_reader).ctr before store
294 * to rcu_gp.ctr.
295 */
296 cmm_barrier();
297
298 /*
299 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
300 * model easier to understand. It does not have a big performance impact
301 * anyway, given this is the write-side.
302 */
303 cmm_smp_mb();
304
305 /* Switch parity: 0 -> 1, 1 -> 0 */
306 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ RCU_GP_CTR);
307
308 /*
309 * Must commit rcu_gp.ctr update to memory before waiting for
310 * quiescent state. Failure to do so could result in the writer
311 * waiting forever while new readers are always accessing data
312 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
313 * before load URCU_TLS(rcu_reader).ctr.
314 */
315 cmm_barrier();
316
317 /*
318 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
319 * model easier to understand. It does not have a big performance impact
320 * anyway, given this is the write-side.
321 */
322 cmm_smp_mb();
323
324 /*
325 * Wait for readers to observe new parity or be quiescent.
326 * wait_for_readers() can release and grab again rcu_registry_lock
327 * interally.
328 */
329 wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
330
331 /*
332 * Put quiescent reader list back into registry.
333 */
334 cds_list_splice(&qsreaders, &registry);
335 out:
336 mutex_unlock(&rcu_registry_lock);
337 mutex_unlock(&rcu_gp_lock);
338 urcu_wake_all_waiters(&waiters);
339 gp_end:
340 /*
341 * Finish waiting for reader threads before letting the old ptr being
342 * freed.
343 */
344 if (was_online)
345 rcu_thread_online();
346 else
347 cmm_smp_mb();
348 }
349 #else /* !(CAA_BITS_PER_LONG < 64) */
350 void synchronize_rcu(void)
351 {
352 CDS_LIST_HEAD(qsreaders);
353 unsigned long was_online;
354 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
355 struct urcu_waiters waiters;
356
357 was_online = rcu_read_ongoing();
358
359 /*
360 * Mark the writer thread offline to make sure we don't wait for
361 * our own quiescent state. This allows using synchronize_rcu()
362 * in threads registered as readers.
363 */
364 if (was_online)
365 rcu_thread_offline();
366 else
367 cmm_smp_mb();
368
369 /*
370 * Add ourself to gp_waiters queue of threads awaiting to wait
371 * for a grace period. Proceed to perform the grace period only
372 * if we are the first thread added into the queue.
373 */
374 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
375 /* Not first in queue: will be awakened by another thread. */
376 urcu_adaptative_busy_wait(&wait);
377 goto gp_end;
378 }
379 /* We won't need to wake ourself up */
380 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
381
382 mutex_lock(&rcu_gp_lock);
383
384 /*
385 * Move all waiters into our local queue.
386 */
387 urcu_move_waiters(&waiters, &gp_waiters);
388
389 mutex_lock(&rcu_registry_lock);
390
391 if (cds_list_empty(&registry))
392 goto out;
393
394 /* Increment current G.P. */
395 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr + RCU_GP_CTR);
396
397 /*
398 * Must commit rcu_gp.ctr update to memory before waiting for
399 * quiescent state. Failure to do so could result in the writer
400 * waiting forever while new readers are always accessing data
401 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
402 * before load URCU_TLS(rcu_reader).ctr.
403 */
404 cmm_barrier();
405
406 /*
407 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
408 * model easier to understand. It does not have a big performance impact
409 * anyway, given this is the write-side.
410 */
411 cmm_smp_mb();
412
413 /*
414 * Wait for readers to observe new count of be quiescent.
415 * wait_for_readers() can release and grab again rcu_registry_lock
416 * interally.
417 */
418 wait_for_readers(&registry, NULL, &qsreaders);
419
420 /*
421 * Put quiescent reader list back into registry.
422 */
423 cds_list_splice(&qsreaders, &registry);
424 out:
425 mutex_unlock(&rcu_registry_lock);
426 mutex_unlock(&rcu_gp_lock);
427 urcu_wake_all_waiters(&waiters);
428 gp_end:
429 if (was_online)
430 rcu_thread_online();
431 else
432 cmm_smp_mb();
433 }
434 #endif /* !(CAA_BITS_PER_LONG < 64) */
435
436 /*
437 * library wrappers to be used by non-LGPL compatible source code.
438 */
439
440 void rcu_read_lock(void)
441 {
442 _rcu_read_lock();
443 }
444
445 void rcu_read_unlock(void)
446 {
447 _rcu_read_unlock();
448 }
449
450 int rcu_read_ongoing(void)
451 {
452 return _rcu_read_ongoing();
453 }
454
455 void rcu_quiescent_state(void)
456 {
457 _rcu_quiescent_state();
458 }
459
460 void rcu_thread_offline(void)
461 {
462 _rcu_thread_offline();
463 }
464
465 void rcu_thread_online(void)
466 {
467 _rcu_thread_online();
468 }
469
470 void rcu_register_thread(void)
471 {
472 URCU_TLS(rcu_reader).tid = pthread_self();
473 assert(URCU_TLS(rcu_reader).ctr == 0);
474
475 mutex_lock(&rcu_registry_lock);
476 cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
477 mutex_unlock(&rcu_registry_lock);
478 _rcu_thread_online();
479 }
480
481 void rcu_unregister_thread(void)
482 {
483 /*
484 * We have to make the thread offline otherwise we end up dealocking
485 * with a waiting writer.
486 */
487 _rcu_thread_offline();
488 mutex_lock(&rcu_registry_lock);
489 cds_list_del(&URCU_TLS(rcu_reader).node);
490 mutex_unlock(&rcu_registry_lock);
491 }
492
493 void rcu_exit(void)
494 {
495 /*
496 * Assertion disabled because call_rcu threads are now rcu
497 * readers, and left running at exit.
498 * assert(cds_list_empty(&registry));
499 */
500 }
501
502 DEFINE_RCU_FLAVOR(rcu_flavor);
503
504 #include "urcu-call-rcu-impl.h"
505 #include "urcu-defer-impl.h"
This page took 0.038088 seconds and 4 git commands to generate.