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