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