call_rcu: use wfcqueue, eliminate false-sharing
[urcu.git] / urcu-call-rcu-impl.h
1 /*
2 * urcu-call-rcu.c
3 *
4 * Userspace RCU library - batch memory reclamation with kernel API
5 *
6 * Copyright (c) 2010 Paul E. McKenney <paulmck@linux.vnet.ibm.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #define _GNU_SOURCE
24 #define _LGPL_SOURCE
25 #include <stdio.h>
26 #include <pthread.h>
27 #include <signal.h>
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <poll.h>
34 #include <sys/time.h>
35 #include <unistd.h>
36 #include <sched.h>
37
38 #include "config.h"
39 #include "urcu/wfcqueue.h"
40 #include "urcu-call-rcu.h"
41 #include "urcu-pointer.h"
42 #include "urcu/list.h"
43 #include "urcu/futex.h"
44 #include "urcu/tls-compat.h"
45 #include "urcu-die.h"
46
47 /* Data structure that identifies a call_rcu thread. */
48
49 struct call_rcu_data {
50 /*
51 * Align the tail on cache line size to eliminate false-sharing
52 * with head.
53 */
54 struct cds_wfcq_tail __attribute__((aligned(CAA_CACHE_LINE_SIZE))) cbs_tail;
55 /* Alignment on cache line size will add padding here */
56
57 struct cds_wfcq_head cbs_head;
58 unsigned long flags;
59 int32_t futex;
60 unsigned long qlen; /* maintained for debugging. */
61 pthread_t tid;
62 int cpu_affinity;
63 struct cds_list_head list;
64 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
65
66 /*
67 * List of all call_rcu_data structures to keep valgrind happy.
68 * Protected by call_rcu_mutex.
69 */
70
71 CDS_LIST_HEAD(call_rcu_data_list);
72
73 /* Link a thread using call_rcu() to its call_rcu thread. */
74
75 static DEFINE_URCU_TLS(struct call_rcu_data *, thread_call_rcu_data);
76
77 /* Guard call_rcu thread creation. */
78
79 static pthread_mutex_t call_rcu_mutex = PTHREAD_MUTEX_INITIALIZER;
80
81 /* If a given thread does not have its own call_rcu thread, this is default. */
82
83 static struct call_rcu_data *default_call_rcu_data;
84
85 /*
86 * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are
87 * available, then we can have call_rcu threads assigned to individual
88 * CPUs rather than only to specific threads.
89 */
90
91 #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
92
93 /*
94 * Pointer to array of pointers to per-CPU call_rcu_data structures
95 * and # CPUs. per_cpu_call_rcu_data is a RCU-protected pointer to an
96 * array of RCU-protected pointers to call_rcu_data. call_rcu acts as a
97 * RCU read-side and reads per_cpu_call_rcu_data and the per-cpu pointer
98 * without mutex. The call_rcu_mutex protects updates.
99 */
100
101 static struct call_rcu_data **per_cpu_call_rcu_data;
102 static long maxcpus;
103
104 static void maxcpus_reset(void)
105 {
106 maxcpus = 0;
107 }
108
109 /* Allocate the array if it has not already been allocated. */
110
111 static void alloc_cpu_call_rcu_data(void)
112 {
113 struct call_rcu_data **p;
114 static int warned = 0;
115
116 if (maxcpus != 0)
117 return;
118 maxcpus = sysconf(_SC_NPROCESSORS_CONF);
119 if (maxcpus <= 0) {
120 return;
121 }
122 p = malloc(maxcpus * sizeof(*per_cpu_call_rcu_data));
123 if (p != NULL) {
124 memset(p, '\0', maxcpus * sizeof(*per_cpu_call_rcu_data));
125 rcu_set_pointer(&per_cpu_call_rcu_data, p);
126 } else {
127 if (!warned) {
128 fprintf(stderr, "[error] liburcu: unable to allocate per-CPU pointer array\n");
129 }
130 warned = 1;
131 }
132 }
133
134 #else /* #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
135
136 /*
137 * per_cpu_call_rcu_data should be constant, but some functions below, used both
138 * for cases where cpu number is available and not available, assume it it not
139 * constant.
140 */
141 static struct call_rcu_data **per_cpu_call_rcu_data = NULL;
142 static const long maxcpus = -1;
143
144 static void maxcpus_reset(void)
145 {
146 }
147
148 static void alloc_cpu_call_rcu_data(void)
149 {
150 }
151
152 static int sched_getcpu(void)
153 {
154 return -1;
155 }
156
157 #endif /* #else #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
158
159 /* Acquire the specified pthread mutex. */
160
161 static void call_rcu_lock(pthread_mutex_t *pmp)
162 {
163 int ret;
164
165 ret = pthread_mutex_lock(pmp);
166 if (ret)
167 urcu_die(ret);
168 }
169
170 /* Release the specified pthread mutex. */
171
172 static void call_rcu_unlock(pthread_mutex_t *pmp)
173 {
174 int ret;
175
176 ret = pthread_mutex_unlock(pmp);
177 if (ret)
178 urcu_die(ret);
179 }
180
181 #if HAVE_SCHED_SETAFFINITY
182 static
183 int set_thread_cpu_affinity(struct call_rcu_data *crdp)
184 {
185 cpu_set_t mask;
186
187 if (crdp->cpu_affinity < 0)
188 return 0;
189
190 CPU_ZERO(&mask);
191 CPU_SET(crdp->cpu_affinity, &mask);
192 #if SCHED_SETAFFINITY_ARGS == 2
193 return sched_setaffinity(0, &mask);
194 #else
195 return sched_setaffinity(0, sizeof(mask), &mask);
196 #endif
197 }
198 #else
199 static
200 int set_thread_cpu_affinity(struct call_rcu_data *crdp)
201 {
202 return 0;
203 }
204 #endif
205
206 static void call_rcu_wait(struct call_rcu_data *crdp)
207 {
208 /* Read call_rcu list before read futex */
209 cmm_smp_mb();
210 if (uatomic_read(&crdp->futex) == -1)
211 futex_async(&crdp->futex, FUTEX_WAIT, -1,
212 NULL, NULL, 0);
213 }
214
215 static void call_rcu_wake_up(struct call_rcu_data *crdp)
216 {
217 /* Write to call_rcu list before reading/writing futex */
218 cmm_smp_mb();
219 if (caa_unlikely(uatomic_read(&crdp->futex) == -1)) {
220 uatomic_set(&crdp->futex, 0);
221 futex_async(&crdp->futex, FUTEX_WAKE, 1,
222 NULL, NULL, 0);
223 }
224 }
225
226 /* This is the code run by each call_rcu thread. */
227
228 static void *call_rcu_thread(void *arg)
229 {
230 unsigned long cbcount;
231 struct call_rcu_data *crdp = (struct call_rcu_data *) arg;
232 int rt = !!(uatomic_read(&crdp->flags) & URCU_CALL_RCU_RT);
233 int ret;
234
235 ret = set_thread_cpu_affinity(crdp);
236 if (ret)
237 urcu_die(errno);
238
239 /*
240 * If callbacks take a read-side lock, we need to be registered.
241 */
242 rcu_register_thread();
243
244 URCU_TLS(thread_call_rcu_data) = crdp;
245 if (!rt) {
246 uatomic_dec(&crdp->futex);
247 /* Decrement futex before reading call_rcu list */
248 cmm_smp_mb();
249 }
250 for (;;) {
251 struct cds_wfcq_head cbs_tmp_head;
252 struct cds_wfcq_tail cbs_tmp_tail;
253 struct cds_wfcq_node *cbs, *cbs_tmp_n;
254
255 cds_wfcq_init(&cbs_tmp_head, &cbs_tmp_tail);
256 __cds_wfcq_splice_blocking(&cbs_tmp_head, &cbs_tmp_tail,
257 &crdp->cbs_head, &crdp->cbs_tail);
258 if (!cds_wfcq_empty(&cbs_tmp_head, &cbs_tmp_tail)) {
259 synchronize_rcu();
260 cbcount = 0;
261 __cds_wfcq_for_each_blocking_safe(&cbs_tmp_head,
262 &cbs_tmp_tail, cbs, cbs_tmp_n) {
263 struct rcu_head *rhp;
264
265 rhp = caa_container_of(cbs,
266 struct rcu_head, next);
267 rhp->func(rhp);
268 cbcount++;
269 }
270 uatomic_sub(&crdp->qlen, cbcount);
271 }
272 if (uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOP)
273 break;
274 rcu_thread_offline();
275 if (!rt) {
276 if (cds_wfcq_empty(&crdp->cbs_head,
277 &crdp->cbs_tail)) {
278 call_rcu_wait(crdp);
279 poll(NULL, 0, 10);
280 uatomic_dec(&crdp->futex);
281 /*
282 * Decrement futex before reading
283 * call_rcu list.
284 */
285 cmm_smp_mb();
286 } else {
287 poll(NULL, 0, 10);
288 }
289 } else {
290 poll(NULL, 0, 10);
291 }
292 rcu_thread_online();
293 }
294 if (!rt) {
295 /*
296 * Read call_rcu list before write futex.
297 */
298 cmm_smp_mb();
299 uatomic_set(&crdp->futex, 0);
300 }
301 uatomic_or(&crdp->flags, URCU_CALL_RCU_STOPPED);
302 rcu_unregister_thread();
303 return NULL;
304 }
305
306 /*
307 * Create both a call_rcu thread and the corresponding call_rcu_data
308 * structure, linking the structure in as specified. Caller must hold
309 * call_rcu_mutex.
310 */
311
312 static void call_rcu_data_init(struct call_rcu_data **crdpp,
313 unsigned long flags,
314 int cpu_affinity)
315 {
316 struct call_rcu_data *crdp;
317 int ret;
318
319 crdp = malloc(sizeof(*crdp));
320 if (crdp == NULL)
321 urcu_die(errno);
322 memset(crdp, '\0', sizeof(*crdp));
323 cds_wfcq_init(&crdp->cbs_head, &crdp->cbs_tail);
324 crdp->qlen = 0;
325 crdp->futex = 0;
326 crdp->flags = flags;
327 cds_list_add(&crdp->list, &call_rcu_data_list);
328 crdp->cpu_affinity = cpu_affinity;
329 cmm_smp_mb(); /* Structure initialized before pointer is planted. */
330 *crdpp = crdp;
331 ret = pthread_create(&crdp->tid, NULL, call_rcu_thread, crdp);
332 if (ret)
333 urcu_die(ret);
334 }
335
336 /*
337 * Return a pointer to the call_rcu_data structure for the specified
338 * CPU, returning NULL if there is none. We cannot automatically
339 * created it because the platform we are running on might not define
340 * sched_getcpu().
341 *
342 * The call to this function and use of the returned call_rcu_data
343 * should be protected by RCU read-side lock.
344 */
345
346 struct call_rcu_data *get_cpu_call_rcu_data(int cpu)
347 {
348 static int warned = 0;
349 struct call_rcu_data **pcpu_crdp;
350
351 pcpu_crdp = rcu_dereference(per_cpu_call_rcu_data);
352 if (pcpu_crdp == NULL)
353 return NULL;
354 if (!warned && maxcpus > 0 && (cpu < 0 || maxcpus <= cpu)) {
355 fprintf(stderr, "[error] liburcu: get CPU # out of range\n");
356 warned = 1;
357 }
358 if (cpu < 0 || maxcpus <= cpu)
359 return NULL;
360 return rcu_dereference(pcpu_crdp[cpu]);
361 }
362
363 /*
364 * Return the tid corresponding to the call_rcu thread whose
365 * call_rcu_data structure is specified.
366 */
367
368 pthread_t get_call_rcu_thread(struct call_rcu_data *crdp)
369 {
370 return crdp->tid;
371 }
372
373 /*
374 * Create a call_rcu_data structure (with thread) and return a pointer.
375 */
376
377 static struct call_rcu_data *__create_call_rcu_data(unsigned long flags,
378 int cpu_affinity)
379 {
380 struct call_rcu_data *crdp;
381
382 call_rcu_data_init(&crdp, flags, cpu_affinity);
383 return crdp;
384 }
385
386 struct call_rcu_data *create_call_rcu_data(unsigned long flags,
387 int cpu_affinity)
388 {
389 struct call_rcu_data *crdp;
390
391 call_rcu_lock(&call_rcu_mutex);
392 crdp = __create_call_rcu_data(flags, cpu_affinity);
393 call_rcu_unlock(&call_rcu_mutex);
394 return crdp;
395 }
396
397 /*
398 * Set the specified CPU to use the specified call_rcu_data structure.
399 *
400 * Use NULL to remove a CPU's call_rcu_data structure, but it is
401 * the caller's responsibility to dispose of the removed structure.
402 * Use get_cpu_call_rcu_data() to obtain a pointer to the old structure
403 * (prior to NULLing it out, of course).
404 *
405 * The caller must wait for a grace-period to pass between return from
406 * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the
407 * previous call rcu data as argument.
408 */
409
410 int set_cpu_call_rcu_data(int cpu, struct call_rcu_data *crdp)
411 {
412 static int warned = 0;
413
414 call_rcu_lock(&call_rcu_mutex);
415 alloc_cpu_call_rcu_data();
416 if (cpu < 0 || maxcpus <= cpu) {
417 if (!warned) {
418 fprintf(stderr, "[error] liburcu: set CPU # out of range\n");
419 warned = 1;
420 }
421 call_rcu_unlock(&call_rcu_mutex);
422 errno = EINVAL;
423 return -EINVAL;
424 }
425
426 if (per_cpu_call_rcu_data == NULL) {
427 call_rcu_unlock(&call_rcu_mutex);
428 errno = ENOMEM;
429 return -ENOMEM;
430 }
431
432 if (per_cpu_call_rcu_data[cpu] != NULL && crdp != NULL) {
433 call_rcu_unlock(&call_rcu_mutex);
434 errno = EEXIST;
435 return -EEXIST;
436 }
437
438 rcu_set_pointer(&per_cpu_call_rcu_data[cpu], crdp);
439 call_rcu_unlock(&call_rcu_mutex);
440 return 0;
441 }
442
443 /*
444 * Return a pointer to the default call_rcu_data structure, creating
445 * one if need be. Because we never free call_rcu_data structures,
446 * we don't need to be in an RCU read-side critical section.
447 */
448
449 struct call_rcu_data *get_default_call_rcu_data(void)
450 {
451 if (default_call_rcu_data != NULL)
452 return rcu_dereference(default_call_rcu_data);
453 call_rcu_lock(&call_rcu_mutex);
454 if (default_call_rcu_data != NULL) {
455 call_rcu_unlock(&call_rcu_mutex);
456 return default_call_rcu_data;
457 }
458 call_rcu_data_init(&default_call_rcu_data, 0, -1);
459 call_rcu_unlock(&call_rcu_mutex);
460 return default_call_rcu_data;
461 }
462
463 /*
464 * Return the call_rcu_data structure that applies to the currently
465 * running thread. Any call_rcu_data structure assigned specifically
466 * to this thread has first priority, followed by any call_rcu_data
467 * structure assigned to the CPU on which the thread is running,
468 * followed by the default call_rcu_data structure. If there is not
469 * yet a default call_rcu_data structure, one will be created.
470 *
471 * Calls to this function and use of the returned call_rcu_data should
472 * be protected by RCU read-side lock.
473 */
474 struct call_rcu_data *get_call_rcu_data(void)
475 {
476 struct call_rcu_data *crd;
477
478 if (URCU_TLS(thread_call_rcu_data) != NULL)
479 return URCU_TLS(thread_call_rcu_data);
480
481 if (maxcpus > 0) {
482 crd = get_cpu_call_rcu_data(sched_getcpu());
483 if (crd)
484 return crd;
485 }
486
487 return get_default_call_rcu_data();
488 }
489
490 /*
491 * Return a pointer to this task's call_rcu_data if there is one.
492 */
493
494 struct call_rcu_data *get_thread_call_rcu_data(void)
495 {
496 return URCU_TLS(thread_call_rcu_data);
497 }
498
499 /*
500 * Set this task's call_rcu_data structure as specified, regardless
501 * of whether or not this task already had one. (This allows switching
502 * to and from real-time call_rcu threads, for example.)
503 *
504 * Use NULL to remove a thread's call_rcu_data structure, but it is
505 * the caller's responsibility to dispose of the removed structure.
506 * Use get_thread_call_rcu_data() to obtain a pointer to the old structure
507 * (prior to NULLing it out, of course).
508 */
509
510 void set_thread_call_rcu_data(struct call_rcu_data *crdp)
511 {
512 URCU_TLS(thread_call_rcu_data) = crdp;
513 }
514
515 /*
516 * Create a separate call_rcu thread for each CPU. This does not
517 * replace a pre-existing call_rcu thread -- use the set_cpu_call_rcu_data()
518 * function if you want that behavior. Should be paired with
519 * free_all_cpu_call_rcu_data() to teardown these call_rcu worker
520 * threads.
521 */
522
523 int create_all_cpu_call_rcu_data(unsigned long flags)
524 {
525 int i;
526 struct call_rcu_data *crdp;
527 int ret;
528
529 call_rcu_lock(&call_rcu_mutex);
530 alloc_cpu_call_rcu_data();
531 call_rcu_unlock(&call_rcu_mutex);
532 if (maxcpus <= 0) {
533 errno = EINVAL;
534 return -EINVAL;
535 }
536 if (per_cpu_call_rcu_data == NULL) {
537 errno = ENOMEM;
538 return -ENOMEM;
539 }
540 for (i = 0; i < maxcpus; i++) {
541 call_rcu_lock(&call_rcu_mutex);
542 if (get_cpu_call_rcu_data(i)) {
543 call_rcu_unlock(&call_rcu_mutex);
544 continue;
545 }
546 crdp = __create_call_rcu_data(flags, i);
547 if (crdp == NULL) {
548 call_rcu_unlock(&call_rcu_mutex);
549 errno = ENOMEM;
550 return -ENOMEM;
551 }
552 call_rcu_unlock(&call_rcu_mutex);
553 if ((ret = set_cpu_call_rcu_data(i, crdp)) != 0) {
554 call_rcu_data_free(crdp);
555
556 /* it has been created by other thread */
557 if (ret == -EEXIST)
558 continue;
559
560 return ret;
561 }
562 }
563 return 0;
564 }
565
566 /*
567 * Wake up the call_rcu thread corresponding to the specified
568 * call_rcu_data structure.
569 */
570 static void wake_call_rcu_thread(struct call_rcu_data *crdp)
571 {
572 if (!(_CMM_LOAD_SHARED(crdp->flags) & URCU_CALL_RCU_RT))
573 call_rcu_wake_up(crdp);
574 }
575
576 /*
577 * Schedule a function to be invoked after a following grace period.
578 * This is the only function that must be called -- the others are
579 * only present to allow applications to tune their use of RCU for
580 * maximum performance.
581 *
582 * Note that unless a call_rcu thread has not already been created,
583 * the first invocation of call_rcu() will create one. So, if you
584 * need the first invocation of call_rcu() to be fast, make sure
585 * to create a call_rcu thread first. One way to accomplish this is
586 * "get_call_rcu_data();", and another is create_all_cpu_call_rcu_data().
587 *
588 * call_rcu must be called by registered RCU read-side threads.
589 */
590
591 void call_rcu(struct rcu_head *head,
592 void (*func)(struct rcu_head *head))
593 {
594 struct call_rcu_data *crdp;
595
596 cds_wfcq_node_init(&head->next);
597 head->func = func;
598 /* Holding rcu read-side lock across use of per-cpu crdp */
599 rcu_read_lock();
600 crdp = get_call_rcu_data();
601 cds_wfcq_enqueue(&crdp->cbs_head, &crdp->cbs_tail, &head->next);
602 uatomic_inc(&crdp->qlen);
603 wake_call_rcu_thread(crdp);
604 rcu_read_unlock();
605 }
606
607 /*
608 * Free up the specified call_rcu_data structure, terminating the
609 * associated call_rcu thread. The caller must have previously
610 * removed the call_rcu_data structure from per-thread or per-CPU
611 * usage. For example, set_cpu_call_rcu_data(cpu, NULL) for per-CPU
612 * call_rcu_data structures or set_thread_call_rcu_data(NULL) for
613 * per-thread call_rcu_data structures.
614 *
615 * We silently refuse to free up the default call_rcu_data structure
616 * because that is where we put any leftover callbacks. Note that
617 * the possibility of self-spawning callbacks makes it impossible
618 * to execute all the callbacks in finite time without putting any
619 * newly spawned callbacks somewhere else. The "somewhere else" of
620 * last resort is the default call_rcu_data structure.
621 *
622 * We also silently refuse to free NULL pointers. This simplifies
623 * the calling code.
624 *
625 * The caller must wait for a grace-period to pass between return from
626 * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the
627 * previous call rcu data as argument.
628 */
629 void call_rcu_data_free(struct call_rcu_data *crdp)
630 {
631 if (crdp == NULL || crdp == default_call_rcu_data) {
632 return;
633 }
634 if ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOPPED) == 0) {
635 uatomic_or(&crdp->flags, URCU_CALL_RCU_STOP);
636 wake_call_rcu_thread(crdp);
637 while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOPPED) == 0)
638 poll(NULL, 0, 1);
639 }
640 if (!cds_wfcq_empty(&crdp->cbs_head, &crdp->cbs_tail)) {
641 /* Create default call rcu data if need be */
642 (void) get_default_call_rcu_data();
643 __cds_wfcq_splice_blocking(&default_call_rcu_data->cbs_head,
644 &default_call_rcu_data->cbs_tail,
645 &crdp->cbs_head, &crdp->cbs_tail);
646 uatomic_add(&default_call_rcu_data->qlen,
647 uatomic_read(&crdp->qlen));
648 wake_call_rcu_thread(default_call_rcu_data);
649 }
650
651 call_rcu_lock(&call_rcu_mutex);
652 cds_list_del(&crdp->list);
653 call_rcu_unlock(&call_rcu_mutex);
654
655 free(crdp);
656 }
657
658 /*
659 * Clean up all the per-CPU call_rcu threads.
660 */
661 void free_all_cpu_call_rcu_data(void)
662 {
663 int cpu;
664 struct call_rcu_data **crdp;
665 static int warned = 0;
666
667 if (maxcpus <= 0)
668 return;
669
670 crdp = malloc(sizeof(*crdp) * maxcpus);
671 if (!crdp) {
672 if (!warned) {
673 fprintf(stderr, "[error] liburcu: unable to allocate per-CPU pointer array\n");
674 }
675 warned = 1;
676 return;
677 }
678
679 for (cpu = 0; cpu < maxcpus; cpu++) {
680 crdp[cpu] = get_cpu_call_rcu_data(cpu);
681 if (crdp[cpu] == NULL)
682 continue;
683 set_cpu_call_rcu_data(cpu, NULL);
684 }
685 /*
686 * Wait for call_rcu sites acting as RCU readers of the
687 * call_rcu_data to become quiescent.
688 */
689 synchronize_rcu();
690 for (cpu = 0; cpu < maxcpus; cpu++) {
691 if (crdp[cpu] == NULL)
692 continue;
693 call_rcu_data_free(crdp[cpu]);
694 }
695 free(crdp);
696 }
697
698 /*
699 * Acquire the call_rcu_mutex in order to ensure that the child sees
700 * all of the call_rcu() data structures in a consistent state.
701 * Suitable for pthread_atfork() and friends.
702 */
703 void call_rcu_before_fork(void)
704 {
705 call_rcu_lock(&call_rcu_mutex);
706 }
707
708 /*
709 * Clean up call_rcu data structures in the parent of a successful fork()
710 * that is not followed by exec() in the child. Suitable for
711 * pthread_atfork() and friends.
712 */
713 void call_rcu_after_fork_parent(void)
714 {
715 call_rcu_unlock(&call_rcu_mutex);
716 }
717
718 /*
719 * Clean up call_rcu data structures in the child of a successful fork()
720 * that is not followed by exec(). Suitable for pthread_atfork() and
721 * friends.
722 */
723 void call_rcu_after_fork_child(void)
724 {
725 struct call_rcu_data *crdp, *next;
726
727 /* Release the mutex. */
728 call_rcu_unlock(&call_rcu_mutex);
729
730 /* Do nothing when call_rcu() has not been used */
731 if (cds_list_empty(&call_rcu_data_list))
732 return;
733
734 /*
735 * Allocate a new default call_rcu_data structure in order
736 * to get a working call_rcu thread to go with it.
737 */
738 default_call_rcu_data = NULL;
739 (void)get_default_call_rcu_data();
740
741 /* Cleanup call_rcu_data pointers before use */
742 maxcpus_reset();
743 free(per_cpu_call_rcu_data);
744 rcu_set_pointer(&per_cpu_call_rcu_data, NULL);
745 URCU_TLS(thread_call_rcu_data) = NULL;
746
747 /* Dispose of all of the rest of the call_rcu_data structures. */
748 cds_list_for_each_entry_safe(crdp, next, &call_rcu_data_list, list) {
749 if (crdp == default_call_rcu_data)
750 continue;
751 uatomic_set(&crdp->flags, URCU_CALL_RCU_STOPPED);
752 call_rcu_data_free(crdp);
753 }
754 }
This page took 0.044163 seconds and 5 git commands to generate.