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_RT
)
189 call_rcu_lock(&crdp
->mtx
);
190 _CMM_STORE_SHARED(crdp
->flags
,
191 crdp
->flags
& ~URCU_CALL_RCU_RUNNING
);
192 if (&crdp
->cbs
.head
==
193 _CMM_LOAD_SHARED(crdp
->cbs
.tail
) &&
194 pthread_cond_wait(&crdp
->cond
, &crdp
->mtx
) != 0) {
195 perror("pthread_cond_wait");
198 _CMM_STORE_SHARED(crdp
->flags
,
199 crdp
->flags
| URCU_CALL_RCU_RUNNING
);
201 call_rcu_unlock(&crdp
->mtx
);
204 return NULL
; /* NOTREACHED */
208 * Create both a call_rcu thread and the corresponding call_rcu_data
209 * structure, linking the structure in as specified. Caller must hold
213 static void call_rcu_data_init(struct call_rcu_data
**crdpp
,
216 struct call_rcu_data
*crdp
;
218 crdp
= malloc(sizeof(*crdp
));
220 fprintf(stderr
, "Out of memory.\n");
223 memset(crdp
, '\0', sizeof(*crdp
));
224 cds_wfq_init(&crdp
->cbs
);
226 if (pthread_mutex_init(&crdp
->mtx
, NULL
) != 0) {
227 perror("pthread_mutex_init");
230 if (pthread_cond_init(&crdp
->cond
, NULL
) != 0) {
231 perror("pthread_cond_init");
234 crdp
->flags
= flags
| URCU_CALL_RCU_RUNNING
;
235 cds_list_add(&crdp
->list
, &call_rcu_data_list
);
236 cmm_smp_mb(); /* Structure initialized before pointer is planted. */
238 if (pthread_create(&crdp
->tid
, NULL
, call_rcu_thread
, crdp
) != 0) {
239 perror("pthread_create");
245 * Return a pointer to the call_rcu_data structure for the specified
246 * CPU, returning NULL if there is none. We cannot automatically
247 * created it because the platform we are running on might not define
251 struct call_rcu_data
*get_cpu_call_rcu_data(int cpu
)
253 static int warned
= 0;
255 if (per_cpu_call_rcu_data
== NULL
)
257 if (!warned
&& maxcpus
> 0 && (cpu
< 0 || maxcpus
<= cpu
)) {
258 fprintf(stderr
, "[error] liburcu: get CPU # out of range\n");
261 if (cpu
< 0 || maxcpus
<= cpu
)
263 return per_cpu_call_rcu_data
[cpu
];
267 * Return the tid corresponding to the call_rcu thread whose
268 * call_rcu_data structure is specified.
271 pthread_t
get_call_rcu_thread(struct call_rcu_data
*crdp
)
277 * Create a call_rcu_data structure (with thread) and return a pointer.
280 static struct call_rcu_data
*__create_call_rcu_data(unsigned long flags
)
282 struct call_rcu_data
*crdp
;
284 call_rcu_data_init(&crdp
, flags
);
288 struct call_rcu_data
*create_call_rcu_data(unsigned long flags
)
290 struct call_rcu_data
*crdp
;
292 call_rcu_lock(&call_rcu_mutex
);
293 crdp
= __create_call_rcu_data(flags
);
294 call_rcu_unlock(&call_rcu_mutex
);
299 * Set the specified CPU to use the specified call_rcu_data structure.
302 int set_cpu_call_rcu_data(int cpu
, struct call_rcu_data
*crdp
)
306 call_rcu_lock(&call_rcu_mutex
);
307 if (cpu
< 0 || maxcpus
<= cpu
) {
309 fprintf(stderr
, "[error] liburcu: set CPU # out of range\n");
312 call_rcu_unlock(&call_rcu_mutex
);
316 alloc_cpu_call_rcu_data();
317 call_rcu_unlock(&call_rcu_mutex
);
318 if (per_cpu_call_rcu_data
== NULL
) {
322 per_cpu_call_rcu_data
[cpu
] = crdp
;
327 * Return a pointer to the default call_rcu_data structure, creating
328 * one if need be. Because we never free call_rcu_data structures,
329 * we don't need to be in an RCU read-side critical section.
332 struct call_rcu_data
*get_default_call_rcu_data(void)
334 if (default_call_rcu_data
!= NULL
)
335 return rcu_dereference(default_call_rcu_data
);
336 call_rcu_lock(&call_rcu_mutex
);
337 if (default_call_rcu_data
!= NULL
) {
338 call_rcu_unlock(&call_rcu_mutex
);
339 return default_call_rcu_data
;
341 call_rcu_data_init(&default_call_rcu_data
, 0);
342 call_rcu_unlock(&call_rcu_mutex
);
343 return default_call_rcu_data
;
347 * Return the call_rcu_data structure that applies to the currently
348 * running thread. Any call_rcu_data structure assigned specifically
349 * to this thread has first priority, followed by any call_rcu_data
350 * structure assigned to the CPU on which the thread is running,
351 * followed by the default call_rcu_data structure. If there is not
352 * yet a default call_rcu_data structure, one will be created.
354 struct call_rcu_data
*get_call_rcu_data(void)
357 static int warned
= 0;
359 if (thread_call_rcu_data
!= NULL
)
360 return thread_call_rcu_data
;
362 return get_default_call_rcu_data();
363 curcpu
= sched_getcpu();
364 if (!warned
&& (curcpu
< 0 || maxcpus
<= curcpu
)) {
365 fprintf(stderr
, "[error] liburcu: gcrd CPU # out of range\n");
368 if (curcpu
>= 0 && maxcpus
> curcpu
&&
369 per_cpu_call_rcu_data
!= NULL
&&
370 per_cpu_call_rcu_data
[curcpu
] != NULL
)
371 return per_cpu_call_rcu_data
[curcpu
];
372 return get_default_call_rcu_data();
376 * Return a pointer to this task's call_rcu_data if there is one.
379 struct call_rcu_data
*get_thread_call_rcu_data(void)
381 return thread_call_rcu_data
;
385 * Set this task's call_rcu_data structure as specified, regardless
386 * of whether or not this task already had one. (This allows switching
387 * to and from real-time call_rcu threads, for example.)
390 void set_thread_call_rcu_data(struct call_rcu_data
*crdp
)
392 thread_call_rcu_data
= crdp
;
396 * Create a separate call_rcu thread for each CPU. This does not
397 * replace a pre-existing call_rcu thread -- use the set_cpu_call_rcu_data()
398 * function if you want that behavior.
401 int create_all_cpu_call_rcu_data(unsigned long flags
)
404 struct call_rcu_data
*crdp
;
407 call_rcu_lock(&call_rcu_mutex
);
408 alloc_cpu_call_rcu_data();
409 call_rcu_unlock(&call_rcu_mutex
);
414 if (per_cpu_call_rcu_data
== NULL
) {
418 for (i
= 0; i
< maxcpus
; i
++) {
419 call_rcu_lock(&call_rcu_mutex
);
420 if (get_cpu_call_rcu_data(i
)) {
421 call_rcu_unlock(&call_rcu_mutex
);
424 crdp
= __create_call_rcu_data(flags
);
426 call_rcu_unlock(&call_rcu_mutex
);
430 call_rcu_unlock(&call_rcu_mutex
);
431 if ((ret
= set_cpu_call_rcu_data(i
, crdp
)) != 0) {
432 /* FIXME: Leaks crdp for now. */
433 return ret
; /* Can happen on race. */
440 * Schedule a function to be invoked after a following grace period.
441 * This is the only function that must be called -- the others are
442 * only present to allow applications to tune their use of RCU for
443 * maximum performance.
445 * Note that unless a call_rcu thread has not already been created,
446 * the first invocation of call_rcu() will create one. So, if you
447 * need the first invocation of call_rcu() to be fast, make sure
448 * to create a call_rcu thread first. One way to accomplish this is
449 * "get_call_rcu_data();", and another is create_all_cpu_call_rcu_data().
452 void call_rcu(struct rcu_head
*head
,
453 void (*func
)(struct rcu_head
*head
))
455 struct call_rcu_data
*crdp
;
457 cds_wfq_node_init(&head
->next
);
459 crdp
= get_call_rcu_data();
460 cds_wfq_enqueue(&crdp
->cbs
, &head
->next
);
461 uatomic_inc(&crdp
->qlen
);
462 if (!(_CMM_LOAD_SHARED(crdp
->flags
) & URCU_CALL_RCU_RT
)) {
463 call_rcu_lock(&crdp
->mtx
);
464 if (!(_CMM_LOAD_SHARED(crdp
->flags
) & URCU_CALL_RCU_RUNNING
)) {
465 if (pthread_cond_signal(&crdp
->cond
) != 0) {
466 perror("pthread_cond_signal");
470 call_rcu_unlock(&crdp
->mtx
);