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