Commit | Line | Data |
---|---|---|
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 |
71c811bf | 27 | #define _LGPL_SOURCE |
fdee2e6d MD |
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 | ||
d73fb81f | 39 | #include "urcu/wfcqueue.h" |
57760d44 | 40 | #include "urcu/map/urcu-bp.h" |
af7c2dbe | 41 | #include "urcu/static/urcu-bp.h" |
618b2595 | 42 | #include "urcu-pointer.h" |
bd252a04 | 43 | #include "urcu/tls-compat.h" |
71c811bf | 44 | |
4a6d7378 MD |
45 | #include "urcu-die.h" |
46 | ||
fdee2e6d | 47 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ |
71c811bf | 48 | #undef _LGPL_SOURCE |
fdee2e6d | 49 | #include "urcu-bp.h" |
71c811bf | 50 | #define _LGPL_SOURCE |
fdee2e6d | 51 | |
4c1ae2ea MD |
52 | #ifndef MAP_ANONYMOUS |
53 | #define MAP_ANONYMOUS MAP_ANON | |
54 | #endif | |
55 | ||
c7eaf61c MD |
56 | #ifdef __linux__ |
57 | static | |
58 | void *mremap_wrapper(void *old_address, size_t old_size, | |
59 | size_t new_size, int flags) | |
60 | { | |
61 | return mremap(old_address, old_size, new_size, flags); | |
62 | } | |
63 | #else | |
45a4872f MD |
64 | |
65 | #define MREMAP_MAYMOVE 1 | |
66 | #define MREMAP_FIXED 2 | |
67 | ||
68 | /* | |
95b94246 | 69 | * mremap wrapper for non-Linux systems not allowing MAYMOVE. |
45a4872f MD |
70 | * This is not generic. |
71 | */ | |
c7eaf61c MD |
72 | static |
73 | void *mremap_wrapper(void *old_address, size_t old_size, | |
74 | size_t new_size, int flags) | |
45a4872f | 75 | { |
95b94246 MD |
76 | assert(!(flags & MREMAP_MAYMOVE)); |
77 | ||
78 | return MAP_FAILED; | |
45a4872f MD |
79 | } |
80 | #endif | |
81 | ||
9340c38d MD |
82 | /* Sleep delay in ms */ |
83 | #define RCU_SLEEP_DELAY_MS 10 | |
95b94246 MD |
84 | #define INIT_NR_THREADS 8 |
85 | #define ARENA_INIT_ALLOC \ | |
86 | sizeof(struct registry_chunk) \ | |
87 | + INIT_NR_THREADS * sizeof(struct rcu_reader) | |
fdee2e6d | 88 | |
b7b6a8f5 PB |
89 | /* |
90 | * Active attempts to check for reader Q.S. before calling sleep(). | |
91 | */ | |
92 | #define RCU_QS_ACTIVE_ATTEMPTS 100 | |
93 | ||
76d6a951 MD |
94 | static |
95 | int rcu_bp_refcount; | |
96 | ||
c1be8fb9 MD |
97 | static |
98 | void __attribute__((constructor)) rcu_bp_init(void); | |
99 | static | |
02be5561 | 100 | void __attribute__((destructor)) rcu_bp_exit(void); |
fdee2e6d | 101 | |
6abb4bd5 | 102 | static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER; |
fdee2e6d | 103 | |
c1be8fb9 MD |
104 | static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER; |
105 | static int initialized; | |
106 | ||
107 | static pthread_key_t urcu_bp_key; | |
108 | ||
fdee2e6d | 109 | #ifdef DEBUG_YIELD |
1de4df4b MD |
110 | unsigned int rcu_yield_active; |
111 | DEFINE_URCU_TLS(unsigned int, rcu_rand_yield); | |
fdee2e6d MD |
112 | #endif |
113 | ||
c13c2e55 | 114 | struct rcu_gp rcu_gp = { .ctr = RCU_GP_COUNT }; |
fdee2e6d MD |
115 | |
116 | /* | |
117 | * Pointer to registry elements. Written to only by each individual reader. Read | |
118 | * by both the reader and the writers. | |
119 | */ | |
bd252a04 | 120 | DEFINE_URCU_TLS(struct rcu_reader *, rcu_reader); |
fdee2e6d | 121 | |
16aa9ee8 | 122 | static CDS_LIST_HEAD(registry); |
fdee2e6d | 123 | |
95b94246 MD |
124 | struct registry_chunk { |
125 | size_t data_len; /* data length */ | |
c1be8fb9 | 126 | size_t used; /* amount of data used */ |
95b94246 MD |
127 | struct cds_list_head node; /* chunk_list node */ |
128 | char data[]; | |
129 | }; | |
130 | ||
fdee2e6d | 131 | struct registry_arena { |
95b94246 | 132 | struct cds_list_head chunk_list; |
fdee2e6d MD |
133 | }; |
134 | ||
95b94246 MD |
135 | static struct registry_arena registry_arena = { |
136 | .chunk_list = CDS_LIST_HEAD_INIT(registry_arena.chunk_list), | |
137 | }; | |
fdee2e6d | 138 | |
4cf1675f MD |
139 | /* Saved fork signal mask, protected by rcu_gp_lock */ |
140 | static sigset_t saved_fork_signal_mask; | |
141 | ||
6abb4bd5 | 142 | static void mutex_lock(pthread_mutex_t *mutex) |
fdee2e6d MD |
143 | { |
144 | int ret; | |
145 | ||
146 | #ifndef DISTRUST_SIGNALS_EXTREME | |
6abb4bd5 | 147 | ret = pthread_mutex_lock(mutex); |
4a6d7378 MD |
148 | if (ret) |
149 | urcu_die(ret); | |
fdee2e6d | 150 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ |
6abb4bd5 | 151 | while ((ret = pthread_mutex_trylock(mutex)) != 0) { |
4a6d7378 MD |
152 | if (ret != EBUSY && ret != EINTR) |
153 | urcu_die(ret); | |
fdee2e6d MD |
154 | poll(NULL,0,10); |
155 | } | |
156 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ | |
157 | } | |
158 | ||
6abb4bd5 | 159 | static void mutex_unlock(pthread_mutex_t *mutex) |
fdee2e6d MD |
160 | { |
161 | int ret; | |
162 | ||
6abb4bd5 | 163 | ret = pthread_mutex_unlock(mutex); |
4a6d7378 MD |
164 | if (ret) |
165 | urcu_die(ret); | |
fdee2e6d MD |
166 | } |
167 | ||
52c75091 MD |
168 | static void wait_for_readers(struct cds_list_head *input_readers, |
169 | struct cds_list_head *cur_snap_readers, | |
170 | struct cds_list_head *qsreaders) | |
fdee2e6d | 171 | { |
9340c38d | 172 | unsigned int wait_loops = 0; |
02be5561 | 173 | struct rcu_reader *index, *tmp; |
fdee2e6d | 174 | |
fdee2e6d | 175 | /* |
dd61d077 MD |
176 | * Wait for each thread URCU_TLS(rcu_reader).ctr to either |
177 | * indicate quiescence (not nested), or observe the current | |
c13c2e55 | 178 | * rcu_gp.ctr value. |
fdee2e6d MD |
179 | */ |
180 | for (;;) { | |
9340c38d MD |
181 | if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS) |
182 | wait_loops++; | |
183 | ||
52c75091 MD |
184 | cds_list_for_each_entry_safe(index, tmp, input_readers, node) { |
185 | switch (rcu_reader_state(&index->ctr)) { | |
186 | case RCU_READER_ACTIVE_CURRENT: | |
187 | if (cur_snap_readers) { | |
188 | cds_list_move(&index->node, | |
189 | cur_snap_readers); | |
190 | break; | |
191 | } | |
192 | /* Fall-through */ | |
193 | case RCU_READER_INACTIVE: | |
194 | cds_list_move(&index->node, qsreaders); | |
195 | break; | |
196 | case RCU_READER_ACTIVE_OLD: | |
197 | /* | |
198 | * Old snapshot. Leaving node in | |
199 | * input_readers will make us busy-loop | |
200 | * until the snapshot becomes current or | |
201 | * the reader becomes inactive. | |
202 | */ | |
203 | break; | |
204 | } | |
fdee2e6d MD |
205 | } |
206 | ||
52c75091 | 207 | if (cds_list_empty(input_readers)) { |
fdee2e6d MD |
208 | break; |
209 | } else { | |
9340c38d MD |
210 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) |
211 | (void) poll(NULL, 0, RCU_SLEEP_DELAY_MS); | |
fdee2e6d | 212 | else |
06f22bdb | 213 | caa_cpu_relax(); |
fdee2e6d MD |
214 | } |
215 | } | |
fdee2e6d MD |
216 | } |
217 | ||
218 | void synchronize_rcu(void) | |
219 | { | |
52c75091 MD |
220 | CDS_LIST_HEAD(cur_snap_readers); |
221 | CDS_LIST_HEAD(qsreaders); | |
fdee2e6d MD |
222 | sigset_t newmask, oldmask; |
223 | int ret; | |
224 | ||
6ed4b2e6 | 225 | ret = sigfillset(&newmask); |
fdee2e6d | 226 | assert(!ret); |
6ed4b2e6 | 227 | ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask); |
fdee2e6d MD |
228 | assert(!ret); |
229 | ||
6abb4bd5 | 230 | mutex_lock(&rcu_gp_lock); |
fdee2e6d | 231 | |
16aa9ee8 | 232 | if (cds_list_empty(®istry)) |
2dfb8b5e | 233 | goto out; |
fdee2e6d MD |
234 | |
235 | /* All threads should read qparity before accessing data structure | |
2dfb8b5e | 236 | * where new ptr points to. */ |
fdee2e6d | 237 | /* Write new ptr before changing the qparity */ |
5481ddb3 | 238 | cmm_smp_mb(); |
fdee2e6d | 239 | |
fdee2e6d | 240 | /* |
dd61d077 MD |
241 | * Wait for readers to observe original parity or be quiescent. |
242 | */ | |
52c75091 | 243 | wait_for_readers(®istry, &cur_snap_readers, &qsreaders); |
dd61d077 MD |
244 | |
245 | /* | |
246 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the | |
247 | * model easier to understand. It does not have a big performance impact | |
248 | * anyway, given this is the write-side. | |
249 | */ | |
250 | cmm_smp_mb(); | |
251 | ||
252 | /* Switch parity: 0 -> 1, 1 -> 0 */ | |
c13c2e55 | 253 | CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ RCU_GP_CTR_PHASE); |
dd61d077 MD |
254 | |
255 | /* | |
256 | * Must commit qparity update to memory before waiting for other parity | |
257 | * quiescent state. Failure to do so could result in the writer waiting | |
258 | * forever while new readers are always accessing data (no progress). | |
259 | * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED. | |
fdee2e6d | 260 | */ |
fdee2e6d MD |
261 | |
262 | /* | |
5481ddb3 | 263 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the |
fdee2e6d MD |
264 | * model easier to understand. It does not have a big performance impact |
265 | * anyway, given this is the write-side. | |
266 | */ | |
5481ddb3 | 267 | cmm_smp_mb(); |
fdee2e6d | 268 | |
fdee2e6d | 269 | /* |
dd61d077 | 270 | * Wait for readers to observe new parity or be quiescent. |
fdee2e6d | 271 | */ |
52c75091 MD |
272 | wait_for_readers(&cur_snap_readers, NULL, &qsreaders); |
273 | ||
274 | /* | |
275 | * Put quiescent reader list back into registry. | |
276 | */ | |
277 | cds_list_splice(&qsreaders, ®istry); | |
fdee2e6d MD |
278 | |
279 | /* | |
2dfb8b5e MD |
280 | * Finish waiting for reader threads before letting the old ptr being |
281 | * freed. | |
fdee2e6d | 282 | */ |
5481ddb3 | 283 | cmm_smp_mb(); |
2dfb8b5e | 284 | out: |
6abb4bd5 | 285 | mutex_unlock(&rcu_gp_lock); |
fdee2e6d MD |
286 | ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL); |
287 | assert(!ret); | |
288 | } | |
289 | ||
290 | /* | |
291 | * library wrappers to be used by non-LGPL compatible source code. | |
292 | */ | |
293 | ||
294 | void rcu_read_lock(void) | |
295 | { | |
296 | _rcu_read_lock(); | |
297 | } | |
298 | ||
299 | void rcu_read_unlock(void) | |
300 | { | |
301 | _rcu_read_unlock(); | |
302 | } | |
303 | ||
882f3357 MD |
304 | int rcu_read_ongoing(void) |
305 | { | |
306 | return _rcu_read_ongoing(); | |
307 | } | |
308 | ||
fdee2e6d | 309 | /* |
95b94246 MD |
310 | * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk. |
311 | * Else, try expanding the last chunk. If this fails, allocate a new | |
312 | * chunk twice as big as the last chunk. | |
313 | * Memory used by chunks _never_ moves. A chunk could theoretically be | |
314 | * freed when all "used" slots are released, but we don't do it at this | |
315 | * point. | |
fdee2e6d | 316 | */ |
95b94246 MD |
317 | static |
318 | void expand_arena(struct registry_arena *arena) | |
fdee2e6d | 319 | { |
95b94246 MD |
320 | struct registry_chunk *new_chunk, *last_chunk; |
321 | size_t old_chunk_len, new_chunk_len; | |
322 | ||
323 | /* No chunk. */ | |
324 | if (cds_list_empty(&arena->chunk_list)) { | |
325 | assert(ARENA_INIT_ALLOC >= | |
326 | sizeof(struct registry_chunk) | |
327 | + sizeof(struct rcu_reader)); | |
328 | new_chunk_len = ARENA_INIT_ALLOC; | |
329 | new_chunk = mmap(NULL, new_chunk_len, | |
9d8612b7 MD |
330 | PROT_READ | PROT_WRITE, |
331 | MAP_ANONYMOUS | MAP_PRIVATE, | |
332 | -1, 0); | |
95b94246 MD |
333 | if (new_chunk == MAP_FAILED) |
334 | abort(); | |
335 | bzero(new_chunk, new_chunk_len); | |
336 | new_chunk->data_len = | |
337 | new_chunk_len - sizeof(struct registry_chunk); | |
338 | cds_list_add_tail(&new_chunk->node, &arena->chunk_list); | |
339 | return; /* We're done. */ | |
340 | } | |
9d8612b7 | 341 | |
95b94246 MD |
342 | /* Try expanding last chunk. */ |
343 | last_chunk = cds_list_entry(arena->chunk_list.prev, | |
344 | struct registry_chunk, node); | |
345 | old_chunk_len = | |
346 | last_chunk->data_len + sizeof(struct registry_chunk); | |
347 | new_chunk_len = old_chunk_len << 1; | |
348 | ||
349 | /* Don't allow memory mapping to move, just expand. */ | |
350 | new_chunk = mremap_wrapper(last_chunk, old_chunk_len, | |
351 | new_chunk_len, 0); | |
352 | if (new_chunk != MAP_FAILED) { | |
353 | /* Should not have moved. */ | |
354 | assert(new_chunk == last_chunk); | |
355 | bzero((char *) last_chunk + old_chunk_len, | |
356 | new_chunk_len - old_chunk_len); | |
357 | last_chunk->data_len = | |
358 | new_chunk_len - sizeof(struct registry_chunk); | |
359 | return; /* We're done. */ | |
360 | } | |
0617bf4c | 361 | |
95b94246 MD |
362 | /* Remap did not succeed, we need to add a new chunk. */ |
363 | new_chunk = mmap(NULL, new_chunk_len, | |
364 | PROT_READ | PROT_WRITE, | |
365 | MAP_ANONYMOUS | MAP_PRIVATE, | |
366 | -1, 0); | |
367 | if (new_chunk == MAP_FAILED) | |
368 | abort(); | |
369 | bzero(new_chunk, new_chunk_len); | |
370 | new_chunk->data_len = | |
371 | new_chunk_len - sizeof(struct registry_chunk); | |
372 | cds_list_add_tail(&new_chunk->node, &arena->chunk_list); | |
373 | } | |
fdee2e6d | 374 | |
95b94246 MD |
375 | static |
376 | struct rcu_reader *arena_alloc(struct registry_arena *arena) | |
377 | { | |
378 | struct registry_chunk *chunk; | |
379 | struct rcu_reader *rcu_reader_reg; | |
380 | int expand_done = 0; /* Only allow to expand once per alloc */ | |
381 | size_t len = sizeof(struct rcu_reader); | |
382 | ||
383 | retry: | |
384 | cds_list_for_each_entry(chunk, &arena->chunk_list, node) { | |
385 | if (chunk->data_len - chunk->used < len) | |
386 | continue; | |
387 | /* Find spot */ | |
388 | for (rcu_reader_reg = (struct rcu_reader *) &chunk->data[0]; | |
389 | rcu_reader_reg < (struct rcu_reader *) &chunk->data[chunk->data_len]; | |
390 | rcu_reader_reg++) { | |
391 | if (!rcu_reader_reg->alloc) { | |
392 | rcu_reader_reg->alloc = 1; | |
393 | chunk->used += len; | |
394 | return rcu_reader_reg; | |
395 | } | |
396 | } | |
397 | } | |
398 | ||
399 | if (!expand_done) { | |
400 | expand_arena(arena); | |
401 | expand_done = 1; | |
402 | goto retry; | |
403 | } | |
404 | ||
405 | return NULL; | |
fdee2e6d MD |
406 | } |
407 | ||
408 | /* Called with signals off and mutex locked */ | |
95b94246 MD |
409 | static |
410 | void add_thread(void) | |
fdee2e6d | 411 | { |
02be5561 | 412 | struct rcu_reader *rcu_reader_reg; |
c1be8fb9 | 413 | int ret; |
fdee2e6d | 414 | |
95b94246 MD |
415 | rcu_reader_reg = arena_alloc(®istry_arena); |
416 | if (!rcu_reader_reg) | |
417 | abort(); | |
c1be8fb9 MD |
418 | ret = pthread_setspecific(urcu_bp_key, rcu_reader_reg); |
419 | if (ret) | |
420 | abort(); | |
fdee2e6d MD |
421 | |
422 | /* Add to registry */ | |
02be5561 MD |
423 | rcu_reader_reg->tid = pthread_self(); |
424 | assert(rcu_reader_reg->ctr == 0); | |
16aa9ee8 | 425 | cds_list_add(&rcu_reader_reg->node, ®istry); |
95b94246 MD |
426 | /* |
427 | * Reader threads are pointing to the reader registry. This is | |
428 | * why its memory should never be relocated. | |
429 | */ | |
bd252a04 | 430 | URCU_TLS(rcu_reader) = rcu_reader_reg; |
fdee2e6d MD |
431 | } |
432 | ||
c1be8fb9 MD |
433 | /* Called with mutex locked */ |
434 | static | |
435 | void cleanup_thread(struct registry_chunk *chunk, | |
436 | struct rcu_reader *rcu_reader_reg) | |
437 | { | |
438 | rcu_reader_reg->ctr = 0; | |
439 | cds_list_del(&rcu_reader_reg->node); | |
440 | rcu_reader_reg->tid = 0; | |
441 | rcu_reader_reg->alloc = 0; | |
442 | chunk->used -= sizeof(struct rcu_reader); | |
443 | } | |
444 | ||
445 | static | |
446 | struct registry_chunk *find_chunk(struct rcu_reader *rcu_reader_reg) | |
fdee2e6d | 447 | { |
95b94246 | 448 | struct registry_chunk *chunk; |
fdee2e6d | 449 | |
95b94246 | 450 | cds_list_for_each_entry(chunk, ®istry_arena.chunk_list, node) { |
c1be8fb9 MD |
451 | if (rcu_reader_reg < (struct rcu_reader *) &chunk->data[0]) |
452 | continue; | |
453 | if (rcu_reader_reg >= (struct rcu_reader *) &chunk->data[chunk->data_len]) | |
454 | continue; | |
455 | return chunk; | |
456 | } | |
457 | return NULL; | |
458 | } | |
95b94246 | 459 | |
c1be8fb9 MD |
460 | /* Called with signals off and mutex locked */ |
461 | static | |
76d6a951 | 462 | void remove_thread(struct rcu_reader *rcu_reader_reg) |
c1be8fb9 | 463 | { |
c1be8fb9 MD |
464 | cleanup_thread(find_chunk(rcu_reader_reg), rcu_reader_reg); |
465 | URCU_TLS(rcu_reader) = NULL; | |
fdee2e6d MD |
466 | } |
467 | ||
468 | /* Disable signals, take mutex, add to registry */ | |
469 | void rcu_bp_register(void) | |
470 | { | |
471 | sigset_t newmask, oldmask; | |
472 | int ret; | |
473 | ||
6ed4b2e6 | 474 | ret = sigfillset(&newmask); |
c1be8fb9 MD |
475 | if (ret) |
476 | abort(); | |
6ed4b2e6 | 477 | ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask); |
c1be8fb9 MD |
478 | if (ret) |
479 | abort(); | |
fdee2e6d MD |
480 | |
481 | /* | |
482 | * Check if a signal concurrently registered our thread since | |
c1be8fb9 MD |
483 | * the check in rcu_read_lock(). |
484 | */ | |
bd252a04 | 485 | if (URCU_TLS(rcu_reader)) |
fdee2e6d MD |
486 | goto end; |
487 | ||
c1be8fb9 MD |
488 | /* |
489 | * Take care of early registration before urcu_bp constructor. | |
490 | */ | |
491 | rcu_bp_init(); | |
492 | ||
6abb4bd5 | 493 | mutex_lock(&rcu_gp_lock); |
fdee2e6d | 494 | add_thread(); |
6abb4bd5 | 495 | mutex_unlock(&rcu_gp_lock); |
fdee2e6d MD |
496 | end: |
497 | ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL); | |
c1be8fb9 MD |
498 | if (ret) |
499 | abort(); | |
500 | } | |
501 | ||
502 | /* Disable signals, take mutex, remove from registry */ | |
503 | static | |
76d6a951 | 504 | void rcu_bp_unregister(struct rcu_reader *rcu_reader_reg) |
c1be8fb9 MD |
505 | { |
506 | sigset_t newmask, oldmask; | |
507 | int ret; | |
508 | ||
509 | ret = sigfillset(&newmask); | |
510 | if (ret) | |
511 | abort(); | |
512 | ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask); | |
513 | if (ret) | |
514 | abort(); | |
515 | ||
516 | mutex_lock(&rcu_gp_lock); | |
76d6a951 | 517 | remove_thread(rcu_reader_reg); |
c1be8fb9 MD |
518 | mutex_unlock(&rcu_gp_lock); |
519 | ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL); | |
520 | if (ret) | |
521 | abort(); | |
76d6a951 | 522 | rcu_bp_exit(); |
c1be8fb9 MD |
523 | } |
524 | ||
525 | /* | |
526 | * Remove thread from the registry when it exits, and flag it as | |
527 | * destroyed so garbage collection can take care of it. | |
528 | */ | |
529 | static | |
530 | void urcu_bp_thread_exit_notifier(void *rcu_key) | |
531 | { | |
76d6a951 | 532 | rcu_bp_unregister(rcu_key); |
c1be8fb9 MD |
533 | } |
534 | ||
535 | static | |
536 | void rcu_bp_init(void) | |
537 | { | |
538 | mutex_lock(&init_lock); | |
76d6a951 | 539 | if (!rcu_bp_refcount++) { |
c1be8fb9 MD |
540 | int ret; |
541 | ||
542 | ret = pthread_key_create(&urcu_bp_key, | |
543 | urcu_bp_thread_exit_notifier); | |
544 | if (ret) | |
545 | abort(); | |
546 | initialized = 1; | |
547 | } | |
548 | mutex_unlock(&init_lock); | |
fdee2e6d MD |
549 | } |
550 | ||
c1be8fb9 | 551 | static |
9380711a | 552 | void rcu_bp_exit(void) |
fdee2e6d | 553 | { |
76d6a951 MD |
554 | mutex_lock(&init_lock); |
555 | if (!--rcu_bp_refcount) { | |
556 | struct registry_chunk *chunk, *tmp; | |
557 | int ret; | |
95b94246 | 558 | |
76d6a951 MD |
559 | cds_list_for_each_entry_safe(chunk, tmp, |
560 | ®istry_arena.chunk_list, node) { | |
561 | munmap(chunk, chunk->data_len | |
562 | + sizeof(struct registry_chunk)); | |
563 | } | |
564 | ret = pthread_key_delete(urcu_bp_key); | |
565 | if (ret) | |
566 | abort(); | |
95b94246 | 567 | } |
76d6a951 | 568 | mutex_unlock(&init_lock); |
fdee2e6d | 569 | } |
4cf1675f MD |
570 | |
571 | /* | |
572 | * Holding the rcu_gp_lock across fork will make sure we fork() don't race with | |
573 | * a concurrent thread executing with this same lock held. This ensures that the | |
574 | * registry is in a coherent state in the child. | |
575 | */ | |
576 | void rcu_bp_before_fork(void) | |
577 | { | |
578 | sigset_t newmask, oldmask; | |
579 | int ret; | |
580 | ||
6ed4b2e6 | 581 | ret = sigfillset(&newmask); |
4cf1675f | 582 | assert(!ret); |
6ed4b2e6 | 583 | ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask); |
4cf1675f MD |
584 | assert(!ret); |
585 | mutex_lock(&rcu_gp_lock); | |
586 | saved_fork_signal_mask = oldmask; | |
587 | } | |
588 | ||
589 | void rcu_bp_after_fork_parent(void) | |
590 | { | |
591 | sigset_t oldmask; | |
592 | int ret; | |
593 | ||
594 | oldmask = saved_fork_signal_mask; | |
595 | mutex_unlock(&rcu_gp_lock); | |
596 | ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL); | |
597 | assert(!ret); | |
598 | } | |
599 | ||
c1be8fb9 MD |
600 | /* |
601 | * Prune all entries from registry except our own thread. Fits the Linux | |
602 | * fork behavior. Called with rcu_gp_lock held. | |
603 | */ | |
604 | static | |
605 | void urcu_bp_prune_registry(void) | |
606 | { | |
607 | struct registry_chunk *chunk; | |
608 | struct rcu_reader *rcu_reader_reg; | |
609 | ||
610 | cds_list_for_each_entry(chunk, ®istry_arena.chunk_list, node) { | |
611 | for (rcu_reader_reg = (struct rcu_reader *) &chunk->data[0]; | |
612 | rcu_reader_reg < (struct rcu_reader *) &chunk->data[chunk->data_len]; | |
613 | rcu_reader_reg++) { | |
614 | if (!rcu_reader_reg->alloc) | |
615 | continue; | |
616 | if (rcu_reader_reg->tid == pthread_self()) | |
617 | continue; | |
618 | cleanup_thread(chunk, rcu_reader_reg); | |
619 | } | |
620 | } | |
621 | } | |
622 | ||
4cf1675f MD |
623 | void rcu_bp_after_fork_child(void) |
624 | { | |
625 | sigset_t oldmask; | |
626 | int ret; | |
627 | ||
c1be8fb9 | 628 | urcu_bp_prune_registry(); |
4cf1675f MD |
629 | oldmask = saved_fork_signal_mask; |
630 | mutex_unlock(&rcu_gp_lock); | |
631 | ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL); | |
632 | assert(!ret); | |
633 | } | |
5e77fc1f | 634 | |
9b7981bb MD |
635 | void *rcu_dereference_sym_bp(void *p) |
636 | { | |
637 | return _rcu_dereference(p); | |
638 | } | |
639 | ||
5efd3cd2 MD |
640 | void *rcu_set_pointer_sym_bp(void **p, void *v) |
641 | { | |
642 | cmm_wmb(); | |
424d4ed5 MD |
643 | uatomic_set(p, v); |
644 | return v; | |
5efd3cd2 MD |
645 | } |
646 | ||
647 | void *rcu_xchg_pointer_sym_bp(void **p, void *v) | |
648 | { | |
649 | cmm_wmb(); | |
650 | return uatomic_xchg(p, v); | |
651 | } | |
652 | ||
653 | void *rcu_cmpxchg_pointer_sym_bp(void **p, void *old, void *_new) | |
654 | { | |
655 | cmm_wmb(); | |
656 | return uatomic_cmpxchg(p, old, _new); | |
657 | } | |
658 | ||
5e6b23a6 | 659 | DEFINE_RCU_FLAVOR(rcu_flavor); |
541d828d | 660 | |
5e77fc1f | 661 | #include "urcu-call-rcu-impl.h" |
0376e7b2 | 662 | #include "urcu-defer-impl.h" |