rculfstack: document "push"
[urcu.git] / urcu-bp.c
CommitLineData
fdee2e6d
MD
1/*
2 * urcu-bp.c
3 *
4 * Userspace RCU library, "bulletproof" version.
5 *
6982d6d7 6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fdee2e6d
MD
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
0617bf4c 26#define _GNU_SOURCE
fdee2e6d
MD
27#include <stdio.h>
28#include <pthread.h>
29#include <signal.h>
30#include <assert.h>
31#include <stdlib.h>
32#include <string.h>
33#include <errno.h>
34#include <poll.h>
35#include <unistd.h>
36#include <sys/mman.h>
37
57760d44 38#include "urcu/map/urcu-bp.h"
5e77fc1f 39
af7c2dbe 40#include "urcu/static/urcu-bp.h"
fdee2e6d
MD
41/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
42#include "urcu-bp.h"
43
4c1ae2ea
MD
44#ifndef MAP_ANONYMOUS
45#define MAP_ANONYMOUS MAP_ANON
46#endif
47
45a4872f
MD
48#ifndef __linux__
49
50#define MREMAP_MAYMOVE 1
51#define MREMAP_FIXED 2
52
53/*
54 * mremap wrapper for non-Linux systems. Maps a RW, anonymous private mapping.
55 * This is not generic.
56*/
57void *mremap(void *old_address, size_t old_size, size_t new_size, int flags)
58{
dc745ef6 59 void *new_address;
45a4872f
MD
60
61 assert(flags & MREMAP_MAYMOVE);
62 assert(!(flags & MREMAP_FIXED));
63 new_address = mmap(old_address, new_size,
64 PROT_READ | PROT_WRITE,
65 MAP_ANONYMOUS | MAP_PRIVATE,
66 -1, 0);
67 if (new_address == MAP_FAILED)
68 return MAP_FAILED;
69 if (old_address) {
70 memcpy(new_address, old_address, old_size);
71 munmap(old_address, old_size);
72 }
73 return new_address;
74}
75#endif
76
fdee2e6d
MD
77/* Sleep delay in us */
78#define RCU_SLEEP_DELAY 1000
79#define ARENA_INIT_ALLOC 16
80
b7b6a8f5
PB
81/*
82 * Active attempts to check for reader Q.S. before calling sleep().
83 */
84#define RCU_QS_ACTIVE_ATTEMPTS 100
85
02be5561 86void __attribute__((destructor)) rcu_bp_exit(void);
fdee2e6d 87
6abb4bd5 88static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
fdee2e6d
MD
89
90#ifdef DEBUG_YIELD
91unsigned int yield_active;
92unsigned int __thread rand_yield;
93#endif
94
95/*
96 * Global grace period counter.
02be5561 97 * Contains the current RCU_GP_CTR_PHASE.
fdee2e6d
MD
98 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
99 * Written to only by writer with mutex taken. Read by both writer and readers.
100 */
02be5561 101long rcu_gp_ctr = RCU_GP_COUNT;
fdee2e6d
MD
102
103/*
104 * Pointer to registry elements. Written to only by each individual reader. Read
105 * by both the reader and the writers.
106 */
02be5561 107struct rcu_reader __thread *rcu_reader;
fdee2e6d 108
16aa9ee8 109static CDS_LIST_HEAD(registry);
fdee2e6d
MD
110
111struct registry_arena {
112 void *p;
113 size_t len;
114 size_t used;
115};
116
117static struct registry_arena registry_arena;
118
4cf1675f
MD
119/* Saved fork signal mask, protected by rcu_gp_lock */
120static sigset_t saved_fork_signal_mask;
121
fdee2e6d
MD
122static void rcu_gc_registry(void);
123
6abb4bd5 124static void mutex_lock(pthread_mutex_t *mutex)
fdee2e6d
MD
125{
126 int ret;
127
128#ifndef DISTRUST_SIGNALS_EXTREME
6abb4bd5 129 ret = pthread_mutex_lock(mutex);
fdee2e6d
MD
130 if (ret) {
131 perror("Error in pthread mutex lock");
132 exit(-1);
133 }
134#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
6abb4bd5 135 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
fdee2e6d
MD
136 if (ret != EBUSY && ret != EINTR) {
137 printf("ret = %d, errno = %d\n", ret, errno);
138 perror("Error in pthread mutex lock");
139 exit(-1);
140 }
fdee2e6d
MD
141 poll(NULL,0,10);
142 }
143#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
144}
145
6abb4bd5 146static void mutex_unlock(pthread_mutex_t *mutex)
fdee2e6d
MD
147{
148 int ret;
149
6abb4bd5 150 ret = pthread_mutex_unlock(mutex);
fdee2e6d
MD
151 if (ret) {
152 perror("Error in pthread mutex unlock");
153 exit(-1);
154 }
155}
156
2dfb8b5e 157void update_counter_and_wait(void)
fdee2e6d 158{
16aa9ee8 159 CDS_LIST_HEAD(qsreaders);
fdee2e6d 160 int wait_loops = 0;
02be5561 161 struct rcu_reader *index, *tmp;
fdee2e6d 162
32c15e4e 163 /* Switch parity: 0 -> 1, 1 -> 0 */
6cf3827c 164 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE);
2dfb8b5e
MD
165
166 /*
167 * Must commit qparity update to memory before waiting for other parity
168 * quiescent state. Failure to do so could result in the writer waiting
169 * forever while new readers are always accessing data (no progress).
6cf3827c 170 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
2dfb8b5e
MD
171 */
172
173 /*
5481ddb3 174 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
2dfb8b5e
MD
175 * model easier to understand. It does not have a big performance impact
176 * anyway, given this is the write-side.
177 */
5481ddb3 178 cmm_smp_mb();
2dfb8b5e 179
fdee2e6d 180 /*
02be5561 181 * Wait for each thread rcu_reader.ctr count to become 0.
fdee2e6d
MD
182 */
183 for (;;) {
184 wait_loops++;
16aa9ee8 185 cds_list_for_each_entry_safe(index, tmp, &registry, node) {
fdee2e6d 186 if (!rcu_old_gp_ongoing(&index->ctr))
16aa9ee8 187 cds_list_move(&index->node, &qsreaders);
fdee2e6d
MD
188 }
189
16aa9ee8 190 if (cds_list_empty(&registry)) {
fdee2e6d
MD
191 break;
192 } else {
193 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS)
194 usleep(RCU_SLEEP_DELAY);
195 else
06f22bdb 196 caa_cpu_relax();
fdee2e6d
MD
197 }
198 }
199 /* put back the reader list in the registry */
16aa9ee8 200 cds_list_splice(&qsreaders, &registry);
fdee2e6d
MD
201}
202
203void synchronize_rcu(void)
204{
205 sigset_t newmask, oldmask;
206 int ret;
207
208 ret = sigemptyset(&newmask);
209 assert(!ret);
210 ret = pthread_sigmask(SIG_SETMASK, &newmask, &oldmask);
211 assert(!ret);
212
6abb4bd5 213 mutex_lock(&rcu_gp_lock);
fdee2e6d 214
16aa9ee8 215 if (cds_list_empty(&registry))
2dfb8b5e 216 goto out;
fdee2e6d
MD
217
218 /* All threads should read qparity before accessing data structure
2dfb8b5e 219 * where new ptr points to. */
fdee2e6d 220 /* Write new ptr before changing the qparity */
5481ddb3 221 cmm_smp_mb();
fdee2e6d 222
2dfb8b5e
MD
223 /* Remove old registry elements */
224 rcu_gc_registry();
fdee2e6d
MD
225
226 /*
227 * Wait for previous parity to be empty of readers.
228 */
2dfb8b5e 229 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
fdee2e6d
MD
230
231 /*
5481ddb3 232 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
fdee2e6d
MD
233 * model easier to understand. It does not have a big performance impact
234 * anyway, given this is the write-side.
235 */
5481ddb3 236 cmm_smp_mb();
fdee2e6d 237
fdee2e6d 238 /*
2dfb8b5e 239 * Wait for previous parity to be empty of readers.
fdee2e6d 240 */
2dfb8b5e 241 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
fdee2e6d
MD
242
243 /*
2dfb8b5e
MD
244 * Finish waiting for reader threads before letting the old ptr being
245 * freed.
fdee2e6d 246 */
5481ddb3 247 cmm_smp_mb();
2dfb8b5e 248out:
6abb4bd5 249 mutex_unlock(&rcu_gp_lock);
fdee2e6d
MD
250 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
251 assert(!ret);
252}
253
254/*
255 * library wrappers to be used by non-LGPL compatible source code.
256 */
257
258void rcu_read_lock(void)
259{
260 _rcu_read_lock();
261}
262
263void rcu_read_unlock(void)
264{
265 _rcu_read_unlock();
266}
267
268/*
269 * only grow for now.
270 */
271static void resize_arena(struct registry_arena *arena, size_t len)
272{
273 void *new_arena;
274
0617bf4c
MD
275 if (!arena->p)
276 new_arena = mmap(arena->p, len,
277 PROT_READ | PROT_WRITE,
278 MAP_ANONYMOUS | MAP_PRIVATE,
279 -1, 0);
280 else
281 new_arena = mremap(arena->p, arena->len,
282 len, MREMAP_MAYMOVE);
283 assert(new_arena != MAP_FAILED);
284
fdee2e6d
MD
285 /*
286 * re-used the same region ?
287 */
288 if (new_arena == arena->p)
289 return;
290
fdee2e6d
MD
291 bzero(new_arena + arena->len, len - arena->len);
292 arena->p = new_arena;
293}
294
295/* Called with signals off and mutex locked */
296static void add_thread(void)
297{
02be5561 298 struct rcu_reader *rcu_reader_reg;
fdee2e6d
MD
299
300 if (registry_arena.len
02be5561 301 < registry_arena.used + sizeof(struct rcu_reader))
fdee2e6d 302 resize_arena(&registry_arena,
2f8a5ae7 303 caa_max(registry_arena.len << 1, ARENA_INIT_ALLOC));
fdee2e6d
MD
304 /*
305 * Find a free spot.
306 */
02be5561
MD
307 for (rcu_reader_reg = registry_arena.p;
308 (void *)rcu_reader_reg < registry_arena.p + registry_arena.len;
309 rcu_reader_reg++) {
310 if (!rcu_reader_reg->alloc)
fdee2e6d
MD
311 break;
312 }
02be5561
MD
313 rcu_reader_reg->alloc = 1;
314 registry_arena.used += sizeof(struct rcu_reader);
fdee2e6d
MD
315
316 /* Add to registry */
02be5561
MD
317 rcu_reader_reg->tid = pthread_self();
318 assert(rcu_reader_reg->ctr == 0);
16aa9ee8 319 cds_list_add(&rcu_reader_reg->node, &registry);
02be5561 320 rcu_reader = rcu_reader_reg;
fdee2e6d
MD
321}
322
323/* Called with signals off and mutex locked */
324static void rcu_gc_registry(void)
325{
02be5561 326 struct rcu_reader *rcu_reader_reg;
fdee2e6d
MD
327 pthread_t tid;
328 int ret;
329
02be5561
MD
330 for (rcu_reader_reg = registry_arena.p;
331 (void *)rcu_reader_reg < registry_arena.p + registry_arena.len;
332 rcu_reader_reg++) {
333 if (!rcu_reader_reg->alloc)
fdee2e6d 334 continue;
02be5561 335 tid = rcu_reader_reg->tid;
fdee2e6d
MD
336 ret = pthread_kill(tid, 0);
337 assert(ret != EINVAL);
338 if (ret == ESRCH) {
16aa9ee8 339 cds_list_del(&rcu_reader_reg->node);
79266051 340 rcu_reader_reg->ctr = 0;
02be5561
MD
341 rcu_reader_reg->alloc = 0;
342 registry_arena.used -= sizeof(struct rcu_reader);
fdee2e6d
MD
343 }
344 }
345}
346
347/* Disable signals, take mutex, add to registry */
348void rcu_bp_register(void)
349{
350 sigset_t newmask, oldmask;
351 int ret;
352
353 ret = sigemptyset(&newmask);
354 assert(!ret);
355 ret = pthread_sigmask(SIG_SETMASK, &newmask, &oldmask);
356 assert(!ret);
357
358 /*
359 * Check if a signal concurrently registered our thread since
360 * the check in rcu_read_lock(). */
02be5561 361 if (rcu_reader)
fdee2e6d
MD
362 goto end;
363
6abb4bd5 364 mutex_lock(&rcu_gp_lock);
fdee2e6d 365 add_thread();
6abb4bd5 366 mutex_unlock(&rcu_gp_lock);
fdee2e6d
MD
367end:
368 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
369 assert(!ret);
370}
371
02be5561 372void rcu_bp_exit()
fdee2e6d
MD
373{
374 munmap(registry_arena.p, registry_arena.len);
375}
4cf1675f
MD
376
377/*
378 * Holding the rcu_gp_lock across fork will make sure we fork() don't race with
379 * a concurrent thread executing with this same lock held. This ensures that the
380 * registry is in a coherent state in the child.
381 */
382void rcu_bp_before_fork(void)
383{
384 sigset_t newmask, oldmask;
385 int ret;
386
387 ret = sigemptyset(&newmask);
388 assert(!ret);
389 ret = pthread_sigmask(SIG_SETMASK, &newmask, &oldmask);
390 assert(!ret);
391 mutex_lock(&rcu_gp_lock);
392 saved_fork_signal_mask = oldmask;
393}
394
395void rcu_bp_after_fork_parent(void)
396{
397 sigset_t oldmask;
398 int ret;
399
400 oldmask = saved_fork_signal_mask;
401 mutex_unlock(&rcu_gp_lock);
402 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
403 assert(!ret);
404}
405
406void rcu_bp_after_fork_child(void)
407{
408 sigset_t oldmask;
409 int ret;
410
411 rcu_gc_registry();
412 oldmask = saved_fork_signal_mask;
413 mutex_unlock(&rcu_gp_lock);
414 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
415 assert(!ret);
416}
5e77fc1f
PM
417
418#include "urcu-call-rcu-impl.h"
0376e7b2 419#include "urcu-defer-impl.h"
This page took 0.041748 seconds and 4 git commands to generate.