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 int defer_thread_futex
;
55 * Written to only by each individual deferer. Read by both the deferer and
56 * the reclamation tread.
58 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
,
86 * Defer thread waiting. Single thread.
88 static void wait_defer(void)
90 atomic_dec(&defer_thread_futex
);
91 if (atomic_read(&defer_thread_futex
) == -1)
92 futex(&defer_thread_futex
, FUTEX_WAIT
, -1,
96 static void internal_urcu_lock(pthread_mutex_t
*mutex
)
100 #ifndef DISTRUST_SIGNALS_EXTREME
101 ret
= pthread_mutex_lock(mutex
);
103 perror("Error in pthread mutex lock");
106 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
107 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
108 if (ret
!= EBUSY
&& ret
!= EINTR
) {
109 printf("ret = %d, errno = %d\n", ret
, errno
);
110 perror("Error in pthread mutex lock");
113 pthread_testcancel();
116 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
119 static void internal_urcu_unlock(pthread_mutex_t
*mutex
)
123 ret
= pthread_mutex_unlock(mutex
);
125 perror("Error in pthread mutex unlock");
131 * Must be called after Q.S. is reached.
133 static void rcu_defer_barrier_queue(struct defer_queue
*queue
,
137 void (*fct
)(void *p
);
141 * Tail is only modified when lock is held.
142 * Head is only modified by owner thread.
145 for (i
= queue
->tail
; i
!= head
;) {
146 smp_rmb(); /* read head before q[]. */
147 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
148 if (unlikely(DQ_IS_FCT_BIT(p
))) {
150 queue
->last_fct_out
= p
;
151 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
152 } else if (unlikely(p
== DQ_FCT_MARK
)) {
153 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
154 queue
->last_fct_out
= p
;
155 p
= LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
157 fct
= queue
->last_fct_out
;
160 smp_mb(); /* push tail after having used q[] */
161 STORE_SHARED(queue
->tail
, i
);
164 static void _rcu_defer_barrier_thread(void)
166 unsigned long head
, num_items
;
168 head
= defer_queue
.head
;
169 num_items
= head
- defer_queue
.tail
;
170 if (unlikely(!num_items
))
173 rcu_defer_barrier_queue(&defer_queue
, head
);
177 void rcu_defer_barrier_thread(void)
179 internal_urcu_lock(&urcu_defer_mutex
);
180 _rcu_defer_barrier_thread();
181 internal_urcu_unlock(&urcu_defer_mutex
);
185 * rcu_defer_barrier - Execute all queued rcu callbacks.
187 * Execute all RCU callbacks queued before rcu_defer_barrier() execution.
188 * All callbacks queued on the local thread prior to a rcu_defer_barrier() call
189 * are guaranteed to be executed.
190 * Callbacks queued by other threads concurrently with rcu_defer_barrier()
191 * execution are not guaranteed to be executed in the current batch (could
192 * be left for the next batch). These callbacks queued by other threads are only
193 * guaranteed to be executed if there is explicit synchronization between
194 * the thread adding to the queue and the thread issuing the defer_barrier call.
197 void rcu_defer_barrier(void)
199 struct deferer_registry
*index
;
200 unsigned long num_items
= 0;
205 internal_urcu_lock(&urcu_defer_mutex
);
206 for (index
= registry
; index
< registry
+ num_deferers
; index
++) {
207 index
->last_head
= LOAD_SHARED(index
->defer_queue
->head
);
208 num_items
+= index
->last_head
- index
->defer_queue
->tail
;
210 if (likely(!num_items
)) {
212 * We skip the grace period because there are no queued
213 * callbacks to execute.
218 for (index
= registry
; index
< registry
+ num_deferers
; index
++)
219 rcu_defer_barrier_queue(index
->defer_queue
,
222 internal_urcu_unlock(&urcu_defer_mutex
);
226 * _rcu_defer_queue - Queue a RCU callback.
228 void _rcu_defer_queue(void (*fct
)(void *p
), void *p
)
230 unsigned long head
, tail
;
233 * Head is only modified by ourself. Tail can be modified by reclamation
236 head
= defer_queue
.head
;
237 tail
= LOAD_SHARED(defer_queue
.tail
);
240 * If queue is full, empty it ourself.
241 * Worse-case: must allow 2 supplementary entries for fct pointer.
243 if (unlikely(head
- tail
>= DEFER_QUEUE_SIZE
- 2)) {
244 assert(head
- tail
<= DEFER_QUEUE_SIZE
);
245 rcu_defer_barrier_thread();
246 assert(head
- LOAD_SHARED(defer_queue
.tail
) == 0);
249 if (unlikely(defer_queue
.last_fct_in
!= fct
)) {
250 defer_queue
.last_fct_in
= fct
;
251 if (unlikely(DQ_IS_FCT_BIT(fct
) || fct
== DQ_FCT_MARK
)) {
253 * If the function to encode is not aligned or the
254 * marker, write DQ_FCT_MARK followed by the function
257 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
259 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
263 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
267 if (unlikely(DQ_IS_FCT_BIT(p
) || p
== DQ_FCT_MARK
)) {
269 * If the data to encode is not aligned or the marker,
270 * write DQ_FCT_MARK followed by the function pointer.
272 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
274 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
],
278 _STORE_SHARED(defer_queue
.q
[head
++ & DEFER_QUEUE_MASK
], p
);
279 smp_wmb(); /* Publish new pointer before head */
280 /* Write q[] before head. */
281 STORE_SHARED(defer_queue
.head
, head
);
283 * Wake-up any waiting defer thread.
288 void *thr_defer(void *args
)
291 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.
301 /* Sleeping after wait_defer to let many callbacks enqueue */
302 //TEST poll(NULL,0,100); /* wait for 100ms */
305 printf("perform deferred call_rcu() from worker thread %lu.\n",
313 * library wrappers to be used by non-LGPL compatible source code.
316 void rcu_defer_queue(void (*fct
)(void *p
), void *p
)
318 _rcu_defer_queue(fct
, p
);
321 static void rcu_add_deferer(pthread_t id
)
323 struct deferer_registry
*oldarray
;
326 alloc_deferers
= INIT_NUM_THREADS
;
329 malloc(sizeof(struct deferer_registry
) * alloc_deferers
);
331 if (alloc_deferers
< num_deferers
+ 1) {
333 registry
= malloc(sizeof(struct deferer_registry
)
334 * (alloc_deferers
<< 1));
335 memcpy(registry
, oldarray
,
336 sizeof(struct deferer_registry
) * alloc_deferers
);
337 alloc_deferers
<<= 1;
340 registry
[num_deferers
].tid
= id
;
341 /* reference to the TLS of _this_ deferer thread. */
342 registry
[num_deferers
].defer_queue
= &defer_queue
;
343 registry
[num_deferers
].last_head
= 0;
348 * Never shrink (implementation limitation).
349 * This is O(nb threads). Eventually use a hash table.
351 static void rcu_remove_deferer(pthread_t id
)
353 struct deferer_registry
*index
;
355 assert(registry
!= NULL
);
356 for (index
= registry
; index
< registry
+ num_deferers
; index
++) {
357 if (pthread_equal(index
->tid
, id
)) {
358 memcpy(index
, ®istry
[num_deferers
- 1],
359 sizeof(struct deferer_registry
));
360 registry
[num_deferers
- 1].tid
= 0;
361 registry
[num_deferers
- 1].defer_queue
= NULL
;
362 registry
[num_deferers
- 1].last_head
= 0;
367 /* Hrm not found, forgot to register ? */
371 static void start_defer_thread(void)
375 ret
= pthread_create(&tid_defer
, NULL
, thr_defer
,
380 static void stop_defer_thread(void)
385 pthread_cancel(tid_defer
);
387 ret
= pthread_join(tid_defer
, &tret
);
391 void rcu_defer_register_thread(void)
395 internal_urcu_lock(&defer_thread_mutex
);
396 internal_urcu_lock(&urcu_defer_mutex
);
397 defer_queue
.q
= malloc(sizeof(void *) * DEFER_QUEUE_SIZE
);
398 rcu_add_deferer(pthread_self());
399 deferers
= num_deferers
;
400 internal_urcu_unlock(&urcu_defer_mutex
);
403 start_defer_thread();
404 internal_urcu_unlock(&defer_thread_mutex
);
407 void rcu_defer_unregister_thread(void)
411 internal_urcu_lock(&defer_thread_mutex
);
412 internal_urcu_lock(&urcu_defer_mutex
);
413 rcu_remove_deferer(pthread_self());
414 _rcu_defer_barrier_thread();
416 defer_queue
.q
= NULL
;
417 deferers
= num_deferers
;
418 internal_urcu_unlock(&urcu_defer_mutex
);
422 internal_urcu_unlock(&defer_thread_mutex
);
425 void urcu_defer_exit(void)