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