urcu-mb/signal/membarrier: batch concurrent synchronize_rcu()
[urcu.git] / urcu.c
1 /*
2 * urcu.c
3 *
4 * Userspace RCU 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 _BSD_SOURCE
27 #define _GNU_SOURCE
28 #define _LGPL_SOURCE
29 #include <stdio.h>
30 #include <pthread.h>
31 #include <signal.h>
32 #include <assert.h>
33 #include <stdlib.h>
34 #include <stdint.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <poll.h>
38
39 #include "urcu/wfcqueue.h"
40 #include "urcu/map/urcu.h"
41 #include "urcu/static/urcu.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.h"
51 #define _LGPL_SOURCE
52
53 /*
54 * If a reader is really non-cooperative and refuses to commit its
55 * rcu_active_readers count to memory (there is no barrier in the reader
56 * per-se), kick it after a few loops waiting for it.
57 */
58 #define KICK_READER_LOOPS 10000
59
60 /*
61 * Active attempts to check for reader Q.S. before calling futex().
62 */
63 #define RCU_QS_ACTIVE_ATTEMPTS 100
64
65 #ifdef RCU_MEMBARRIER
66 static int init_done;
67 int rcu_has_sys_membarrier;
68
69 void __attribute__((constructor)) rcu_init(void);
70 #endif
71
72 #ifdef RCU_MB
73 void rcu_init(void)
74 {
75 }
76 #endif
77
78 #ifdef RCU_SIGNAL
79 static int init_done;
80
81 void __attribute__((constructor)) rcu_init(void);
82 void __attribute__((destructor)) rcu_exit(void);
83 #endif
84
85 static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
86
87 int32_t rcu_gp_futex;
88
89 /*
90 * Global grace period counter.
91 * Contains the current RCU_GP_CTR_PHASE.
92 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
93 * Written to only by writer with mutex taken. Read by both writer and readers.
94 */
95 unsigned long rcu_gp_ctr = RCU_GP_COUNT;
96
97 /*
98 * Written to only by each individual reader. Read by both the reader and the
99 * writers.
100 */
101 DEFINE_URCU_TLS(struct rcu_reader, rcu_reader);
102
103 #ifdef DEBUG_YIELD
104 unsigned int rcu_yield_active;
105 DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
106 #endif
107
108 static CDS_LIST_HEAD(registry);
109
110 /*
111 * Queue keeping threads awaiting to wait for a grace period. Contains
112 * struct gp_waiters_thread objects.
113 */
114 static DEFINE_URCU_WAIT_QUEUE(gp_waiters);
115
116 static void mutex_lock(pthread_mutex_t *mutex)
117 {
118 int ret;
119
120 #ifndef DISTRUST_SIGNALS_EXTREME
121 ret = pthread_mutex_lock(mutex);
122 if (ret)
123 urcu_die(ret);
124 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
125 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
126 if (ret != EBUSY && ret != EINTR)
127 urcu_die(ret);
128 if (CMM_LOAD_SHARED(URCU_TLS(rcu_reader).need_mb)) {
129 cmm_smp_mb();
130 _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
131 cmm_smp_mb();
132 }
133 poll(NULL,0,10);
134 }
135 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
136 }
137
138 static void mutex_unlock(pthread_mutex_t *mutex)
139 {
140 int ret;
141
142 ret = pthread_mutex_unlock(mutex);
143 if (ret)
144 urcu_die(ret);
145 }
146
147 #ifdef RCU_MEMBARRIER
148 static void smp_mb_master(int group)
149 {
150 if (caa_likely(rcu_has_sys_membarrier))
151 membarrier(MEMBARRIER_EXPEDITED);
152 else
153 cmm_smp_mb();
154 }
155 #endif
156
157 #ifdef RCU_MB
158 static void smp_mb_master(int group)
159 {
160 cmm_smp_mb();
161 }
162 #endif
163
164 #ifdef RCU_SIGNAL
165 static void force_mb_all_readers(void)
166 {
167 struct rcu_reader *index;
168
169 /*
170 * Ask for each threads to execute a cmm_smp_mb() so we can consider the
171 * compiler barriers around rcu read lock as real memory barriers.
172 */
173 if (cds_list_empty(&registry))
174 return;
175 /*
176 * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs
177 * a cache flush on architectures with non-coherent cache. Let's play
178 * safe and don't assume anything : we use cmm_smp_mc() to make sure the
179 * cache flush is enforced.
180 */
181 cds_list_for_each_entry(index, &registry, node) {
182 CMM_STORE_SHARED(index->need_mb, 1);
183 pthread_kill(index->tid, SIGRCU);
184 }
185 /*
186 * Wait for sighandler (and thus mb()) to execute on every thread.
187 *
188 * Note that the pthread_kill() will never be executed on systems
189 * that correctly deliver signals in a timely manner. However, it
190 * is not uncommon for kernels to have bugs that can result in
191 * lost or unduly delayed signals.
192 *
193 * If you are seeing the below pthread_kill() executing much at
194 * all, we suggest testing the underlying kernel and filing the
195 * relevant bug report. For Linux kernels, we recommend getting
196 * the Linux Test Project (LTP).
197 */
198 cds_list_for_each_entry(index, &registry, node) {
199 while (CMM_LOAD_SHARED(index->need_mb)) {
200 pthread_kill(index->tid, SIGRCU);
201 poll(NULL, 0, 1);
202 }
203 }
204 cmm_smp_mb(); /* read ->need_mb before ending the barrier */
205 }
206
207 static void smp_mb_master(int group)
208 {
209 force_mb_all_readers();
210 }
211 #endif /* #ifdef RCU_SIGNAL */
212
213 /*
214 * synchronize_rcu() waiting. Single thread.
215 */
216 static void wait_gp(void)
217 {
218 /* Read reader_gp before read futex */
219 smp_mb_master(RCU_MB_GROUP);
220 if (uatomic_read(&rcu_gp_futex) == -1)
221 futex_async(&rcu_gp_futex, FUTEX_WAIT, -1,
222 NULL, NULL, 0);
223 }
224
225 static void wait_for_readers(struct cds_list_head *input_readers,
226 struct cds_list_head *cur_snap_readers,
227 struct cds_list_head *qsreaders)
228 {
229 int wait_loops = 0;
230 struct rcu_reader *index, *tmp;
231
232 /*
233 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
234 * indicate quiescence (not nested), or observe the current
235 * rcu_gp_ctr value.
236 */
237 for (;;) {
238 wait_loops++;
239 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
240 uatomic_dec(&rcu_gp_futex);
241 /* Write futex before read reader_gp */
242 smp_mb_master(RCU_MB_GROUP);
243 }
244
245 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
246 switch (rcu_reader_state(&index->ctr)) {
247 case RCU_READER_ACTIVE_CURRENT:
248 if (cur_snap_readers) {
249 cds_list_move(&index->node,
250 cur_snap_readers);
251 break;
252 }
253 /* Fall-through */
254 case RCU_READER_INACTIVE:
255 cds_list_move(&index->node, qsreaders);
256 break;
257 case RCU_READER_ACTIVE_OLD:
258 /*
259 * Old snapshot. Leaving node in
260 * input_readers will make us busy-loop
261 * until the snapshot becomes current or
262 * the reader becomes inactive.
263 */
264 break;
265 }
266 }
267
268 #ifndef HAS_INCOHERENT_CACHES
269 if (cds_list_empty(input_readers)) {
270 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
271 /* Read reader_gp before write futex */
272 smp_mb_master(RCU_MB_GROUP);
273 uatomic_set(&rcu_gp_futex, 0);
274 }
275 break;
276 } else {
277 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS)
278 wait_gp();
279 else
280 caa_cpu_relax();
281 }
282 #else /* #ifndef HAS_INCOHERENT_CACHES */
283 /*
284 * BUSY-LOOP. Force the reader thread to commit its
285 * URCU_TLS(rcu_reader).ctr update to memory if we wait
286 * for too long.
287 */
288 if (cds_list_empty(input_readers)) {
289 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
290 /* Read reader_gp before write futex */
291 smp_mb_master(RCU_MB_GROUP);
292 uatomic_set(&rcu_gp_futex, 0);
293 }
294 break;
295 } else {
296 switch (wait_loops) {
297 case RCU_QS_ACTIVE_ATTEMPTS:
298 wait_gp();
299 break; /* only escape switch */
300 case KICK_READER_LOOPS:
301 smp_mb_master(RCU_MB_GROUP);
302 wait_loops = 0;
303 break; /* only escape switch */
304 default:
305 caa_cpu_relax();
306 }
307 }
308 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
309 }
310 }
311
312 void synchronize_rcu(void)
313 {
314 CDS_LIST_HEAD(cur_snap_readers);
315 CDS_LIST_HEAD(qsreaders);
316 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
317 struct urcu_waiters waiters;
318
319 /*
320 * Add ourself to gp_waiters queue of threads awaiting to wait
321 * for a grace period. Proceed to perform the grace period only
322 * if we are the first thread added into the queue.
323 * The implicit memory barrier before urcu_wait_add()
324 * orders prior memory accesses of threads put into the wait
325 * queue before their insertion into the wait queue.
326 */
327 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
328 /* Not first in queue: will be awakened by another thread. */
329 urcu_adaptative_busy_wait(&wait);
330 /* Order following memory accesses after grace period. */
331 cmm_smp_mb();
332 return;
333 }
334 /* We won't need to wake ourself up */
335 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
336
337 mutex_lock(&rcu_gp_lock);
338
339 /*
340 * Move all waiters into our local queue.
341 */
342 urcu_move_waiters(&waiters, &gp_waiters);
343
344 if (cds_list_empty(&registry))
345 goto out;
346
347 /* All threads should read qparity before accessing data structure
348 * where new ptr points to. Must be done within rcu_gp_lock because it
349 * iterates on reader threads.*/
350 /* Write new ptr before changing the qparity */
351 smp_mb_master(RCU_MB_GROUP);
352
353 /*
354 * Wait for readers to observe original parity or be quiescent.
355 */
356 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
357
358 /*
359 * Must finish waiting for quiescent state for original parity before
360 * committing next rcu_gp_ctr update to memory. Failure to do so could
361 * result in the writer waiting forever while new readers are always
362 * accessing data (no progress). Enforce compiler-order of load
363 * URCU_TLS(rcu_reader).ctr before store to rcu_gp_ctr.
364 */
365 cmm_barrier();
366
367 /*
368 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
369 * model easier to understand. It does not have a big performance impact
370 * anyway, given this is the write-side.
371 */
372 cmm_smp_mb();
373
374 /* Switch parity: 0 -> 1, 1 -> 0 */
375 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE);
376
377 /*
378 * Must commit rcu_gp_ctr update to memory before waiting for quiescent
379 * state. Failure to do so could result in the writer waiting forever
380 * while new readers are always accessing data (no progress). Enforce
381 * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr.
382 */
383 cmm_barrier();
384
385 /*
386 *
387 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
388 * model easier to understand. It does not have a big performance impact
389 * anyway, given this is the write-side.
390 */
391 cmm_smp_mb();
392
393 /*
394 * Wait for readers to observe new parity or be quiescent.
395 */
396 wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
397
398 /*
399 * Put quiescent reader list back into registry.
400 */
401 cds_list_splice(&qsreaders, &registry);
402
403 /* Finish waiting for reader threads before letting the old ptr being
404 * freed. Must be done within rcu_gp_lock because it iterates on reader
405 * threads. */
406 smp_mb_master(RCU_MB_GROUP);
407 out:
408 mutex_unlock(&rcu_gp_lock);
409
410 /*
411 * Wakeup waiters only after we have completed the grace period
412 * and have ensured the memory barriers at the end of the grace
413 * period have been issued.
414 */
415 urcu_wake_all_waiters(&waiters);
416 }
417
418 /*
419 * library wrappers to be used by non-LGPL compatible source code.
420 */
421
422 void rcu_read_lock(void)
423 {
424 _rcu_read_lock();
425 }
426
427 void rcu_read_unlock(void)
428 {
429 _rcu_read_unlock();
430 }
431
432 void rcu_register_thread(void)
433 {
434 URCU_TLS(rcu_reader).tid = pthread_self();
435 assert(URCU_TLS(rcu_reader).need_mb == 0);
436 assert(!(URCU_TLS(rcu_reader).ctr & RCU_GP_CTR_NEST_MASK));
437
438 mutex_lock(&rcu_gp_lock);
439 rcu_init(); /* In case gcc does not support constructor attribute */
440 cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
441 mutex_unlock(&rcu_gp_lock);
442 }
443
444 void rcu_unregister_thread(void)
445 {
446 mutex_lock(&rcu_gp_lock);
447 cds_list_del(&URCU_TLS(rcu_reader).node);
448 mutex_unlock(&rcu_gp_lock);
449 }
450
451 #ifdef RCU_MEMBARRIER
452 void rcu_init(void)
453 {
454 if (init_done)
455 return;
456 init_done = 1;
457 if (!membarrier(MEMBARRIER_EXPEDITED | MEMBARRIER_QUERY))
458 rcu_has_sys_membarrier = 1;
459 }
460 #endif
461
462 #ifdef RCU_SIGNAL
463 static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context)
464 {
465 /*
466 * Executing this cmm_smp_mb() is the only purpose of this signal handler.
467 * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
468 * executed on.
469 */
470 cmm_smp_mb();
471 _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
472 cmm_smp_mb();
473 }
474
475 /*
476 * rcu_init constructor. Called when the library is linked, but also when
477 * reader threads are calling rcu_register_thread().
478 * Should only be called by a single thread at a given time. This is ensured by
479 * holing the rcu_gp_lock from rcu_register_thread() or by running at library
480 * load time, which should not be executed by multiple threads nor concurrently
481 * with rcu_register_thread() anyway.
482 */
483 void rcu_init(void)
484 {
485 struct sigaction act;
486 int ret;
487
488 if (init_done)
489 return;
490 init_done = 1;
491
492 act.sa_sigaction = sigrcu_handler;
493 act.sa_flags = SA_SIGINFO | SA_RESTART;
494 sigemptyset(&act.sa_mask);
495 ret = sigaction(SIGRCU, &act, NULL);
496 if (ret)
497 urcu_die(errno);
498 }
499
500 void rcu_exit(void)
501 {
502 struct sigaction act;
503 int ret;
504
505 ret = sigaction(SIGRCU, NULL, &act);
506 if (ret)
507 urcu_die(errno);
508 assert(act.sa_sigaction == sigrcu_handler);
509 assert(cds_list_empty(&registry));
510 }
511
512 #endif /* #ifdef RCU_SIGNAL */
513
514 DEFINE_RCU_FLAVOR(rcu_flavor);
515
516 #include "urcu-call-rcu-impl.h"
517 #include "urcu-defer-impl.h"
This page took 0.039378 seconds and 5 git commands to generate.