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
31 #include <linux/futex.h>
36 #include "urcu-defer-static.h"
37 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
38 #include "urcu-defer.h"
40 #define futex(...) syscall(__NR_futex, __VA_ARGS__)
42 void __attribute__((destructor
)) urcu_defer_exit(void);
44 extern void synchronize_rcu(void);
47 * urcu_defer_mutex nests inside defer_thread_mutex.
49 static pthread_mutex_t urcu_defer_mutex
= PTHREAD_MUTEX_INITIALIZER
;
50 static pthread_mutex_t defer_thread_mutex
= PTHREAD_MUTEX_INITIALIZER
;
52 static int defer_thread_futex
;
55 * Written to only by each individual deferer. Read by both the deferer and
56 * the reclamation tread.
58 static struct defer_queue __thread defer_queue
;
60 /* Thread IDs of registered deferers */
61 #define INIT_NUM_THREADS 4
63 struct deferer_registry
{
65 struct defer_queue
*defer_queue
;
66 unsigned long last_head
;
69 static struct deferer_registry
*registry
;
70 static int num_deferers
, alloc_deferers
;
72 static pthread_t tid_defer
;
74 static void internal_urcu_lock(pthread_mutex_t
*mutex
)
78 #ifndef DISTRUST_SIGNALS_EXTREME
79 ret
= pthread_mutex_lock(mutex
);
81 perror("Error in pthread mutex lock");
84 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
85 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
86 if (ret
!= EBUSY
&& ret
!= EINTR
) {
87 printf("ret = %d, errno = %d\n", ret
, errno
);
88 perror("Error in pthread mutex lock");
94 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
97 static void internal_urcu_unlock(pthread_mutex_t
*mutex
)
101 ret
= pthread_mutex_unlock(mutex
);
103 perror("Error in pthread mutex unlock");
109 * Wake-up any waiting defer thread. Called from many concurrent threads.
111 static void wake_up_defer(void)
113 if (unlikely(atomic_read(&defer_thread_futex
) == -1)) {
114 atomic_set(&defer_thread_futex
, 0);
115 futex(&defer_thread_futex
, FUTEX_WAKE
, 0,
120 static unsigned long rcu_defer_num_callbacks(void)
122 unsigned long num_items
= 0, head
;
123 struct deferer_registry
*index
;
125 internal_urcu_lock(&urcu_defer_mutex
);
126 for (index
= registry
; index
< registry
+ num_deferers
; index
++) {
127 head
= LOAD_SHARED(index
->defer_queue
->head
);
128 num_items
+= head
- index
->defer_queue
->tail
;
130 internal_urcu_unlock(&urcu_defer_mutex
);
135 * Defer thread waiting. Single thread.
137 static void wait_defer(void)
139 atomic_dec(&defer_thread_futex
);
140 smp_mb(); /* Write futex before read queue */
141 if (rcu_defer_num_callbacks()) {
142 smp_mb(); /* Read queue before write futex */
143 /* Callbacks are queued, don't wait. */
144 atomic_set(&defer_thread_futex
, 0);
146 smp_rmb(); /* Read queue before read futex */
147 if (atomic_read(&defer_thread_futex
) == -1)
148 futex(&defer_thread_futex
, FUTEX_WAIT
, -1,
154 * Must be called after Q.S. is reached.
156 static void rcu_defer_barrier_queue(struct defer_queue
*queue
,
160 void (*fct
)(void *p
);
164 * Tail is only modified when lock is held.
165 * Head is only modified by owner thread.
168 for (i
= queue
->tail
; i
!= head
;) {
169 smp_rmb(); /* read head before q[]. */
170 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
171 if (unlikely(DQ_IS_FCT_BIT(p
))) {
173 queue
->last_fct_out
= p
;
174 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
175 } else if (unlikely(p
== DQ_FCT_MARK
)) {
176 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
177 queue
->last_fct_out
= p
;
178 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
180 fct
= queue
->last_fct_out
;
183 smp_mb(); /* push tail after having used q[] */
184 STORE_SHARED(queue
->tail
, i
);
187 static void _rcu_defer_barrier_thread(void)
189 unsigned long head
, num_items
;
191 head
= defer_queue
.head
;
192 num_items
= head
- defer_queue
.tail
;
193 if (unlikely(!num_items
))
196 rcu_defer_barrier_queue(&defer_queue
, head
);
200 void rcu_defer_barrier_thread(void)
202 internal_urcu_lock(&urcu_defer_mutex
);
203 _rcu_defer_barrier_thread();
204 internal_urcu_unlock(&urcu_defer_mutex
);
208 * rcu_defer_barrier - Execute all queued rcu callbacks.
210 * Execute all RCU callbacks queued before rcu_defer_barrier() execution.
211 * All callbacks queued on the local thread prior to a rcu_defer_barrier() call
212 * are guaranteed to be executed.
213 * Callbacks queued by other threads concurrently with rcu_defer_barrier()
214 * execution are not guaranteed to be executed in the current batch (could
215 * be left for the next batch). These callbacks queued by other threads are only
216 * guaranteed to be executed if there is explicit synchronization between
217 * the thread adding to the queue and the thread issuing the defer_barrier call.
220 void rcu_defer_barrier(void)
222 struct deferer_registry
*index
;
223 unsigned long num_items
= 0;
228 internal_urcu_lock(&urcu_defer_mutex
);
229 for (index
= registry
; index
< registry
+ num_deferers
; index
++) {
230 index
->last_head
= LOAD_SHARED(index
->defer_queue
->head
);
231 num_items
+= index
->last_head
- index
->defer_queue
->tail
;
233 if (likely(!num_items
)) {
235 * We skip the grace period because there are no queued
236 * callbacks to execute.
241 for (index
= registry
; index
< registry
+ num_deferers
; index
++)
242 rcu_defer_barrier_queue(index
->defer_queue
,
245 internal_urcu_unlock(&urcu_defer_mutex
);
249 * _rcu_defer_queue - Queue a RCU callback.
251 void _rcu_defer_queue(void (*fct
)(void *p
), void *p
)
253 unsigned long head
, tail
;
256 * Head is only modified by ourself. Tail can be modified by reclamation
259 head
= defer_queue
.head
;
260 tail
= LOAD_SHARED(defer_queue
.tail
);
263 * If queue is full, empty it ourself.
264 * Worse-case: must allow 2 supplementary entries for fct pointer.
266 if (unlikely(head
- tail
>= DEFER_QUEUE_SIZE
- 2)) {
267 assert(head
- tail
<= DEFER_QUEUE_SIZE
);
268 rcu_defer_barrier_thread();
269 assert(head
- LOAD_SHARED(defer_queue
.tail
) == 0);
272 if (unlikely(defer_queue
.last_fct_in
!= fct
)) {
273 defer_queue
.last_fct_in
= fct
;
274 if (unlikely(DQ_IS_FCT_BIT(fct
) || fct
== DQ_FCT_MARK
)) {
276 * If the function to encode is not aligned or the
277 * marker, write DQ_FCT_MARK followed by the function
280 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
282 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
286 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
290 if (unlikely(DQ_IS_FCT_BIT(p
) || p
== DQ_FCT_MARK
)) {
292 * If the data to encode is not aligned or the marker,
293 * write DQ_FCT_MARK followed by the function pointer.
295 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
297 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
301 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
], p
);
302 smp_wmb(); /* Publish new pointer before head */
303 /* Write q[] before head. */
304 STORE_SHARED(defer_queue
.head
, head
);
305 smp_mb(); /* Write queue head before read futex */
307 * Wake-up any waiting defer thread.
312 void *thr_defer(void *args
)
315 pthread_testcancel();
317 * "Be green". Don't wake up the CPU if there is no RCU work
318 * to perform whatsoever. Aims at saving laptop battery life by
319 * leaving the processor in sleep state when idle.
322 /* Sleeping after wait_defer to let many callbacks enqueue */
323 poll(NULL
,0,100); /* wait for 100ms */
331 * library wrappers to be used by non-LGPL compatible source code.
334 void rcu_defer_queue(void (*fct
)(void *p
), void *p
)
336 _rcu_defer_queue(fct
, p
);
339 static void rcu_add_deferer(pthread_t id
)
341 struct deferer_registry
*oldarray
;
344 alloc_deferers
= INIT_NUM_THREADS
;
347 malloc(sizeof(struct deferer_registry
) * alloc_deferers
);
349 if (alloc_deferers
< num_deferers
+ 1) {
351 registry
= malloc(sizeof(struct deferer_registry
)
352 * (alloc_deferers
<< 1));
353 memcpy(registry
, oldarray
,
354 sizeof(struct deferer_registry
) * alloc_deferers
);
355 alloc_deferers
<<= 1;
358 registry
[num_deferers
].tid
= id
;
359 /* reference to the TLS of _this_ deferer thread. */
360 registry
[num_deferers
].defer_queue
= &defer_queue
;
361 registry
[num_deferers
].last_head
= 0;
366 * Never shrink (implementation limitation).
367 * This is O(nb threads). Eventually use a hash table.
369 static void rcu_remove_deferer(pthread_t id
)
371 struct deferer_registry
*index
;
373 assert(registry
!= NULL
);
374 for (index
= registry
; index
< registry
+ num_deferers
; index
++) {
375 if (pthread_equal(index
->tid
, id
)) {
376 memcpy(index
, ®istry
[num_deferers
- 1],
377 sizeof(struct deferer_registry
));
378 registry
[num_deferers
- 1].tid
= 0;
379 registry
[num_deferers
- 1].defer_queue
= NULL
;
380 registry
[num_deferers
- 1].last_head
= 0;
385 /* Hrm not found, forgot to register ? */
389 static void start_defer_thread(void)
393 ret
= pthread_create(&tid_defer
, NULL
, thr_defer
,
398 static void stop_defer_thread(void)
403 pthread_cancel(tid_defer
);
405 ret
= pthread_join(tid_defer
, &tret
);
409 void rcu_defer_register_thread(void)
413 internal_urcu_lock(&defer_thread_mutex
);
414 internal_urcu_lock(&urcu_defer_mutex
);
415 defer_queue
.q
= malloc(sizeof(void *) * DEFER_QUEUE_SIZE
);
416 rcu_add_deferer(pthread_self());
417 deferers
= num_deferers
;
418 internal_urcu_unlock(&urcu_defer_mutex
);
421 start_defer_thread();
422 internal_urcu_unlock(&defer_thread_mutex
);
425 void rcu_defer_unregister_thread(void)
429 internal_urcu_lock(&defer_thread_mutex
);
430 internal_urcu_lock(&urcu_defer_mutex
);
431 rcu_remove_deferer(pthread_self());
432 _rcu_defer_barrier_thread();
434 defer_queue
.q
= NULL
;
435 deferers
= num_deferers
;
436 internal_urcu_unlock(&urcu_defer_mutex
);
440 internal_urcu_unlock(&defer_thread_mutex
);
443 void urcu_defer_exit(void)