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