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