1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 // SPDX-FileCopyrightText: 2009 Paul E. McKenney, IBM Corporation.
4 // SPDX-License-Identifier: LGPL-2.1-or-later
6 #ifndef _URCU_DEFER_IMPL_H
7 #define _URCU_DEFER_IMPL_H
10 * Userspace RCU header - memory reclamation.
12 * TO BE INCLUDED ONLY FROM URCU LIBRARY CODE. See urcu-defer.h for linking
13 * dynamically with the userspace rcu reclamation library.
15 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
29 #include "urcu/futex.h"
31 #include <urcu/assert.h>
32 #include <urcu/compiler.h>
33 #include <urcu/arch.h>
34 #include <urcu/uatomic.h>
35 #include <urcu/list.h>
36 #include <urcu/system.h>
37 #include <urcu/tls-compat.h>
39 #include "urcu-utils.h"
42 * Number of entries in the per-thread defer queue. Must be power of 2.
44 #define DEFER_QUEUE_SIZE (1 << 12)
45 #define DEFER_QUEUE_MASK (DEFER_QUEUE_SIZE - 1)
48 * Typically, data is aligned at least on the architecture size.
49 * Use lowest bit to indicate that the current callback is changing.
50 * Assumes that (void *)-2L is not used often. Used to encode non-aligned
51 * functions and non-aligned data using extra space.
52 * We encode the (void *)-2L fct as: -2L, fct, data.
53 * We encode the (void *)-2L data as either:
54 * fct | DQ_FCT_BIT, data (if fct is aligned), or
55 * -2L, fct, data (if fct is not aligned).
56 * Here, DQ_FCT_MARK == ~DQ_FCT_BIT. Required for the test order.
58 #define DQ_FCT_BIT (1 << 0)
59 #define DQ_IS_FCT_BIT(x) ((unsigned long)(x) & DQ_FCT_BIT)
60 #define DQ_SET_FCT_BIT(x) \
61 (x = (void *)((unsigned long)(x) | DQ_FCT_BIT))
62 #define DQ_CLEAR_FCT_BIT(x) \
63 (x = (void *)((unsigned long)(x) & ~DQ_FCT_BIT))
64 #define DQ_FCT_MARK ((void *)(~DQ_FCT_BIT))
67 * This code section can only be included in LGPL 2.1 compatible source code.
68 * See below for the function call wrappers which can be used in code meant to
69 * be only linked with the Userspace RCU library. This comes with a small
70 * performance degradation on the read-side due to the added function calls.
71 * This is required to permit relinking with newer versions of the library.
76 * Contains pointers. Encoded to save space when same callback is often used.
77 * When looking up the next item:
78 * - if DQ_FCT_BIT is set, set the current callback to DQ_CLEAR_FCT_BIT(ptr)
79 * - next element contains pointer to data.
80 * - else if item == DQ_FCT_MARK
81 * - set the current callback to next element ptr
82 * - following next element contains pointer to data.
83 * - else current element contains data
86 unsigned long head
; /* add element at head */
87 void *last_fct_in
; /* last fct pointer encoded */
88 unsigned long tail
; /* next element to remove at tail */
89 void *last_fct_out
; /* last fct pointer encoded */
91 /* registry information */
92 unsigned long last_head
;
93 struct cds_list_head list
; /* list of thread queues */
96 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
97 #include <urcu/defer.h>
99 void __attribute__((destructor
)) rcu_defer_exit(void);
101 extern void synchronize_rcu(void);
104 * rcu_defer_mutex nests inside defer_thread_mutex.
106 static pthread_mutex_t rcu_defer_mutex
= PTHREAD_MUTEX_INITIALIZER
;
107 static pthread_mutex_t defer_thread_mutex
= PTHREAD_MUTEX_INITIALIZER
;
109 static int32_t defer_thread_futex
;
110 static int32_t defer_thread_stop
;
113 * Written to only by each individual deferer. Read by both the deferer and
114 * the reclamation tread.
116 static DEFINE_URCU_TLS(struct defer_queue
, defer_queue
);
117 static CDS_LIST_HEAD(registry_defer
);
118 static pthread_t tid_defer
;
120 static void mutex_lock_defer(pthread_mutex_t
*mutex
)
124 #ifndef DISTRUST_SIGNALS_EXTREME
125 ret
= pthread_mutex_lock(mutex
);
128 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
129 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
130 if (ret
!= EBUSY
&& ret
!= EINTR
)
132 (void) poll(NULL
,0,10);
134 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
138 * Wake-up any waiting defer thread. Called from many concurrent threads.
140 static void wake_up_defer(void)
142 if (caa_unlikely(uatomic_read(&defer_thread_futex
) == -1)) {
143 uatomic_set(&defer_thread_futex
, 0);
144 if (futex_noasync(&defer_thread_futex
, FUTEX_WAKE
, 1,
150 static unsigned long rcu_defer_num_callbacks(void)
152 unsigned long num_items
= 0, head
;
153 struct defer_queue
*index
;
155 mutex_lock_defer(&rcu_defer_mutex
);
156 cds_list_for_each_entry(index
, ®istry_defer
, list
) {
157 head
= CMM_LOAD_SHARED(index
->head
);
158 num_items
+= head
- index
->tail
;
160 mutex_unlock(&rcu_defer_mutex
);
165 * Defer thread waiting. Single thread.
167 static void wait_defer(void)
169 uatomic_dec(&defer_thread_futex
);
170 /* Write futex before read queue */
171 /* Write futex before read defer_thread_stop */
173 if (_CMM_LOAD_SHARED(defer_thread_stop
)) {
174 uatomic_set(&defer_thread_futex
, 0);
177 if (rcu_defer_num_callbacks()) {
178 cmm_smp_mb(); /* Read queue before write futex */
179 /* Callbacks are queued, don't wait. */
180 uatomic_set(&defer_thread_futex
, 0);
182 cmm_smp_rmb(); /* Read queue before read futex */
183 while (uatomic_read(&defer_thread_futex
) == -1) {
184 if (!futex_noasync(&defer_thread_futex
, FUTEX_WAIT
, -1, NULL
, NULL
, 0)) {
186 * Prior queued wakeups queued by unrelated code
187 * using the same address can cause futex wait to
188 * return 0 even through the futex value is still
189 * -1 (spurious wakeups). Check the value again
190 * in user-space to validate whether it really
197 /* Value already changed. */
200 /* Retry if interrupted by signal. */
201 break; /* Get out of switch. Check again. */
203 /* Unexpected error. */
211 * Must be called after Q.S. is reached.
213 static void rcu_defer_barrier_queue(struct defer_queue
*queue
,
217 void (*fct
)(void *p
);
221 * Tail is only modified when lock is held.
222 * Head is only modified by owner thread.
225 for (i
= queue
->tail
; i
!= head
;) {
226 cmm_smp_rmb(); /* read head before q[]. */
227 p
= CMM_LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
228 if (caa_unlikely(DQ_IS_FCT_BIT(p
))) {
230 queue
->last_fct_out
= p
;
231 p
= CMM_LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
232 } else if (caa_unlikely(p
== DQ_FCT_MARK
)) {
233 p
= CMM_LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
234 queue
->last_fct_out
= p
;
235 p
= CMM_LOAD_SHARED(queue
->q
[i
++ & DEFER_QUEUE_MASK
]);
237 fct
= queue
->last_fct_out
;
240 cmm_smp_mb(); /* push tail after having used q[] */
241 CMM_STORE_SHARED(queue
->tail
, i
);
244 static void _rcu_defer_barrier_thread(void)
246 unsigned long head
, num_items
;
248 head
= URCU_TLS(defer_queue
).head
;
249 num_items
= head
- URCU_TLS(defer_queue
).tail
;
250 if (caa_unlikely(!num_items
))
253 rcu_defer_barrier_queue(&URCU_TLS(defer_queue
), head
);
256 void rcu_defer_barrier_thread(void)
258 mutex_lock_defer(&rcu_defer_mutex
);
259 _rcu_defer_barrier_thread();
260 mutex_unlock(&rcu_defer_mutex
);
264 * rcu_defer_barrier - Execute all queued rcu callbacks.
266 * Execute all RCU callbacks queued before rcu_defer_barrier() execution.
267 * All callbacks queued on the local thread prior to a rcu_defer_barrier() call
268 * are guaranteed to be executed.
269 * Callbacks queued by other threads concurrently with rcu_defer_barrier()
270 * execution are not guaranteed to be executed in the current batch (could
271 * be left for the next batch). These callbacks queued by other threads are only
272 * guaranteed to be executed if there is explicit synchronization between
273 * the thread adding to the queue and the thread issuing the defer_barrier call.
276 void rcu_defer_barrier(void)
278 struct defer_queue
*index
;
279 unsigned long num_items
= 0;
281 if (cds_list_empty(®istry_defer
))
284 mutex_lock_defer(&rcu_defer_mutex
);
285 cds_list_for_each_entry(index
, ®istry_defer
, list
) {
286 index
->last_head
= CMM_LOAD_SHARED(index
->head
);
287 num_items
+= index
->last_head
- index
->tail
;
289 if (caa_likely(!num_items
)) {
291 * We skip the grace period because there are no queued
292 * callbacks to execute.
297 cds_list_for_each_entry(index
, ®istry_defer
, list
)
298 rcu_defer_barrier_queue(index
, index
->last_head
);
300 mutex_unlock(&rcu_defer_mutex
);
304 * _defer_rcu - Queue a RCU callback.
306 static void _defer_rcu(void (*fct
)(void *p
), void *p
)
308 unsigned long head
, tail
;
311 * Head is only modified by ourself. Tail can be modified by reclamation
314 head
= URCU_TLS(defer_queue
).head
;
315 tail
= CMM_LOAD_SHARED(URCU_TLS(defer_queue
).tail
);
318 * If queue is full, or reached threshold. Empty queue ourself.
319 * Worse-case: must allow 2 supplementary entries for fct pointer.
321 if (caa_unlikely(head
- tail
>= DEFER_QUEUE_SIZE
- 2)) {
322 urcu_posix_assert(head
- tail
<= DEFER_QUEUE_SIZE
);
323 rcu_defer_barrier_thread();
324 urcu_posix_assert(head
- CMM_LOAD_SHARED(URCU_TLS(defer_queue
).tail
) == 0);
329 * if the function is not changed and the data is aligned and it is
332 * otherwise if the function is aligned and its not the marker:
333 * store the function with DQ_FCT_BIT
336 * store the marker (DQ_FCT_MARK)
340 * Decode: see the comments before 'struct defer_queue'
341 * or the code in rcu_defer_barrier_queue().
343 if (caa_unlikely(URCU_TLS(defer_queue
).last_fct_in
!= fct
345 || p
== DQ_FCT_MARK
)) {
346 URCU_TLS(defer_queue
).last_fct_in
= fct
;
347 if (caa_unlikely(DQ_IS_FCT_BIT(fct
) || fct
== DQ_FCT_MARK
)) {
348 _CMM_STORE_SHARED(URCU_TLS(defer_queue
).q
[head
++ & DEFER_QUEUE_MASK
],
350 _CMM_STORE_SHARED(URCU_TLS(defer_queue
).q
[head
++ & DEFER_QUEUE_MASK
],
354 _CMM_STORE_SHARED(URCU_TLS(defer_queue
).q
[head
++ & DEFER_QUEUE_MASK
],
358 _CMM_STORE_SHARED(URCU_TLS(defer_queue
).q
[head
++ & DEFER_QUEUE_MASK
], p
);
359 cmm_smp_wmb(); /* Publish new pointer before head */
360 /* Write q[] before head. */
361 CMM_STORE_SHARED(URCU_TLS(defer_queue
).head
, head
);
362 cmm_smp_mb(); /* Write queue head before read futex */
364 * Wake-up any waiting defer thread.
369 static void *thr_defer(void *args
__attribute__((unused
)))
373 * "Be green". Don't wake up the CPU if there is no RCU work
374 * to perform whatsoever. Aims at saving laptop battery life by
375 * leaving the processor in sleep state when idle.
378 /* Sleeping after wait_defer to let many callbacks enqueue */
379 (void) poll(NULL
,0,100); /* wait for 100ms */
387 * library wrappers to be used by non-LGPL compatible source code.
390 void defer_rcu(void (*fct
)(void *p
), void *p
)
395 static void start_defer_thread(void)
398 sigset_t newmask
, oldmask
;
400 ret
= sigfillset(&newmask
);
401 urcu_posix_assert(!ret
);
402 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
403 urcu_posix_assert(!ret
);
405 ret
= pthread_create(&tid_defer
, NULL
, thr_defer
, NULL
);
409 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
410 urcu_posix_assert(!ret
);
413 static void stop_defer_thread(void)
418 _CMM_STORE_SHARED(defer_thread_stop
, 1);
419 /* Store defer_thread_stop before testing futex */
423 ret
= pthread_join(tid_defer
, &tret
);
424 urcu_posix_assert(!ret
);
426 CMM_STORE_SHARED(defer_thread_stop
, 0);
427 /* defer thread should always exit when futex value is 0 */
428 urcu_posix_assert(uatomic_read(&defer_thread_futex
) == 0);
431 int rcu_defer_register_thread(void)
435 urcu_posix_assert(URCU_TLS(defer_queue
).last_head
== 0);
436 urcu_posix_assert(URCU_TLS(defer_queue
).q
== NULL
);
437 URCU_TLS(defer_queue
).q
= malloc(sizeof(void *) * DEFER_QUEUE_SIZE
);
438 if (!URCU_TLS(defer_queue
).q
)
441 mutex_lock_defer(&defer_thread_mutex
);
442 mutex_lock_defer(&rcu_defer_mutex
);
443 was_empty
= cds_list_empty(®istry_defer
);
444 cds_list_add(&URCU_TLS(defer_queue
).list
, ®istry_defer
);
445 mutex_unlock(&rcu_defer_mutex
);
448 start_defer_thread();
449 mutex_unlock(&defer_thread_mutex
);
453 void rcu_defer_unregister_thread(void)
457 mutex_lock_defer(&defer_thread_mutex
);
458 mutex_lock_defer(&rcu_defer_mutex
);
459 cds_list_del(&URCU_TLS(defer_queue
).list
);
460 _rcu_defer_barrier_thread();
461 free(URCU_TLS(defer_queue
).q
);
462 URCU_TLS(defer_queue
).q
= NULL
;
463 is_empty
= cds_list_empty(®istry_defer
);
464 mutex_unlock(&rcu_defer_mutex
);
468 mutex_unlock(&defer_thread_mutex
);
471 void rcu_defer_exit(void)
473 urcu_posix_assert(cds_list_empty(®istry_defer
));
476 #endif /* _URCU_DEFER_IMPL_H */