rculfhash: document destroy context limitations
[urcu.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 50 /*
0b8ab7df
MD
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.
5161f31e 56 */
b9f893b6 57 struct cds_wfcq_tail cbs_tail;
0b8ab7df 58 struct cds_wfcq_head cbs_head;
b57aee66 59 unsigned long flags;
6d841bc2 60 int32_t futex;
73987721 61 unsigned long qlen; /* maintained for debugging. */
b57aee66 62 pthread_t tid;
c1d2c60b 63 int cpu_affinity;
3c24913f 64 struct cds_list_head list;
b57aee66
PM
65} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
66
3c24913f
PM
67/*
68 * List of all call_rcu_data structures to keep valgrind happy.
69 * Protected by call_rcu_mutex.
70 */
71
bab44e28 72static CDS_LIST_HEAD(call_rcu_data_list);
3c24913f 73
b57aee66
PM
74/* Link a thread using call_rcu() to its call_rcu thread. */
75
bd252a04 76static DEFINE_URCU_TLS(struct call_rcu_data *, thread_call_rcu_data);
b57aee66 77
e85451a1
MD
78/*
79 * Guard call_rcu thread creation and atfork handlers.
80 */
b57aee66
PM
81static 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
85static struct call_rcu_data *default_call_rcu_data;
86
b57aee66
PM
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
63b495d8
MD
93#ifdef HAVE_SCHED_GETCPU
94
95static int urcu_sched_getcpu(void)
96{
97 return sched_getcpu();
98}
99
100#else /* #ifdef HAVE_SCHED_GETCPU */
101
102static 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)
b57aee66
PM
110
111/*
112 * Pointer to array of pointers to per-CPU call_rcu_data structures
618b2595
MD
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.
b57aee66
PM
117 */
118
119static struct call_rcu_data **per_cpu_call_rcu_data;
120static long maxcpus;
121
60af049d
LJ
122static void maxcpus_reset(void)
123{
124 maxcpus = 0;
125}
126
b57aee66
PM
127/* Allocate the array if it has not already been allocated. */
128
129static 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));
618b2595 143 rcu_set_pointer(&per_cpu_call_rcu_data, p);
b57aee66
PM
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
63b495d8 152#else /* #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU) */
b57aee66 153
f9437098
MD
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 */
159static struct call_rcu_data **per_cpu_call_rcu_data = NULL;
b57aee66
PM
160static const long maxcpus = -1;
161
60af049d
LJ
162static void maxcpus_reset(void)
163{
164}
165
b57aee66
PM
166static void alloc_cpu_call_rcu_data(void)
167{
168}
169
63b495d8 170#endif /* #else #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU) */
b57aee66
PM
171
172/* Acquire the specified pthread mutex. */
173
174static void call_rcu_lock(pthread_mutex_t *pmp)
175{
4a6d7378
MD
176 int ret;
177
178 ret = pthread_mutex_lock(pmp);
179 if (ret)
180 urcu_die(ret);
b57aee66
PM
181}
182
183/* Release the specified pthread mutex. */
184
185static void call_rcu_unlock(pthread_mutex_t *pmp)
186{
4a6d7378
MD
187 int ret;
188
189 ret = pthread_mutex_unlock(pmp);
190 if (ret)
191 urcu_die(ret);
b57aee66
PM
192}
193
c1d2c60b
MD
194#if HAVE_SCHED_SETAFFINITY
195static
196int 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
212static
213int set_thread_cpu_affinity(struct call_rcu_data *crdp)
214{
215 return 0;
216}
217#endif
218
03fe58b3
MD
219static 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
228static 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();
a0b7f7ea 232 if (caa_unlikely(uatomic_read(&crdp->futex) == -1)) {
03fe58b3
MD
233 uatomic_set(&crdp->futex, 0);
234 futex_async(&crdp->futex, FUTEX_WAKE, 1,
235 NULL, NULL, 0);
236 }
237}
238
b57aee66
PM
239/* This is the code run by each call_rcu thread. */
240
241static void *call_rcu_thread(void *arg)
242{
243 unsigned long cbcount;
5161f31e 244 struct call_rcu_data *crdp = (struct call_rcu_data *) arg;
2870aa1e 245 int rt = !!(uatomic_read(&crdp->flags) & URCU_CALL_RCU_RT);
4a6d7378 246 int ret;
b57aee66 247
4a6d7378
MD
248 ret = set_thread_cpu_affinity(crdp);
249 if (ret)
250 urcu_die(errno);
c1d2c60b 251
765f3ead
MD
252 /*
253 * If callbacks take a read-side lock, we need to be registered.
254 */
255 rcu_register_thread();
256
bd252a04 257 URCU_TLS(thread_call_rcu_data) = crdp;
bc94ca9b
MD
258 if (!rt) {
259 uatomic_dec(&crdp->futex);
260 /* Decrement futex before reading call_rcu list */
261 cmm_smp_mb();
262 }
b57aee66 263 for (;;) {
5161f31e
MD
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;
ae25b7e2 267 enum cds_wfcq_ret splice_ret;
5161f31e 268
e85451a1
MD
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
5161f31e 284 cds_wfcq_init(&cbs_tmp_head, &cbs_tmp_tail);
ae25b7e2
MD
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) {
b57aee66
PM
290 synchronize_rcu();
291 cbcount = 0;
5161f31e
MD
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);
b57aee66
PM
298 rhp->func(rhp);
299 cbcount++;
5161f31e 300 }
b57aee66
PM
301 uatomic_sub(&crdp->qlen, cbcount);
302 }
bc94ca9b
MD
303 if (uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOP)
304 break;
765f3ead 305 rcu_thread_offline();
bc94ca9b 306 if (!rt) {
5161f31e
MD
307 if (cds_wfcq_empty(&crdp->cbs_head,
308 &crdp->cbs_tail)) {
bc94ca9b
MD
309 call_rcu_wait(crdp);
310 poll(NULL, 0, 10);
311 uatomic_dec(&crdp->futex);
c768e45e 312 /*
bc94ca9b
MD
313 * Decrement futex before reading
314 * call_rcu list.
c768e45e
MD
315 */
316 cmm_smp_mb();
ccbac24d
MD
317 } else {
318 poll(NULL, 0, 10);
c768e45e 319 }
bc94ca9b
MD
320 } else {
321 poll(NULL, 0, 10);
b57aee66 322 }
765f3ead 323 rcu_thread_online();
bc94ca9b
MD
324 }
325 if (!rt) {
326 /*
327 * Read call_rcu list before write futex.
328 */
329 cmm_smp_mb();
330 uatomic_set(&crdp->futex, 0);
b57aee66 331 }
2870aa1e 332 uatomic_or(&crdp->flags, URCU_CALL_RCU_STOPPED);
765f3ead 333 rcu_unregister_thread();
7106ddf8 334 return NULL;
b57aee66
PM
335}
336
337/*
338 * Create both a call_rcu thread and the corresponding call_rcu_data
3c24913f
PM
339 * structure, linking the structure in as specified. Caller must hold
340 * call_rcu_mutex.
b57aee66
PM
341 */
342
3c24913f 343static void call_rcu_data_init(struct call_rcu_data **crdpp,
c1d2c60b
MD
344 unsigned long flags,
345 int cpu_affinity)
b57aee66
PM
346{
347 struct call_rcu_data *crdp;
4a6d7378 348 int ret;
b57aee66
PM
349
350 crdp = malloc(sizeof(*crdp));
4a6d7378
MD
351 if (crdp == NULL)
352 urcu_die(errno);
b57aee66 353 memset(crdp, '\0', sizeof(*crdp));
5161f31e 354 cds_wfcq_init(&crdp->cbs_head, &crdp->cbs_tail);
b57aee66 355 crdp->qlen = 0;
263e3cf9
MD
356 crdp->futex = 0;
357 crdp->flags = flags;
3c24913f 358 cds_list_add(&crdp->list, &call_rcu_data_list);
c1d2c60b 359 crdp->cpu_affinity = cpu_affinity;
b57aee66
PM
360 cmm_smp_mb(); /* Structure initialized before pointer is planted. */
361 *crdpp = crdp;
4a6d7378
MD
362 ret = pthread_create(&crdp->tid, NULL, call_rcu_thread, crdp);
363 if (ret)
364 urcu_die(ret);
b57aee66
PM
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
63b495d8 371 * urcu_sched_getcpu().
618b2595
MD
372 *
373 * The call to this function and use of the returned call_rcu_data
374 * should be protected by RCU read-side lock.
b57aee66
PM
375 */
376
377struct call_rcu_data *get_cpu_call_rcu_data(int cpu)
378{
379 static int warned = 0;
618b2595 380 struct call_rcu_data **pcpu_crdp;
b57aee66 381
618b2595
MD
382 pcpu_crdp = rcu_dereference(per_cpu_call_rcu_data);
383 if (pcpu_crdp == NULL)
b57aee66
PM
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;
618b2595 391 return rcu_dereference(pcpu_crdp[cpu]);
b57aee66
PM
392}
393
394/*
395 * Return the tid corresponding to the call_rcu thread whose
396 * call_rcu_data structure is specified.
397 */
398
399pthread_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
c1d2c60b
MD
408static struct call_rcu_data *__create_call_rcu_data(unsigned long flags,
409 int cpu_affinity)
b57aee66
PM
410{
411 struct call_rcu_data *crdp;
412
c1d2c60b 413 call_rcu_data_init(&crdp, flags, cpu_affinity);
b57aee66
PM
414 return crdp;
415}
416
c1d2c60b
MD
417struct call_rcu_data *create_call_rcu_data(unsigned long flags,
418 int cpu_affinity)
3c24913f
PM
419{
420 struct call_rcu_data *crdp;
421
422 call_rcu_lock(&call_rcu_mutex);
c1d2c60b 423 crdp = __create_call_rcu_data(flags, cpu_affinity);
3c24913f
PM
424 call_rcu_unlock(&call_rcu_mutex);
425 return crdp;
426}
427
b57aee66
PM
428/*
429 * Set the specified CPU to use the specified call_rcu_data structure.
7106ddf8
PM
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).
f9da0936
MD
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.
b57aee66
PM
439 */
440
441int set_cpu_call_rcu_data(int cpu, struct call_rcu_data *crdp)
442{
dcfc8165 443 static int warned = 0;
b57aee66
PM
444
445 call_rcu_lock(&call_rcu_mutex);
f3776786 446 alloc_cpu_call_rcu_data();
b57aee66
PM
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 }
53a55535 456
b57aee66 457 if (per_cpu_call_rcu_data == NULL) {
3670fef2 458 call_rcu_unlock(&call_rcu_mutex);
b57aee66
PM
459 errno = ENOMEM;
460 return -ENOMEM;
461 }
53a55535
LJ
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
618b2595 469 rcu_set_pointer(&per_cpu_call_rcu_data[cpu], crdp);
3670fef2 470 call_rcu_unlock(&call_rcu_mutex);
b57aee66
PM
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
480struct 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 }
c1d2c60b 489 call_rcu_data_init(&default_call_rcu_data, 0, -1);
b57aee66
PM
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.
618b2595
MD
501 *
502 * Calls to this function and use of the returned call_rcu_data should
503 * be protected by RCU read-side lock.
b57aee66
PM
504 */
505struct call_rcu_data *get_call_rcu_data(void)
506{
9744f3bb 507 struct call_rcu_data *crd;
b57aee66 508
bd252a04
MD
509 if (URCU_TLS(thread_call_rcu_data) != NULL)
510 return URCU_TLS(thread_call_rcu_data);
9744f3bb
LJ
511
512 if (maxcpus > 0) {
63b495d8 513 crd = get_cpu_call_rcu_data(urcu_sched_getcpu());
9744f3bb
LJ
514 if (crd)
515 return crd;
b57aee66 516 }
9744f3bb 517
b57aee66
PM
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
525struct call_rcu_data *get_thread_call_rcu_data(void)
526{
bd252a04 527 return URCU_TLS(thread_call_rcu_data);
b57aee66
PM
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.)
7106ddf8
PM
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).
b57aee66
PM
539 */
540
541void set_thread_call_rcu_data(struct call_rcu_data *crdp)
542{
bd252a04 543 URCU_TLS(thread_call_rcu_data) = crdp;
b57aee66
PM
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()
0938c541
MD
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.
b57aee66
PM
552 */
553
554int 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 }
c1d2c60b 577 crdp = __create_call_rcu_data(flags, i);
b57aee66
PM
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) {
356c8794
LJ
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;
b57aee66
PM
592 }
593 }
594 return 0;
595}
596
7106ddf8
PM
597/*
598 * Wake up the call_rcu thread corresponding to the specified
599 * call_rcu_data structure.
600 */
601static void wake_call_rcu_thread(struct call_rcu_data *crdp)
602{
263e3cf9
MD
603 if (!(_CMM_LOAD_SHARED(crdp->flags) & URCU_CALL_RCU_RT))
604 call_rcu_wake_up(crdp);
7106ddf8
PM
605}
606
b57aee66
PM
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().
618b2595
MD
618 *
619 * call_rcu must be called by registered RCU read-side threads.
b57aee66
PM
620 */
621
622void call_rcu(struct rcu_head *head,
623 void (*func)(struct rcu_head *head))
624{
625 struct call_rcu_data *crdp;
626
5161f31e 627 cds_wfcq_node_init(&head->next);
b57aee66 628 head->func = func;
618b2595
MD
629 /* Holding rcu read-side lock across use of per-cpu crdp */
630 rcu_read_lock();
b57aee66 631 crdp = get_call_rcu_data();
5161f31e 632 cds_wfcq_enqueue(&crdp->cbs_head, &crdp->cbs_tail, &head->next);
b57aee66 633 uatomic_inc(&crdp->qlen);
7106ddf8 634 wake_call_rcu_thread(crdp);
618b2595 635 rcu_read_unlock();
7106ddf8
PM
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.
f9da0936
MD
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.
03e5118f
MD
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.
7106ddf8
PM
663 */
664void call_rcu_data_free(struct call_rcu_data *crdp)
665{
7106ddf8
PM
666 if (crdp == NULL || crdp == default_call_rcu_data) {
667 return;
668 }
2870aa1e
PB
669 if ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOPPED) == 0) {
670 uatomic_or(&crdp->flags, URCU_CALL_RCU_STOP);
7106ddf8 671 wake_call_rcu_thread(crdp);
2870aa1e 672 while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOPPED) == 0)
7106ddf8
PM
673 poll(NULL, 0, 1);
674 }
5161f31e 675 if (!cds_wfcq_empty(&crdp->cbs_head, &crdp->cbs_tail)) {
698d0778
MD
676 /* Create default call rcu data if need be */
677 (void) get_default_call_rcu_data();
5161f31e
MD
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);
7106ddf8
PM
681 uatomic_add(&default_call_rcu_data->qlen,
682 uatomic_read(&crdp->qlen));
1e92aa15 683 wake_call_rcu_thread(default_call_rcu_data);
7106ddf8 684 }
59dc9e9d 685
b75dffe6 686 call_rcu_lock(&call_rcu_mutex);
59dc9e9d 687 cds_list_del(&crdp->list);
b75dffe6
LJ
688 call_rcu_unlock(&call_rcu_mutex);
689
59dc9e9d 690 free(crdp);
7106ddf8
PM
691}
692
693/*
694 * Clean up all the per-CPU call_rcu threads.
695 */
696void free_all_cpu_call_rcu_data(void)
697{
698 int cpu;
618b2595
MD
699 struct call_rcu_data **crdp;
700 static int warned = 0;
7106ddf8
PM
701
702 if (maxcpus <= 0)
703 return;
618b2595
MD
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;
d31150b4 711 return;
618b2595
MD
712 }
713
7106ddf8 714 for (cpu = 0; cpu < maxcpus; cpu++) {
618b2595
MD
715 crdp[cpu] = get_cpu_call_rcu_data(cpu);
716 if (crdp[cpu] == NULL)
7106ddf8
PM
717 continue;
718 set_cpu_call_rcu_data(cpu, NULL);
7106ddf8 719 }
618b2595
MD
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);
7106ddf8
PM
731}
732
81ad2e19
PM
733/*
734 * Acquire the call_rcu_mutex in order to ensure that the child sees
e85451a1
MD
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.
81ad2e19
PM
737 * Suitable for pthread_atfork() and friends.
738 */
739void call_rcu_before_fork(void)
740{
e85451a1
MD
741 struct call_rcu_data *crdp;
742
81ad2e19 743 call_rcu_lock(&call_rcu_mutex);
e85451a1
MD
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 }
81ad2e19
PM
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 */
761void call_rcu_after_fork_parent(void)
762{
e85451a1
MD
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);
81ad2e19
PM
767 call_rcu_unlock(&call_rcu_mutex);
768}
769
7106ddf8
PM
770/*
771 * Clean up call_rcu data structures in the child of a successful fork()
81ad2e19
PM
772 * that is not followed by exec(). Suitable for pthread_atfork() and
773 * friends.
7106ddf8
PM
774 */
775void call_rcu_after_fork_child(void)
776{
077ff173 777 struct call_rcu_data *crdp, *next;
7106ddf8 778
81ad2e19
PM
779 /* Release the mutex. */
780 call_rcu_unlock(&call_rcu_mutex);
781
ad1b9909
LJ
782 /* Do nothing when call_rcu() has not been used */
783 if (cds_list_empty(&call_rcu_data_list))
784 return;
785
7106ddf8
PM
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
60af049d
LJ
793 /* Cleanup call_rcu_data pointers before use */
794 maxcpus_reset();
795 free(per_cpu_call_rcu_data);
618b2595 796 rcu_set_pointer(&per_cpu_call_rcu_data, NULL);
bd252a04 797 URCU_TLS(thread_call_rcu_data) = NULL;
60af049d 798
e85451a1
MD
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 */
077ff173 804 cds_list_for_each_entry_safe(crdp, next, &call_rcu_data_list, list) {
7106ddf8 805 if (crdp == default_call_rcu_data)
077ff173 806 continue;
2870aa1e 807 uatomic_set(&crdp->flags, URCU_CALL_RCU_STOPPED);
7106ddf8 808 call_rcu_data_free(crdp);
b57aee66
PM
809 }
810}
This page took 0.062696 seconds and 4 git commands to generate.