4 * Userspace RCU library - batch memory reclamation with kernel API
6 * Copyright (c) 2010 Paul E. McKenney <paulmck@linux.vnet.ibm.com>
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.
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.
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
36 #include "urcu/wfqueue.h"
37 #include "urcu-call-rcu.h"
38 #include "urcu-pointer.h"
39 #include "urcu/list.h"
41 /* Data structure that identifies a call_rcu thread. */
43 struct call_rcu_data
{
44 struct cds_wfq_queue cbs
;
50 struct cds_list_head list
;
51 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
54 * List of all call_rcu_data structures to keep valgrind happy.
55 * Protected by call_rcu_mutex.
58 CDS_LIST_HEAD(call_rcu_data_list
);
60 /* Link a thread using call_rcu() to its call_rcu thread. */
62 static __thread
struct call_rcu_data
*thread_call_rcu_data
;
64 /* Guard call_rcu thread creation. */
66 static pthread_mutex_t call_rcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
68 /* If a given thread does not have its own call_rcu thread, this is default. */
70 static struct call_rcu_data
*default_call_rcu_data
;
72 extern void synchronize_rcu(void);
75 * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are
76 * available, then we can have call_rcu threads assigned to individual
77 * CPUs rather than only to specific threads.
80 #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
83 * Pointer to array of pointers to per-CPU call_rcu_data structures
87 static struct call_rcu_data
**per_cpu_call_rcu_data
;
90 /* Allocate the array if it has not already been allocated. */
92 static void alloc_cpu_call_rcu_data(void)
94 struct call_rcu_data
**p
;
95 static int warned
= 0;
99 maxcpus
= sysconf(_SC_NPROCESSORS_CONF
);
103 p
= malloc(maxcpus
* sizeof(*per_cpu_call_rcu_data
));
105 memset(p
, '\0', maxcpus
* sizeof(*per_cpu_call_rcu_data
));
106 per_cpu_call_rcu_data
= p
;
109 fprintf(stderr
, "[error] liburcu: unable to allocate per-CPU pointer array\n");
115 #else /* #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
117 static const struct call_rcu_data
**per_cpu_call_rcu_data
= NULL
;
118 static const long maxcpus
= -1;
120 static void alloc_cpu_call_rcu_data(void)
124 static int sched_getcpu(void)
129 #endif /* #else #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
131 /* Acquire the specified pthread mutex. */
133 static void call_rcu_lock(pthread_mutex_t
*pmp
)
135 if (pthread_mutex_lock(pmp
) != 0) {
136 perror("pthread_mutex_lock");
141 /* Release the specified pthread mutex. */
143 static void call_rcu_unlock(pthread_mutex_t
*pmp
)
145 if (pthread_mutex_unlock(pmp
) != 0) {
146 perror("pthread_mutex_unlock");
151 /* This is the code run by each call_rcu thread. */
153 static void *call_rcu_thread(void *arg
)
155 unsigned long cbcount
;
156 struct cds_wfq_node
*cbs
;
157 struct cds_wfq_node
**cbs_tail
;
158 struct call_rcu_data
*crdp
= (struct call_rcu_data
*)arg
;
159 struct rcu_head
*rhp
;
161 thread_call_rcu_data
= crdp
;
163 if (&crdp
->cbs
.head
!= _CMM_LOAD_SHARED(crdp
->cbs
.tail
)) {
164 while ((cbs
= _CMM_LOAD_SHARED(crdp
->cbs
.head
)) == NULL
)
166 _CMM_STORE_SHARED(crdp
->cbs
.head
, NULL
);
167 cbs_tail
= (struct cds_wfq_node
**)
168 uatomic_xchg(&crdp
->cbs
.tail
, &crdp
->cbs
.head
);
172 while (cbs
->next
== NULL
&&
173 &cbs
->next
!= cbs_tail
)
175 if (cbs
== &crdp
->cbs
.dummy
) {
179 rhp
= (struct rcu_head
*)cbs
;
183 } while (cbs
!= NULL
);
184 uatomic_sub(&crdp
->qlen
, cbcount
);
186 if (crdp
->flags
& URCU_CALL_RCU_STOP
)
188 if (crdp
->flags
& URCU_CALL_RCU_RT
)
191 call_rcu_lock(&crdp
->mtx
);
192 _CMM_STORE_SHARED(crdp
->flags
,
193 crdp
->flags
& ~URCU_CALL_RCU_RUNNING
);
194 if (&crdp
->cbs
.head
==
195 _CMM_LOAD_SHARED(crdp
->cbs
.tail
) &&
196 pthread_cond_wait(&crdp
->cond
, &crdp
->mtx
) != 0) {
197 perror("pthread_cond_wait");
200 _CMM_STORE_SHARED(crdp
->flags
,
201 crdp
->flags
| URCU_CALL_RCU_RUNNING
);
203 call_rcu_unlock(&crdp
->mtx
);
206 call_rcu_lock(&crdp
->mtx
);
207 crdp
->flags
|= URCU_CALL_RCU_STOPPED
;
208 call_rcu_unlock(&crdp
->mtx
);
213 * Create both a call_rcu thread and the corresponding call_rcu_data
214 * structure, linking the structure in as specified. Caller must hold
218 static void call_rcu_data_init(struct call_rcu_data
**crdpp
,
221 struct call_rcu_data
*crdp
;
223 crdp
= malloc(sizeof(*crdp
));
225 fprintf(stderr
, "Out of memory.\n");
228 memset(crdp
, '\0', sizeof(*crdp
));
229 cds_wfq_init(&crdp
->cbs
);
231 if (pthread_mutex_init(&crdp
->mtx
, NULL
) != 0) {
232 perror("pthread_mutex_init");
235 if (pthread_cond_init(&crdp
->cond
, NULL
) != 0) {
236 perror("pthread_cond_init");
239 crdp
->flags
= flags
| URCU_CALL_RCU_RUNNING
;
240 cds_list_add(&crdp
->list
, &call_rcu_data_list
);
241 cmm_smp_mb(); /* Structure initialized before pointer is planted. */
243 if (pthread_create(&crdp
->tid
, NULL
, call_rcu_thread
, crdp
) != 0) {
244 perror("pthread_create");
250 * Return a pointer to the call_rcu_data structure for the specified
251 * CPU, returning NULL if there is none. We cannot automatically
252 * created it because the platform we are running on might not define
256 struct call_rcu_data
*get_cpu_call_rcu_data(int cpu
)
258 static int warned
= 0;
260 if (per_cpu_call_rcu_data
== NULL
)
262 if (!warned
&& maxcpus
> 0 && (cpu
< 0 || maxcpus
<= cpu
)) {
263 fprintf(stderr
, "[error] liburcu: get CPU # out of range\n");
266 if (cpu
< 0 || maxcpus
<= cpu
)
268 return per_cpu_call_rcu_data
[cpu
];
272 * Return the tid corresponding to the call_rcu thread whose
273 * call_rcu_data structure is specified.
276 pthread_t
get_call_rcu_thread(struct call_rcu_data
*crdp
)
282 * Create a call_rcu_data structure (with thread) and return a pointer.
285 static struct call_rcu_data
*__create_call_rcu_data(unsigned long flags
)
287 struct call_rcu_data
*crdp
;
289 call_rcu_data_init(&crdp
, flags
);
293 struct call_rcu_data
*create_call_rcu_data(unsigned long flags
)
295 struct call_rcu_data
*crdp
;
297 call_rcu_lock(&call_rcu_mutex
);
298 crdp
= __create_call_rcu_data(flags
);
299 call_rcu_unlock(&call_rcu_mutex
);
304 * Set the specified CPU to use the specified call_rcu_data structure.
306 * Use NULL to remove a CPU's call_rcu_data structure, but it is
307 * the caller's responsibility to dispose of the removed structure.
308 * Use get_cpu_call_rcu_data() to obtain a pointer to the old structure
309 * (prior to NULLing it out, of course).
312 int set_cpu_call_rcu_data(int cpu
, struct call_rcu_data
*crdp
)
316 call_rcu_lock(&call_rcu_mutex
);
317 if (cpu
< 0 || maxcpus
<= cpu
) {
319 fprintf(stderr
, "[error] liburcu: set CPU # out of range\n");
322 call_rcu_unlock(&call_rcu_mutex
);
326 alloc_cpu_call_rcu_data();
327 call_rcu_unlock(&call_rcu_mutex
);
328 if (per_cpu_call_rcu_data
== NULL
) {
332 per_cpu_call_rcu_data
[cpu
] = crdp
;
337 * Return a pointer to the default call_rcu_data structure, creating
338 * one if need be. Because we never free call_rcu_data structures,
339 * we don't need to be in an RCU read-side critical section.
342 struct call_rcu_data
*get_default_call_rcu_data(void)
344 if (default_call_rcu_data
!= NULL
)
345 return rcu_dereference(default_call_rcu_data
);
346 call_rcu_lock(&call_rcu_mutex
);
347 if (default_call_rcu_data
!= NULL
) {
348 call_rcu_unlock(&call_rcu_mutex
);
349 return default_call_rcu_data
;
351 call_rcu_data_init(&default_call_rcu_data
, 0);
352 call_rcu_unlock(&call_rcu_mutex
);
353 return default_call_rcu_data
;
357 * Return the call_rcu_data structure that applies to the currently
358 * running thread. Any call_rcu_data structure assigned specifically
359 * to this thread has first priority, followed by any call_rcu_data
360 * structure assigned to the CPU on which the thread is running,
361 * followed by the default call_rcu_data structure. If there is not
362 * yet a default call_rcu_data structure, one will be created.
364 struct call_rcu_data
*get_call_rcu_data(void)
367 static int warned
= 0;
369 if (thread_call_rcu_data
!= NULL
)
370 return thread_call_rcu_data
;
372 return get_default_call_rcu_data();
373 curcpu
= sched_getcpu();
374 if (!warned
&& (curcpu
< 0 || maxcpus
<= curcpu
)) {
375 fprintf(stderr
, "[error] liburcu: gcrd CPU # out of range\n");
378 if (curcpu
>= 0 && maxcpus
> curcpu
&&
379 per_cpu_call_rcu_data
!= NULL
&&
380 per_cpu_call_rcu_data
[curcpu
] != NULL
)
381 return per_cpu_call_rcu_data
[curcpu
];
382 return get_default_call_rcu_data();
386 * Return a pointer to this task's call_rcu_data if there is one.
389 struct call_rcu_data
*get_thread_call_rcu_data(void)
391 return thread_call_rcu_data
;
395 * Set this task's call_rcu_data structure as specified, regardless
396 * of whether or not this task already had one. (This allows switching
397 * to and from real-time call_rcu threads, for example.)
399 * Use NULL to remove a thread's call_rcu_data structure, but it is
400 * the caller's responsibility to dispose of the removed structure.
401 * Use get_thread_call_rcu_data() to obtain a pointer to the old structure
402 * (prior to NULLing it out, of course).
405 void set_thread_call_rcu_data(struct call_rcu_data
*crdp
)
407 thread_call_rcu_data
= crdp
;
411 * Create a separate call_rcu thread for each CPU. This does not
412 * replace a pre-existing call_rcu thread -- use the set_cpu_call_rcu_data()
413 * function if you want that behavior.
416 int create_all_cpu_call_rcu_data(unsigned long flags
)
419 struct call_rcu_data
*crdp
;
422 call_rcu_lock(&call_rcu_mutex
);
423 alloc_cpu_call_rcu_data();
424 call_rcu_unlock(&call_rcu_mutex
);
429 if (per_cpu_call_rcu_data
== NULL
) {
433 for (i
= 0; i
< maxcpus
; i
++) {
434 call_rcu_lock(&call_rcu_mutex
);
435 if (get_cpu_call_rcu_data(i
)) {
436 call_rcu_unlock(&call_rcu_mutex
);
439 crdp
= __create_call_rcu_data(flags
);
441 call_rcu_unlock(&call_rcu_mutex
);
445 call_rcu_unlock(&call_rcu_mutex
);
446 if ((ret
= set_cpu_call_rcu_data(i
, crdp
)) != 0) {
447 /* FIXME: Leaks crdp for now. */
448 return ret
; /* Can happen on race. */
455 * Wake up the call_rcu thread corresponding to the specified
456 * call_rcu_data structure.
458 static void wake_call_rcu_thread(struct call_rcu_data
*crdp
)
460 if (!(_CMM_LOAD_SHARED(crdp
->flags
) & URCU_CALL_RCU_RT
)) {
461 call_rcu_lock(&crdp
->mtx
);
462 if (!(_CMM_LOAD_SHARED(crdp
->flags
) & URCU_CALL_RCU_RUNNING
)) {
463 if (pthread_cond_signal(&crdp
->cond
) != 0) {
464 perror("pthread_cond_signal");
468 call_rcu_unlock(&crdp
->mtx
);
473 * Schedule a function to be invoked after a following grace period.
474 * This is the only function that must be called -- the others are
475 * only present to allow applications to tune their use of RCU for
476 * maximum performance.
478 * Note that unless a call_rcu thread has not already been created,
479 * the first invocation of call_rcu() will create one. So, if you
480 * need the first invocation of call_rcu() to be fast, make sure
481 * to create a call_rcu thread first. One way to accomplish this is
482 * "get_call_rcu_data();", and another is create_all_cpu_call_rcu_data().
485 void call_rcu(struct rcu_head
*head
,
486 void (*func
)(struct rcu_head
*head
))
488 struct call_rcu_data
*crdp
;
490 cds_wfq_node_init(&head
->next
);
492 crdp
= get_call_rcu_data();
493 cds_wfq_enqueue(&crdp
->cbs
, &head
->next
);
494 uatomic_inc(&crdp
->qlen
);
495 wake_call_rcu_thread(crdp
);
499 * Free up the specified call_rcu_data structure, terminating the
500 * associated call_rcu thread. The caller must have previously
501 * removed the call_rcu_data structure from per-thread or per-CPU
502 * usage. For example, set_cpu_call_rcu_data(cpu, NULL) for per-CPU
503 * call_rcu_data structures or set_thread_call_rcu_data(NULL) for
504 * per-thread call_rcu_data structures.
506 * We silently refuse to free up the default call_rcu_data structure
507 * because that is where we put any leftover callbacks. Note that
508 * the possibility of self-spawning callbacks makes it impossible
509 * to execute all the callbacks in finite time without putting any
510 * newly spawned callbacks somewhere else. The "somewhere else" of
511 * last resort is the default call_rcu_data structure.
513 * We also silently refuse to free NULL pointers. This simplifies
516 void call_rcu_data_free(struct call_rcu_data
*crdp
)
518 struct cds_wfq_node
*cbs
;
519 struct cds_wfq_node
**cbs_tail
;
520 struct cds_wfq_node
**cbs_endprev
;
522 if (crdp
== NULL
|| crdp
== default_call_rcu_data
) {
525 if ((crdp
->flags
& URCU_CALL_RCU_STOPPED
) == 0) {
526 call_rcu_lock(&crdp
->mtx
);
527 crdp
->flags
|= URCU_CALL_RCU_STOP
;
528 call_rcu_unlock(&crdp
->mtx
);
529 wake_call_rcu_thread(crdp
);
530 while ((crdp
->flags
& URCU_CALL_RCU_STOPPED
) == 0)
533 if (&crdp
->cbs
.head
!= _CMM_LOAD_SHARED(crdp
->cbs
.tail
)) {
534 while ((cbs
= _CMM_LOAD_SHARED(crdp
->cbs
.head
)) == NULL
)
536 _CMM_STORE_SHARED(crdp
->cbs
.head
, NULL
);
537 cbs_tail
= (struct cds_wfq_node
**)
538 uatomic_xchg(&crdp
->cbs
.tail
, &crdp
->cbs
.head
);
539 cbs_endprev
= (struct cds_wfq_node
**)
540 uatomic_xchg(&default_call_rcu_data
, cbs_tail
);
542 uatomic_add(&default_call_rcu_data
->qlen
,
543 uatomic_read(&crdp
->qlen
));
544 cds_list_del(&crdp
->list
);
550 * Clean up all the per-CPU call_rcu threads.
552 void free_all_cpu_call_rcu_data(void)
555 struct call_rcu_data
*crdp
;
559 for (cpu
= 0; cpu
< maxcpus
; cpu
++) {
560 crdp
= get_cpu_call_rcu_data(cpu
);
563 set_cpu_call_rcu_data(cpu
, NULL
);
564 call_rcu_data_free(crdp
);
569 * Clean up call_rcu data structures in the child of a successful fork()
570 * that is not followed by exec().
572 void call_rcu_after_fork_child(void)
574 struct call_rcu_data
*crdp
;
577 * Allocate a new default call_rcu_data structure in order
578 * to get a working call_rcu thread to go with it.
580 default_call_rcu_data
= NULL
;
581 (void)get_default_call_rcu_data();
583 /* Dispose of all of the rest of the call_rcu_data structures. */
584 while (call_rcu_data_list
.next
!= call_rcu_data_list
.prev
) {
585 crdp
= cds_list_entry(call_rcu_data_list
.prev
,
586 struct call_rcu_data
, list
);
587 if (crdp
== default_call_rcu_data
)
588 crdp
= cds_list_entry(crdp
->list
.prev
,
589 struct call_rcu_data
, list
);
590 crdp
->flags
= URCU_CALL_RCU_STOPPED
;
591 call_rcu_data_free(crdp
);