doc/examples: document call_rcu()
[userspace-rcu.git] / urcu-bp.c
... / ...
CommitLineData
1/*
2 * urcu-bp.c
3 *
4 * Userspace RCU library, "bulletproof" version.
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 <string.h>
34#include <errno.h>
35#include <poll.h>
36#include <unistd.h>
37#include <sys/mman.h>
38
39#include "urcu/wfcqueue.h"
40#include "urcu/map/urcu-bp.h"
41#include "urcu/static/urcu-bp.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-bp.h"
50#define _LGPL_SOURCE
51
52#ifndef MAP_ANONYMOUS
53#define MAP_ANONYMOUS MAP_ANON
54#endif
55
56#ifdef __linux__
57static
58void *mremap_wrapper(void *old_address, size_t old_size,
59 size_t new_size, int flags)
60{
61 return mremap(old_address, old_size, new_size, flags);
62}
63#else
64
65#define MREMAP_MAYMOVE 1
66#define MREMAP_FIXED 2
67
68/*
69 * mremap wrapper for non-Linux systems. Maps a RW, anonymous private mapping.
70 * This is not generic.
71*/
72static
73void *mremap_wrapper(void *old_address, size_t old_size,
74 size_t new_size, int flags)
75{
76 void *new_address;
77
78 assert(flags & MREMAP_MAYMOVE);
79 assert(!(flags & MREMAP_FIXED));
80 new_address = mmap(old_address, new_size,
81 PROT_READ | PROT_WRITE,
82 MAP_ANONYMOUS | MAP_PRIVATE,
83 -1, 0);
84 if (new_address == MAP_FAILED)
85 return MAP_FAILED;
86 if (old_address) {
87 memcpy(new_address, old_address, old_size);
88 munmap(old_address, old_size);
89 }
90 return new_address;
91}
92#endif
93
94/* Sleep delay in us */
95#define RCU_SLEEP_DELAY 1000
96#define ARENA_INIT_ALLOC 16
97
98/*
99 * Active attempts to check for reader Q.S. before calling sleep().
100 */
101#define RCU_QS_ACTIVE_ATTEMPTS 100
102
103void __attribute__((destructor)) rcu_bp_exit(void);
104
105static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
106
107#ifdef DEBUG_YIELD
108unsigned int rcu_yield_active;
109DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
110#endif
111
112struct rcu_gp rcu_gp = { .ctr = RCU_GP_COUNT };
113
114/*
115 * Pointer to registry elements. Written to only by each individual reader. Read
116 * by both the reader and the writers.
117 */
118DEFINE_URCU_TLS(struct rcu_reader *, rcu_reader);
119
120static CDS_LIST_HEAD(registry);
121
122struct registry_arena {
123 void *p;
124 size_t len;
125 size_t used;
126};
127
128static struct registry_arena registry_arena;
129
130/* Saved fork signal mask, protected by rcu_gp_lock */
131static sigset_t saved_fork_signal_mask;
132
133static void rcu_gc_registry(void);
134
135static void mutex_lock(pthread_mutex_t *mutex)
136{
137 int ret;
138
139#ifndef DISTRUST_SIGNALS_EXTREME
140 ret = pthread_mutex_lock(mutex);
141 if (ret)
142 urcu_die(ret);
143#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
144 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
145 if (ret != EBUSY && ret != EINTR)
146 urcu_die(ret);
147 poll(NULL,0,10);
148 }
149#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
150}
151
152static void mutex_unlock(pthread_mutex_t *mutex)
153{
154 int ret;
155
156 ret = pthread_mutex_unlock(mutex);
157 if (ret)
158 urcu_die(ret);
159}
160
161static void wait_for_readers(struct cds_list_head *input_readers,
162 struct cds_list_head *cur_snap_readers,
163 struct cds_list_head *qsreaders)
164{
165 int wait_loops = 0;
166 struct rcu_reader *index, *tmp;
167
168 /*
169 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
170 * indicate quiescence (not nested), or observe the current
171 * rcu_gp.ctr value.
172 */
173 for (;;) {
174 wait_loops++;
175 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
176 switch (rcu_reader_state(&index->ctr)) {
177 case RCU_READER_ACTIVE_CURRENT:
178 if (cur_snap_readers) {
179 cds_list_move(&index->node,
180 cur_snap_readers);
181 break;
182 }
183 /* Fall-through */
184 case RCU_READER_INACTIVE:
185 cds_list_move(&index->node, qsreaders);
186 break;
187 case RCU_READER_ACTIVE_OLD:
188 /*
189 * Old snapshot. Leaving node in
190 * input_readers will make us busy-loop
191 * until the snapshot becomes current or
192 * the reader becomes inactive.
193 */
194 break;
195 }
196 }
197
198 if (cds_list_empty(input_readers)) {
199 break;
200 } else {
201 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS)
202 usleep(RCU_SLEEP_DELAY);
203 else
204 caa_cpu_relax();
205 }
206 }
207}
208
209void synchronize_rcu(void)
210{
211 CDS_LIST_HEAD(cur_snap_readers);
212 CDS_LIST_HEAD(qsreaders);
213 sigset_t newmask, oldmask;
214 int ret;
215
216 ret = sigfillset(&newmask);
217 assert(!ret);
218 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
219 assert(!ret);
220
221 mutex_lock(&rcu_gp_lock);
222
223 if (cds_list_empty(&registry))
224 goto out;
225
226 /* All threads should read qparity before accessing data structure
227 * where new ptr points to. */
228 /* Write new ptr before changing the qparity */
229 cmm_smp_mb();
230
231 /* Remove old registry elements */
232 rcu_gc_registry();
233
234 /*
235 * Wait for readers to observe original parity or be quiescent.
236 */
237 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
238
239 /*
240 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
241 * model easier to understand. It does not have a big performance impact
242 * anyway, given this is the write-side.
243 */
244 cmm_smp_mb();
245
246 /* Switch parity: 0 -> 1, 1 -> 0 */
247 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ RCU_GP_CTR_PHASE);
248
249 /*
250 * Must commit qparity update to memory before waiting for other parity
251 * quiescent state. Failure to do so could result in the writer waiting
252 * forever while new readers are always accessing data (no progress).
253 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
254 */
255
256 /*
257 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
258 * model easier to understand. It does not have a big performance impact
259 * anyway, given this is the write-side.
260 */
261 cmm_smp_mb();
262
263 /*
264 * Wait for readers to observe new parity or be quiescent.
265 */
266 wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
267
268 /*
269 * Put quiescent reader list back into registry.
270 */
271 cds_list_splice(&qsreaders, &registry);
272
273 /*
274 * Finish waiting for reader threads before letting the old ptr being
275 * freed.
276 */
277 cmm_smp_mb();
278out:
279 mutex_unlock(&rcu_gp_lock);
280 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
281 assert(!ret);
282}
283
284/*
285 * library wrappers to be used by non-LGPL compatible source code.
286 */
287
288void rcu_read_lock(void)
289{
290 _rcu_read_lock();
291}
292
293void rcu_read_unlock(void)
294{
295 _rcu_read_unlock();
296}
297
298int rcu_read_ongoing(void)
299{
300 return _rcu_read_ongoing();
301}
302
303/*
304 * only grow for now.
305 */
306static void resize_arena(struct registry_arena *arena, size_t len)
307{
308 void *new_arena;
309
310 if (!arena->p)
311 new_arena = mmap(arena->p, len,
312 PROT_READ | PROT_WRITE,
313 MAP_ANONYMOUS | MAP_PRIVATE,
314 -1, 0);
315 else
316 new_arena = mremap_wrapper(arena->p, arena->len,
317 len, MREMAP_MAYMOVE);
318 assert(new_arena != MAP_FAILED);
319
320 /*
321 * re-used the same region ?
322 */
323 if (new_arena == arena->p)
324 return;
325
326 bzero(new_arena + arena->len, len - arena->len);
327 arena->p = new_arena;
328}
329
330/* Called with signals off and mutex locked */
331static void add_thread(void)
332{
333 struct rcu_reader *rcu_reader_reg;
334
335 if (registry_arena.len
336 < registry_arena.used + sizeof(struct rcu_reader))
337 resize_arena(&registry_arena,
338 caa_max(registry_arena.len << 1, ARENA_INIT_ALLOC));
339 /*
340 * Find a free spot.
341 */
342 for (rcu_reader_reg = registry_arena.p;
343 (void *)rcu_reader_reg < registry_arena.p + registry_arena.len;
344 rcu_reader_reg++) {
345 if (!rcu_reader_reg->alloc)
346 break;
347 }
348 rcu_reader_reg->alloc = 1;
349 registry_arena.used += sizeof(struct rcu_reader);
350
351 /* Add to registry */
352 rcu_reader_reg->tid = pthread_self();
353 assert(rcu_reader_reg->ctr == 0);
354 cds_list_add(&rcu_reader_reg->node, &registry);
355 URCU_TLS(rcu_reader) = rcu_reader_reg;
356}
357
358/* Called with signals off and mutex locked */
359static void rcu_gc_registry(void)
360{
361 struct rcu_reader *rcu_reader_reg;
362 pthread_t tid;
363 int ret;
364
365 for (rcu_reader_reg = registry_arena.p;
366 (void *)rcu_reader_reg < registry_arena.p + registry_arena.len;
367 rcu_reader_reg++) {
368 if (!rcu_reader_reg->alloc)
369 continue;
370 tid = rcu_reader_reg->tid;
371 ret = pthread_kill(tid, 0);
372 assert(ret != EINVAL);
373 if (ret == ESRCH) {
374 cds_list_del(&rcu_reader_reg->node);
375 rcu_reader_reg->ctr = 0;
376 rcu_reader_reg->alloc = 0;
377 registry_arena.used -= sizeof(struct rcu_reader);
378 }
379 }
380}
381
382/* Disable signals, take mutex, add to registry */
383void rcu_bp_register(void)
384{
385 sigset_t newmask, oldmask;
386 int ret;
387
388 ret = sigfillset(&newmask);
389 assert(!ret);
390 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
391 assert(!ret);
392
393 /*
394 * Check if a signal concurrently registered our thread since
395 * the check in rcu_read_lock(). */
396 if (URCU_TLS(rcu_reader))
397 goto end;
398
399 mutex_lock(&rcu_gp_lock);
400 add_thread();
401 mutex_unlock(&rcu_gp_lock);
402end:
403 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
404 assert(!ret);
405}
406
407void rcu_bp_exit(void)
408{
409 if (registry_arena.p)
410 munmap(registry_arena.p, registry_arena.len);
411}
412
413/*
414 * Holding the rcu_gp_lock across fork will make sure we fork() don't race with
415 * a concurrent thread executing with this same lock held. This ensures that the
416 * registry is in a coherent state in the child.
417 */
418void rcu_bp_before_fork(void)
419{
420 sigset_t newmask, oldmask;
421 int ret;
422
423 ret = sigfillset(&newmask);
424 assert(!ret);
425 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
426 assert(!ret);
427 mutex_lock(&rcu_gp_lock);
428 saved_fork_signal_mask = oldmask;
429}
430
431void rcu_bp_after_fork_parent(void)
432{
433 sigset_t oldmask;
434 int ret;
435
436 oldmask = saved_fork_signal_mask;
437 mutex_unlock(&rcu_gp_lock);
438 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
439 assert(!ret);
440}
441
442void rcu_bp_after_fork_child(void)
443{
444 sigset_t oldmask;
445 int ret;
446
447 rcu_gc_registry();
448 oldmask = saved_fork_signal_mask;
449 mutex_unlock(&rcu_gp_lock);
450 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
451 assert(!ret);
452}
453
454void *rcu_dereference_sym_bp(void *p)
455{
456 return _rcu_dereference(p);
457}
458
459void *rcu_set_pointer_sym_bp(void **p, void *v)
460{
461 cmm_wmb();
462 uatomic_set(p, v);
463 return v;
464}
465
466void *rcu_xchg_pointer_sym_bp(void **p, void *v)
467{
468 cmm_wmb();
469 return uatomic_xchg(p, v);
470}
471
472void *rcu_cmpxchg_pointer_sym_bp(void **p, void *old, void *_new)
473{
474 cmm_wmb();
475 return uatomic_cmpxchg(p, old, _new);
476}
477
478DEFINE_RCU_FLAVOR(rcu_flavor);
479
480#include "urcu-call-rcu-impl.h"
481#include "urcu-defer-impl.h"
This page took 0.027291 seconds and 4 git commands to generate.