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