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