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