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