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
;
75 * Wake-up any waiting defer thread. Called from many concurrent threads.
77 static void wake_up_defer(void)
79 if (unlikely(atomic_read(&defer_thread_futex
) == -1)) {
80 atomic_set(&defer_thread_futex
, 0);
81 futex(&defer_thread_futex
, FUTEX_WAKE
, 0,
87 * Defer thread waiting. Single thread.
89 static void wait_defer(void)
91 atomic_dec(&defer_thread_futex
);
92 if (atomic_read(&defer_thread_futex
) == -1)
93 futex(&defer_thread_futex
, FUTEX_WAIT
, -1,
97 static void internal_urcu_lock(pthread_mutex_t
*mutex
)
101 #ifndef DISTRUST_SIGNALS_EXTREME
102 ret
= pthread_mutex_lock(mutex
);
104 perror("Error in pthread mutex lock");
107 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
108 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
109 if (ret
!= EBUSY
&& ret
!= EINTR
) {
110 printf("ret = %d, errno = %d\n", ret
, errno
);
111 perror("Error in pthread mutex lock");
114 pthread_testcancel();
117 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
120 static void internal_urcu_unlock(pthread_mutex_t
*mutex
)
124 ret
= pthread_mutex_unlock(mutex
);
126 perror("Error in pthread mutex unlock");
132 * Must be called after Q.S. is reached.
134 static void rcu_defer_barrier_queue(struct defer_queue
*queue
,
138 void (*fct
)(void *p
);
142 * Tail is only modified when lock is held.
143 * Head is only modified by owner thread.
146 for (i
= queue
->tail
; i
!= head
;) {
147 smp_rmb(); /* read head before q[]. */
148 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
149 if (unlikely(DQ_IS_FCT_BIT(p
))) {
151 queue
->last_fct_out
= p
;
152 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
153 } else if (unlikely(p
== DQ_FCT_MARK
)) {
154 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
155 queue
->last_fct_out
= p
;
156 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
158 fct
= queue
->last_fct_out
;
161 smp_mb(); /* push tail after having used q[] */
162 STORE_SHARED(queue
->tail
, i
);
165 static void _rcu_defer_barrier_thread(void)
167 unsigned long head
, num_items
;
169 head
= defer_queue
.head
;
170 num_items
= head
- defer_queue
.tail
;
171 if (unlikely(!num_items
))
174 rcu_defer_barrier_queue(&defer_queue
, head
);
178 void rcu_defer_barrier_thread(void)
180 internal_urcu_lock(&urcu_defer_mutex
);
181 _rcu_defer_barrier_thread();
182 internal_urcu_unlock(&urcu_defer_mutex
);
186 * rcu_defer_barrier - Execute all queued rcu callbacks.
188 * Execute all RCU callbacks queued before rcu_defer_barrier() execution.
189 * All callbacks queued on the local thread prior to a rcu_defer_barrier() call
190 * are guaranteed to be executed.
191 * Callbacks queued by other threads concurrently with rcu_defer_barrier()
192 * execution are not guaranteed to be executed in the current batch (could
193 * be left for the next batch). These callbacks queued by other threads are only
194 * guaranteed to be executed if there is explicit synchronization between
195 * the thread adding to the queue and the thread issuing the defer_barrier call.
198 void rcu_defer_barrier(void)
200 struct deferer_registry
*index
;
201 unsigned long num_items
= 0;
206 internal_urcu_lock(&urcu_defer_mutex
);
207 for (index
= registry
; index
< registry
+ num_deferers
; index
++) {
208 index
->last_head
= LOAD_SHARED(index
->defer_queue
->head
);
209 num_items
+= index
->last_head
- index
->defer_queue
->tail
;
211 if (likely(!num_items
)) {
213 * We skip the grace period because there are no queued
214 * callbacks to execute.
219 for (index
= registry
; index
< registry
+ num_deferers
; index
++)
220 rcu_defer_barrier_queue(index
->defer_queue
,
223 internal_urcu_unlock(&urcu_defer_mutex
);
227 * _rcu_defer_queue - Queue a RCU callback.
229 void _rcu_defer_queue(void (*fct
)(void *p
), void *p
)
231 unsigned long head
, tail
;
234 * Head is only modified by ourself. Tail can be modified by reclamation
237 head
= defer_queue
.head
;
238 tail
= LOAD_SHARED(defer_queue
.tail
);
241 * If queue is full, empty it ourself.
242 * Worse-case: must allow 2 supplementary entries for fct pointer.
244 if (unlikely(head
- tail
>= DEFER_QUEUE_SIZE
- 2)) {
245 assert(head
- tail
<= DEFER_QUEUE_SIZE
);
246 rcu_defer_barrier_thread();
247 assert(head
- LOAD_SHARED(defer_queue
.tail
) == 0);
250 if (unlikely(defer_queue
.last_fct_in
!= fct
)) {
251 defer_queue
.last_fct_in
= fct
;
252 if (unlikely(DQ_IS_FCT_BIT(fct
) || fct
== DQ_FCT_MARK
)) {
254 * If the function to encode is not aligned or the
255 * marker, write DQ_FCT_MARK followed by the function
258 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
260 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
264 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
268 if (unlikely(DQ_IS_FCT_BIT(p
) || p
== DQ_FCT_MARK
)) {
270 * If the data to encode is not aligned or the marker,
271 * write DQ_FCT_MARK followed by the function pointer.
273 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
275 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
279 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
], p
);
280 smp_wmb(); /* Publish new pointer before head */
281 /* Write q[] before head. */
282 STORE_SHARED(defer_queue
.head
, head
);
284 * Wake-up any waiting defer thread.
289 void *thr_defer(void *args
)
292 pthread_testcancel();
294 * "Be green". Don't wake up the CPU if there is no RCU work
295 * to perform whatsoever. Aims at saving laptop battery life by
296 * leaving the processor in sleep state when idle.
299 /* Sleeping after wait_defer to let many callbacks enqueue */
300 poll(NULL
,0,100); /* wait for 100ms */
308 * library wrappers to be used by non-LGPL compatible source code.
311 void rcu_defer_queue(void (*fct
)(void *p
), void *p
)
313 _rcu_defer_queue(fct
, p
);
316 static void rcu_add_deferer(pthread_t id
)
318 struct deferer_registry
*oldarray
;
321 alloc_deferers
= INIT_NUM_THREADS
;
324 malloc(sizeof(struct deferer_registry
) * alloc_deferers
);
326 if (alloc_deferers
< num_deferers
+ 1) {
328 registry
= malloc(sizeof(struct deferer_registry
)
329 * (alloc_deferers
<< 1));
330 memcpy(registry
, oldarray
,
331 sizeof(struct deferer_registry
) * alloc_deferers
);
332 alloc_deferers
<<= 1;
335 registry
[num_deferers
].tid
= id
;
336 /* reference to the TLS of _this_ deferer thread. */
337 registry
[num_deferers
].defer_queue
= &defer_queue
;
338 registry
[num_deferers
].last_head
= 0;
343 * Never shrink (implementation limitation).
344 * This is O(nb threads). Eventually use a hash table.
346 static void rcu_remove_deferer(pthread_t id
)
348 struct deferer_registry
*index
;
350 assert(registry
!= NULL
);
351 for (index
= registry
; index
< registry
+ num_deferers
; index
++) {
352 if (pthread_equal(index
->tid
, id
)) {
353 memcpy(index
, ®istry
[num_deferers
- 1],
354 sizeof(struct deferer_registry
));
355 registry
[num_deferers
- 1].tid
= 0;
356 registry
[num_deferers
- 1].defer_queue
= NULL
;
357 registry
[num_deferers
- 1].last_head
= 0;
362 /* Hrm not found, forgot to register ? */
366 static void start_defer_thread(void)
370 ret
= pthread_create(&tid_defer
, NULL
, thr_defer
,
375 static void stop_defer_thread(void)
380 pthread_cancel(tid_defer
);
382 ret
= pthread_join(tid_defer
, &tret
);
386 void rcu_defer_register_thread(void)
390 internal_urcu_lock(&defer_thread_mutex
);
391 internal_urcu_lock(&urcu_defer_mutex
);
392 defer_queue
.q
= malloc(sizeof(void *) * DEFER_QUEUE_SIZE
);
393 rcu_add_deferer(pthread_self());
394 deferers
= num_deferers
;
395 internal_urcu_unlock(&urcu_defer_mutex
);
398 start_defer_thread();
399 internal_urcu_unlock(&defer_thread_mutex
);
402 void rcu_defer_unregister_thread(void)
406 internal_urcu_lock(&defer_thread_mutex
);
407 internal_urcu_lock(&urcu_defer_mutex
);
408 rcu_remove_deferer(pthread_self());
409 _rcu_defer_barrier_thread();
411 defer_queue
.q
= NULL
;
412 deferers
= num_deferers
;
413 internal_urcu_unlock(&urcu_defer_mutex
);
417 internal_urcu_unlock(&defer_thread_mutex
);
420 void urcu_defer_exit(void)