4 * Userspace RCU library - batch memory reclamation
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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
35 #include "urcu-defer-static.h"
36 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
37 #include "urcu-defer.h"
39 #define futex(...) syscall(__NR_futex, __VA_ARGS__)
43 void __attribute__((destructor
)) urcu_defer_exit(void);
45 extern void synchronize_rcu(void);
48 * urcu_defer_mutex nests inside defer_thread_mutex.
50 static pthread_mutex_t urcu_defer_mutex
= PTHREAD_MUTEX_INITIALIZER
;
51 static pthread_mutex_t defer_thread_mutex
= PTHREAD_MUTEX_INITIALIZER
;
53 static int defer_thread_futex
;
56 * Written to only by each individual deferer. Read by both the deferer and
57 * the reclamation tread.
59 static struct defer_queue __thread defer_queue
;
60 static LIST_HEAD(registry
);
61 static pthread_t tid_defer
;
63 static void internal_urcu_lock(pthread_mutex_t
*mutex
)
67 #ifndef DISTRUST_SIGNALS_EXTREME
68 ret
= pthread_mutex_lock(mutex
);
70 perror("Error in pthread mutex lock");
73 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
74 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
75 if (ret
!= EBUSY
&& ret
!= EINTR
) {
76 printf("ret = %d, errno = %d\n", ret
, errno
);
77 perror("Error in pthread mutex lock");
83 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
86 static void internal_urcu_unlock(pthread_mutex_t
*mutex
)
90 ret
= pthread_mutex_unlock(mutex
);
92 perror("Error in pthread mutex unlock");
98 * Wake-up any waiting defer thread. Called from many concurrent threads.
100 static void wake_up_defer(void)
102 if (unlikely(uatomic_read(&defer_thread_futex
) == -1)) {
103 uatomic_set(&defer_thread_futex
, 0);
104 futex(&defer_thread_futex
, FUTEX_WAKE
, 1,
109 static unsigned long rcu_defer_num_callbacks(void)
111 unsigned long num_items
= 0, head
;
112 struct defer_queue
*index
;
114 internal_urcu_lock(&urcu_defer_mutex
);
115 list_for_each_entry(index
, ®istry
, list
) {
116 head
= LOAD_SHARED(index
->head
);
117 num_items
+= head
- index
->tail
;
119 internal_urcu_unlock(&urcu_defer_mutex
);
124 * Defer thread waiting. Single thread.
126 static void wait_defer(void)
128 uatomic_dec(&defer_thread_futex
);
129 smp_mb(); /* Write futex before read queue */
130 if (rcu_defer_num_callbacks()) {
131 smp_mb(); /* Read queue before write futex */
132 /* Callbacks are queued, don't wait. */
133 uatomic_set(&defer_thread_futex
, 0);
135 smp_rmb(); /* Read queue before read futex */
136 if (uatomic_read(&defer_thread_futex
) == -1)
137 futex(&defer_thread_futex
, FUTEX_WAIT
, -1,
143 * Must be called after Q.S. is reached.
145 static void rcu_defer_barrier_queue(struct defer_queue
*queue
,
149 void (*fct
)(void *p
);
153 * Tail is only modified when lock is held.
154 * Head is only modified by owner thread.
157 for (i
= queue
->tail
; i
!= head
;) {
158 smp_rmb(); /* read head before q[]. */
159 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
160 if (unlikely(DQ_IS_FCT_BIT(p
))) {
162 queue
->last_fct_out
= p
;
163 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
164 } else if (unlikely(p
== DQ_FCT_MARK
)) {
165 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
166 queue
->last_fct_out
= p
;
167 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
169 fct
= queue
->last_fct_out
;
172 smp_mb(); /* push tail after having used q[] */
173 STORE_SHARED(queue
->tail
, i
);
176 static void _rcu_defer_barrier_thread(void)
178 unsigned long head
, num_items
;
180 head
= defer_queue
.head
;
181 num_items
= head
- defer_queue
.tail
;
182 if (unlikely(!num_items
))
185 rcu_defer_barrier_queue(&defer_queue
, head
);
188 void rcu_defer_barrier_thread(void)
190 internal_urcu_lock(&urcu_defer_mutex
);
191 _rcu_defer_barrier_thread();
192 internal_urcu_unlock(&urcu_defer_mutex
);
196 * rcu_defer_barrier - Execute all queued rcu callbacks.
198 * Execute all RCU callbacks queued before rcu_defer_barrier() execution.
199 * All callbacks queued on the local thread prior to a rcu_defer_barrier() call
200 * are guaranteed to be executed.
201 * Callbacks queued by other threads concurrently with rcu_defer_barrier()
202 * execution are not guaranteed to be executed in the current batch (could
203 * be left for the next batch). These callbacks queued by other threads are only
204 * guaranteed to be executed if there is explicit synchronization between
205 * the thread adding to the queue and the thread issuing the defer_barrier call.
208 void rcu_defer_barrier(void)
210 struct defer_queue
*index
;
211 unsigned long num_items
= 0;
213 if (list_empty(®istry
))
216 internal_urcu_lock(&urcu_defer_mutex
);
217 list_for_each_entry(index
, ®istry
, list
) {
218 index
->last_head
= LOAD_SHARED(index
->head
);
219 num_items
+= index
->last_head
- index
->tail
;
221 if (likely(!num_items
)) {
223 * We skip the grace period because there are no queued
224 * callbacks to execute.
229 list_for_each_entry(index
, ®istry
, list
)
230 rcu_defer_barrier_queue(index
, index
->last_head
);
232 internal_urcu_unlock(&urcu_defer_mutex
);
236 * _rcu_defer_queue - Queue a RCU callback.
238 void _rcu_defer_queue(void (*fct
)(void *p
), void *p
)
240 unsigned long head
, tail
;
243 * Head is only modified by ourself. Tail can be modified by reclamation
246 head
= defer_queue
.head
;
247 tail
= LOAD_SHARED(defer_queue
.tail
);
250 * If queue is full, empty it ourself.
251 * Worse-case: must allow 2 supplementary entries for fct pointer.
253 if (unlikely(head
- tail
>= DEFER_QUEUE_SIZE
- 2)) {
254 assert(head
- tail
<= DEFER_QUEUE_SIZE
);
255 rcu_defer_barrier_thread();
256 assert(head
- LOAD_SHARED(defer_queue
.tail
) == 0);
259 if (unlikely(defer_queue
.last_fct_in
!= fct
)) {
260 defer_queue
.last_fct_in
= fct
;
261 if (unlikely(DQ_IS_FCT_BIT(fct
) || fct
== DQ_FCT_MARK
)) {
263 * If the function to encode is not aligned or the
264 * marker, write DQ_FCT_MARK followed by the function
267 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
269 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
273 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
277 if (unlikely(DQ_IS_FCT_BIT(p
) || p
== DQ_FCT_MARK
)) {
279 * If the data to encode is not aligned or the marker,
280 * write DQ_FCT_MARK followed by the function pointer.
282 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
284 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
288 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
], p
);
289 smp_wmb(); /* Publish new pointer before head */
290 /* Write q[] before head. */
291 STORE_SHARED(defer_queue
.head
, head
);
292 smp_mb(); /* Write queue head before read futex */
294 * Wake-up any waiting defer thread.
299 void *thr_defer(void *args
)
302 pthread_testcancel();
304 * "Be green". Don't wake up the CPU if there is no RCU work
305 * to perform whatsoever. Aims at saving laptop battery life by
306 * leaving the processor in sleep state when idle.
309 /* Sleeping after wait_defer to let many callbacks enqueue */
310 poll(NULL
,0,100); /* wait for 100ms */
318 * library wrappers to be used by non-LGPL compatible source code.
321 void rcu_defer_queue(void (*fct
)(void *p
), void *p
)
323 _rcu_defer_queue(fct
, p
);
326 static void start_defer_thread(void)
330 ret
= pthread_create(&tid_defer
, NULL
, thr_defer
, NULL
);
334 static void stop_defer_thread(void)
339 pthread_cancel(tid_defer
);
341 ret
= pthread_join(tid_defer
, &tret
);
345 void rcu_defer_register_thread(void)
349 assert(defer_queue
.last_head
== 0);
350 assert(defer_queue
.q
== NULL
);
351 defer_queue
.q
= malloc(sizeof(void *) * DEFER_QUEUE_SIZE
);
353 internal_urcu_lock(&defer_thread_mutex
);
354 internal_urcu_lock(&urcu_defer_mutex
);
355 was_empty
= list_empty(®istry
);
356 list_add(&defer_queue
.list
, ®istry
);
357 internal_urcu_unlock(&urcu_defer_mutex
);
360 start_defer_thread();
361 internal_urcu_unlock(&defer_thread_mutex
);
364 void rcu_defer_unregister_thread(void)
368 internal_urcu_lock(&defer_thread_mutex
);
369 internal_urcu_lock(&urcu_defer_mutex
);
370 list_del(&defer_queue
.list
);
371 _rcu_defer_barrier_thread();
373 defer_queue
.q
= NULL
;
374 is_empty
= list_empty(®istry
);
375 internal_urcu_unlock(&urcu_defer_mutex
);
379 internal_urcu_unlock(&defer_thread_mutex
);
382 void urcu_defer_exit(void)
384 assert(list_empty(®istry
));