Documentation: rculfhash: cds_lfht_resize not within read-side C.S.
[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 int rcu_read_ongoing(void)
433 {
434 return _rcu_read_ongoing();
435 }
436
437 void rcu_register_thread(void)
438 {
439 URCU_TLS(rcu_reader).tid = pthread_self();
440 assert(URCU_TLS(rcu_reader).need_mb == 0);
441 assert(!(URCU_TLS(rcu_reader).ctr & RCU_GP_CTR_NEST_MASK));
442
443 mutex_lock(&rcu_gp_lock);
444 rcu_init(); /* In case gcc does not support constructor attribute */
445 cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
446 mutex_unlock(&rcu_gp_lock);
447 }
448
449 void rcu_unregister_thread(void)
450 {
451 mutex_lock(&rcu_gp_lock);
452 cds_list_del(&URCU_TLS(rcu_reader).node);
453 mutex_unlock(&rcu_gp_lock);
454 }
455
456 #ifdef RCU_MEMBARRIER
457 void rcu_init(void)
458 {
459 if (init_done)
460 return;
461 init_done = 1;
462 if (!membarrier(MEMBARRIER_EXPEDITED | MEMBARRIER_QUERY))
463 rcu_has_sys_membarrier = 1;
464 }
465 #endif
466
467 #ifdef RCU_SIGNAL
468 static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context)
469 {
470 /*
471 * Executing this cmm_smp_mb() is the only purpose of this signal handler.
472 * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
473 * executed on.
474 */
475 cmm_smp_mb();
476 _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
477 cmm_smp_mb();
478 }
479
480 /*
481 * rcu_init constructor. Called when the library is linked, but also when
482 * reader threads are calling rcu_register_thread().
483 * Should only be called by a single thread at a given time. This is ensured by
484 * holing the rcu_gp_lock from rcu_register_thread() or by running at library
485 * load time, which should not be executed by multiple threads nor concurrently
486 * with rcu_register_thread() anyway.
487 */
488 void rcu_init(void)
489 {
490 struct sigaction act;
491 int ret;
492
493 if (init_done)
494 return;
495 init_done = 1;
496
497 act.sa_sigaction = sigrcu_handler;
498 act.sa_flags = SA_SIGINFO | SA_RESTART;
499 sigemptyset(&act.sa_mask);
500 ret = sigaction(SIGRCU, &act, NULL);
501 if (ret)
502 urcu_die(errno);
503 }
504
505 void rcu_exit(void)
506 {
507 struct sigaction act;
508 int ret;
509
510 ret = sigaction(SIGRCU, NULL, &act);
511 if (ret)
512 urcu_die(errno);
513 assert(act.sa_sigaction == sigrcu_handler);
514 assert(cds_list_empty(&registry));
515 }
516
517 #endif /* #ifdef RCU_SIGNAL */
518
519 DEFINE_RCU_FLAVOR(rcu_flavor);
520
521 #include "urcu-call-rcu-impl.h"
522 #include "urcu-defer-impl.h"
This page took 0.039407 seconds and 4 git commands to generate.