call_rcu: use wfcqueue, eliminate false-sharing
[userspace-rcu.git] / urcu-call-rcu-impl.h
CommitLineData
b57aee66
PM
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
c1d2c60b 23#define _GNU_SOURCE
5161f31e 24#define _LGPL_SOURCE
b57aee66
PM
25#include <stdio.h>
26#include <pthread.h>
27#include <signal.h>
28#include <assert.h>
29#include <stdlib.h>
6d841bc2 30#include <stdint.h>
b57aee66
PM
31#include <string.h>
32#include <errno.h>
33#include <poll.h>
34#include <sys/time.h>
b57aee66 35#include <unistd.h>
c1d2c60b 36#include <sched.h>
b57aee66
PM
37
38#include "config.h"
5161f31e 39#include "urcu/wfcqueue.h"
b57aee66
PM
40#include "urcu-call-rcu.h"
41#include "urcu-pointer.h"
3c24913f 42#include "urcu/list.h"
41849996 43#include "urcu/futex.h"
bd252a04 44#include "urcu/tls-compat.h"
4a6d7378 45#include "urcu-die.h"
b57aee66
PM
46
47/* Data structure that identifies a call_rcu thread. */
48
49struct call_rcu_data {
5161f31e
MD
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;
b57aee66 58 unsigned long flags;
6d841bc2 59 int32_t futex;
73987721 60 unsigned long qlen; /* maintained for debugging. */
b57aee66 61 pthread_t tid;
c1d2c60b 62 int cpu_affinity;
3c24913f 63 struct cds_list_head list;
b57aee66
PM
64} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
65
3c24913f
PM
66/*
67 * List of all call_rcu_data structures to keep valgrind happy.
68 * Protected by call_rcu_mutex.
69 */
70
71CDS_LIST_HEAD(call_rcu_data_list);
72
b57aee66
PM
73/* Link a thread using call_rcu() to its call_rcu thread. */
74
bd252a04 75static DEFINE_URCU_TLS(struct call_rcu_data *, thread_call_rcu_data);
b57aee66
PM
76
77/* Guard call_rcu thread creation. */
78
79static 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
83static struct call_rcu_data *default_call_rcu_data;
84
b57aee66
PM
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
618b2595
MD
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.
b57aee66
PM
99 */
100
101static struct call_rcu_data **per_cpu_call_rcu_data;
102static long maxcpus;
103
60af049d
LJ
104static void maxcpus_reset(void)
105{
106 maxcpus = 0;
107}
108
b57aee66
PM
109/* Allocate the array if it has not already been allocated. */
110
111static 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));
618b2595 125 rcu_set_pointer(&per_cpu_call_rcu_data, p);
b57aee66
PM
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
f9437098
MD
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 */
141static struct call_rcu_data **per_cpu_call_rcu_data = NULL;
b57aee66
PM
142static const long maxcpus = -1;
143
60af049d
LJ
144static void maxcpus_reset(void)
145{
146}
147
b57aee66
PM
148static void alloc_cpu_call_rcu_data(void)
149{
150}
151
152static 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
161static void call_rcu_lock(pthread_mutex_t *pmp)
162{
4a6d7378
MD
163 int ret;
164
165 ret = pthread_mutex_lock(pmp);
166 if (ret)
167 urcu_die(ret);
b57aee66
PM
168}
169
170/* Release the specified pthread mutex. */
171
172static void call_rcu_unlock(pthread_mutex_t *pmp)
173{
4a6d7378
MD
174 int ret;
175
176 ret = pthread_mutex_unlock(pmp);
177 if (ret)
178 urcu_die(ret);
b57aee66
PM
179}
180
c1d2c60b
MD
181#if HAVE_SCHED_SETAFFINITY
182static
183int 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
199static
200int set_thread_cpu_affinity(struct call_rcu_data *crdp)
201{
202 return 0;
203}
204#endif
205
03fe58b3
MD
206static 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
215static 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();
a0b7f7ea 219 if (caa_unlikely(uatomic_read(&crdp->futex) == -1)) {
03fe58b3
MD
220 uatomic_set(&crdp->futex, 0);
221 futex_async(&crdp->futex, FUTEX_WAKE, 1,
222 NULL, NULL, 0);
223 }
224}
225
b57aee66
PM
226/* This is the code run by each call_rcu thread. */
227
228static void *call_rcu_thread(void *arg)
229{
230 unsigned long cbcount;
5161f31e 231 struct call_rcu_data *crdp = (struct call_rcu_data *) arg;
2870aa1e 232 int rt = !!(uatomic_read(&crdp->flags) & URCU_CALL_RCU_RT);
4a6d7378 233 int ret;
b57aee66 234
4a6d7378
MD
235 ret = set_thread_cpu_affinity(crdp);
236 if (ret)
237 urcu_die(errno);
c1d2c60b 238
765f3ead
MD
239 /*
240 * If callbacks take a read-side lock, we need to be registered.
241 */
242 rcu_register_thread();
243
bd252a04 244 URCU_TLS(thread_call_rcu_data) = crdp;
bc94ca9b
MD
245 if (!rt) {
246 uatomic_dec(&crdp->futex);
247 /* Decrement futex before reading call_rcu list */
248 cmm_smp_mb();
249 }
b57aee66 250 for (;;) {
5161f31e
MD
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)) {
b57aee66
PM
259 synchronize_rcu();
260 cbcount = 0;
5161f31e
MD
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);
b57aee66
PM
267 rhp->func(rhp);
268 cbcount++;
5161f31e 269 }
b57aee66
PM
270 uatomic_sub(&crdp->qlen, cbcount);
271 }
bc94ca9b
MD
272 if (uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOP)
273 break;
765f3ead 274 rcu_thread_offline();
bc94ca9b 275 if (!rt) {
5161f31e
MD
276 if (cds_wfcq_empty(&crdp->cbs_head,
277 &crdp->cbs_tail)) {
bc94ca9b
MD
278 call_rcu_wait(crdp);
279 poll(NULL, 0, 10);
280 uatomic_dec(&crdp->futex);
c768e45e 281 /*
bc94ca9b
MD
282 * Decrement futex before reading
283 * call_rcu list.
c768e45e
MD
284 */
285 cmm_smp_mb();
ccbac24d
MD
286 } else {
287 poll(NULL, 0, 10);
c768e45e 288 }
bc94ca9b
MD
289 } else {
290 poll(NULL, 0, 10);
b57aee66 291 }
765f3ead 292 rcu_thread_online();
bc94ca9b
MD
293 }
294 if (!rt) {
295 /*
296 * Read call_rcu list before write futex.
297 */
298 cmm_smp_mb();
299 uatomic_set(&crdp->futex, 0);
b57aee66 300 }
2870aa1e 301 uatomic_or(&crdp->flags, URCU_CALL_RCU_STOPPED);
765f3ead 302 rcu_unregister_thread();
7106ddf8 303 return NULL;
b57aee66
PM
304}
305
306/*
307 * Create both a call_rcu thread and the corresponding call_rcu_data
3c24913f
PM
308 * structure, linking the structure in as specified. Caller must hold
309 * call_rcu_mutex.
b57aee66
PM
310 */
311
3c24913f 312static void call_rcu_data_init(struct call_rcu_data **crdpp,
c1d2c60b
MD
313 unsigned long flags,
314 int cpu_affinity)
b57aee66
PM
315{
316 struct call_rcu_data *crdp;
4a6d7378 317 int ret;
b57aee66
PM
318
319 crdp = malloc(sizeof(*crdp));
4a6d7378
MD
320 if (crdp == NULL)
321 urcu_die(errno);
b57aee66 322 memset(crdp, '\0', sizeof(*crdp));
5161f31e 323 cds_wfcq_init(&crdp->cbs_head, &crdp->cbs_tail);
b57aee66 324 crdp->qlen = 0;
263e3cf9
MD
325 crdp->futex = 0;
326 crdp->flags = flags;
3c24913f 327 cds_list_add(&crdp->list, &call_rcu_data_list);
c1d2c60b 328 crdp->cpu_affinity = cpu_affinity;
b57aee66
PM
329 cmm_smp_mb(); /* Structure initialized before pointer is planted. */
330 *crdpp = crdp;
4a6d7378
MD
331 ret = pthread_create(&crdp->tid, NULL, call_rcu_thread, crdp);
332 if (ret)
333 urcu_die(ret);
b57aee66
PM
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().
618b2595
MD
341 *
342 * The call to this function and use of the returned call_rcu_data
343 * should be protected by RCU read-side lock.
b57aee66
PM
344 */
345
346struct call_rcu_data *get_cpu_call_rcu_data(int cpu)
347{
348 static int warned = 0;
618b2595 349 struct call_rcu_data **pcpu_crdp;
b57aee66 350
618b2595
MD
351 pcpu_crdp = rcu_dereference(per_cpu_call_rcu_data);
352 if (pcpu_crdp == NULL)
b57aee66
PM
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;
618b2595 360 return rcu_dereference(pcpu_crdp[cpu]);
b57aee66
PM
361}
362
363/*
364 * Return the tid corresponding to the call_rcu thread whose
365 * call_rcu_data structure is specified.
366 */
367
368pthread_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
c1d2c60b
MD
377static struct call_rcu_data *__create_call_rcu_data(unsigned long flags,
378 int cpu_affinity)
b57aee66
PM
379{
380 struct call_rcu_data *crdp;
381
c1d2c60b 382 call_rcu_data_init(&crdp, flags, cpu_affinity);
b57aee66
PM
383 return crdp;
384}
385
c1d2c60b
MD
386struct call_rcu_data *create_call_rcu_data(unsigned long flags,
387 int cpu_affinity)
3c24913f
PM
388{
389 struct call_rcu_data *crdp;
390
391 call_rcu_lock(&call_rcu_mutex);
c1d2c60b 392 crdp = __create_call_rcu_data(flags, cpu_affinity);
3c24913f
PM
393 call_rcu_unlock(&call_rcu_mutex);
394 return crdp;
395}
396
b57aee66
PM
397/*
398 * Set the specified CPU to use the specified call_rcu_data structure.
7106ddf8
PM
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).
f9da0936
MD
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.
b57aee66
PM
408 */
409
410int set_cpu_call_rcu_data(int cpu, struct call_rcu_data *crdp)
411{
dcfc8165 412 static int warned = 0;
b57aee66
PM
413
414 call_rcu_lock(&call_rcu_mutex);
f3776786 415 alloc_cpu_call_rcu_data();
b57aee66
PM
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 }
53a55535 425
b57aee66 426 if (per_cpu_call_rcu_data == NULL) {
3670fef2 427 call_rcu_unlock(&call_rcu_mutex);
b57aee66
PM
428 errno = ENOMEM;
429 return -ENOMEM;
430 }
53a55535
LJ
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
618b2595 438 rcu_set_pointer(&per_cpu_call_rcu_data[cpu], crdp);
3670fef2 439 call_rcu_unlock(&call_rcu_mutex);
b57aee66
PM
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
449struct 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 }
c1d2c60b 458 call_rcu_data_init(&default_call_rcu_data, 0, -1);
b57aee66
PM
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.
618b2595
MD
470 *
471 * Calls to this function and use of the returned call_rcu_data should
472 * be protected by RCU read-side lock.
b57aee66
PM
473 */
474struct call_rcu_data *get_call_rcu_data(void)
475{
9744f3bb 476 struct call_rcu_data *crd;
b57aee66 477
bd252a04
MD
478 if (URCU_TLS(thread_call_rcu_data) != NULL)
479 return URCU_TLS(thread_call_rcu_data);
9744f3bb
LJ
480
481 if (maxcpus > 0) {
482 crd = get_cpu_call_rcu_data(sched_getcpu());
483 if (crd)
484 return crd;
b57aee66 485 }
9744f3bb 486
b57aee66
PM
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
494struct call_rcu_data *get_thread_call_rcu_data(void)
495{
bd252a04 496 return URCU_TLS(thread_call_rcu_data);
b57aee66
PM
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.)
7106ddf8
PM
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).
b57aee66
PM
508 */
509
510void set_thread_call_rcu_data(struct call_rcu_data *crdp)
511{
bd252a04 512 URCU_TLS(thread_call_rcu_data) = crdp;
b57aee66
PM
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()
0938c541
MD
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.
b57aee66
PM
521 */
522
523int 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 }
c1d2c60b 546 crdp = __create_call_rcu_data(flags, i);
b57aee66
PM
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) {
356c8794
LJ
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;
b57aee66
PM
561 }
562 }
563 return 0;
564}
565
7106ddf8
PM
566/*
567 * Wake up the call_rcu thread corresponding to the specified
568 * call_rcu_data structure.
569 */
570static void wake_call_rcu_thread(struct call_rcu_data *crdp)
571{
263e3cf9
MD
572 if (!(_CMM_LOAD_SHARED(crdp->flags) & URCU_CALL_RCU_RT))
573 call_rcu_wake_up(crdp);
7106ddf8
PM
574}
575
b57aee66
PM
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().
618b2595
MD
587 *
588 * call_rcu must be called by registered RCU read-side threads.
b57aee66
PM
589 */
590
591void call_rcu(struct rcu_head *head,
592 void (*func)(struct rcu_head *head))
593{
594 struct call_rcu_data *crdp;
595
5161f31e 596 cds_wfcq_node_init(&head->next);
b57aee66 597 head->func = func;
618b2595
MD
598 /* Holding rcu read-side lock across use of per-cpu crdp */
599 rcu_read_lock();
b57aee66 600 crdp = get_call_rcu_data();
5161f31e 601 cds_wfcq_enqueue(&crdp->cbs_head, &crdp->cbs_tail, &head->next);
b57aee66 602 uatomic_inc(&crdp->qlen);
7106ddf8 603 wake_call_rcu_thread(crdp);
618b2595 604 rcu_read_unlock();
7106ddf8
PM
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.
f9da0936
MD
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.
7106ddf8
PM
628 */
629void call_rcu_data_free(struct call_rcu_data *crdp)
630{
7106ddf8
PM
631 if (crdp == NULL || crdp == default_call_rcu_data) {
632 return;
633 }
2870aa1e
PB
634 if ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOPPED) == 0) {
635 uatomic_or(&crdp->flags, URCU_CALL_RCU_STOP);
7106ddf8 636 wake_call_rcu_thread(crdp);
2870aa1e 637 while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOPPED) == 0)
7106ddf8
PM
638 poll(NULL, 0, 1);
639 }
5161f31e 640 if (!cds_wfcq_empty(&crdp->cbs_head, &crdp->cbs_tail)) {
698d0778
MD
641 /* Create default call rcu data if need be */
642 (void) get_default_call_rcu_data();
5161f31e
MD
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);
7106ddf8
PM
646 uatomic_add(&default_call_rcu_data->qlen,
647 uatomic_read(&crdp->qlen));
1e92aa15 648 wake_call_rcu_thread(default_call_rcu_data);
7106ddf8 649 }
59dc9e9d 650
b75dffe6 651 call_rcu_lock(&call_rcu_mutex);
59dc9e9d 652 cds_list_del(&crdp->list);
b75dffe6
LJ
653 call_rcu_unlock(&call_rcu_mutex);
654
59dc9e9d 655 free(crdp);
7106ddf8
PM
656}
657
658/*
659 * Clean up all the per-CPU call_rcu threads.
660 */
661void free_all_cpu_call_rcu_data(void)
662{
663 int cpu;
618b2595
MD
664 struct call_rcu_data **crdp;
665 static int warned = 0;
7106ddf8
PM
666
667 if (maxcpus <= 0)
668 return;
618b2595
MD
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;
d31150b4 676 return;
618b2595
MD
677 }
678
7106ddf8 679 for (cpu = 0; cpu < maxcpus; cpu++) {
618b2595
MD
680 crdp[cpu] = get_cpu_call_rcu_data(cpu);
681 if (crdp[cpu] == NULL)
7106ddf8
PM
682 continue;
683 set_cpu_call_rcu_data(cpu, NULL);
7106ddf8 684 }
618b2595
MD
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);
7106ddf8
PM
696}
697
81ad2e19
PM
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 */
703void 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 */
713void call_rcu_after_fork_parent(void)
714{
715 call_rcu_unlock(&call_rcu_mutex);
716}
717
7106ddf8
PM
718/*
719 * Clean up call_rcu data structures in the child of a successful fork()
81ad2e19
PM
720 * that is not followed by exec(). Suitable for pthread_atfork() and
721 * friends.
7106ddf8
PM
722 */
723void call_rcu_after_fork_child(void)
724{
077ff173 725 struct call_rcu_data *crdp, *next;
7106ddf8 726
81ad2e19
PM
727 /* Release the mutex. */
728 call_rcu_unlock(&call_rcu_mutex);
729
ad1b9909
LJ
730 /* Do nothing when call_rcu() has not been used */
731 if (cds_list_empty(&call_rcu_data_list))
732 return;
733
7106ddf8
PM
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
60af049d
LJ
741 /* Cleanup call_rcu_data pointers before use */
742 maxcpus_reset();
743 free(per_cpu_call_rcu_data);
618b2595 744 rcu_set_pointer(&per_cpu_call_rcu_data, NULL);
bd252a04 745 URCU_TLS(thread_call_rcu_data) = NULL;
60af049d 746
7106ddf8 747 /* Dispose of all of the rest of the call_rcu_data structures. */
077ff173 748 cds_list_for_each_entry_safe(crdp, next, &call_rcu_data_list, list) {
7106ddf8 749 if (crdp == default_call_rcu_data)
077ff173 750 continue;
2870aa1e 751 uatomic_set(&crdp->flags, URCU_CALL_RCU_STOPPED);
7106ddf8 752 call_rcu_data_free(crdp);
b57aee66
PM
753 }
754}
This page took 0.05818 seconds and 4 git commands to generate.