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