1 /* SPDX-License-Identifier: GPL-2.0-or-later */
7 * This API defers a function call within a defer_call_begin()/defer_call_end()
8 * section, allowing multiple calls to batch up. This is a performance
9 * optimization that is used in the block layer to submit several I/O requests
10 * at once instead of individually:
12 * defer_call_begin(); <-- start of section
14 * defer_call(my_func, my_obj); <-- deferred my_func(my_obj) call
15 * defer_call(my_func, my_obj); <-- another
16 * defer_call(my_func, my_obj); <-- another
18 * defer_call_end(); <-- end of section, my_func(my_obj) is called once
21 #include "qemu/osdep.h"
22 #include "qemu/coroutine-tls.h"
23 #include "qemu/notify.h"
24 #include "qemu/thread.h"
25 #include "qemu/defer-call.h"
27 /* A function call that has been deferred until defer_call_end() */
33 /* Per-thread state */
35 unsigned nesting_level
;
36 GArray
*deferred_call_array
;
37 } DeferCallThreadState
;
39 /* Use get_ptr_defer_call_thread_state() to fetch this thread-local value */
40 QEMU_DEFINE_STATIC_CO_TLS(DeferCallThreadState
, defer_call_thread_state
);
42 /* Called at thread cleanup time */
43 static void defer_call_atexit(Notifier
*n
, void *value
)
45 DeferCallThreadState
*thread_state
= get_ptr_defer_call_thread_state();
46 g_array_free(thread_state
->deferred_call_array
, TRUE
);
49 /* This won't involve coroutines, so use __thread */
50 static __thread Notifier defer_call_atexit_notifier
;
54 * @fn: a function pointer to be invoked
55 * @opaque: a user-defined argument to @fn()
57 * Call @fn(@opaque) immediately if not within a
58 * defer_call_begin()/defer_call_end() section.
60 * Otherwise defer the call until the end of the outermost
61 * defer_call_begin()/defer_call_end() section in this thread. If the same
62 * @fn/@opaque pair has already been deferred, it will only be called once upon
63 * defer_call_end() so that accumulated calls are batched into a single call.
65 * The caller must ensure that @opaque is not freed before @fn() is invoked.
67 void defer_call(void (*fn
)(void *), void *opaque
)
69 DeferCallThreadState
*thread_state
= get_ptr_defer_call_thread_state();
71 /* Call immediately if we're not deferring calls */
72 if (thread_state
->nesting_level
== 0) {
77 GArray
*array
= thread_state
->deferred_call_array
;
79 array
= g_array_new(FALSE
, FALSE
, sizeof(DeferredCall
));
80 thread_state
->deferred_call_array
= array
;
81 defer_call_atexit_notifier
.notify
= defer_call_atexit
;
82 qemu_thread_atexit_add(&defer_call_atexit_notifier
);
85 DeferredCall
*fns
= (DeferredCall
*)array
->data
;
86 DeferredCall new_fn
= {
92 * There won't be many, so do a linear search. If this becomes a bottleneck
93 * then a binary search (glib 2.62+) or different data structure could be
96 for (guint i
= 0; i
< array
->len
; i
++) {
97 if (memcmp(&fns
[i
], &new_fn
, sizeof(new_fn
)) == 0) {
98 return; /* already exists */
102 g_array_append_val(array
, new_fn
);
106 * defer_call_begin: Defer defer_call() functions until defer_call_end()
108 * defer_call_begin() and defer_call_end() are thread-local operations. The
109 * caller must ensure that each defer_call_begin() has a matching
110 * defer_call_end() in the same thread.
112 * Nesting is supported. defer_call() functions are only called at the
113 * outermost defer_call_end().
115 void defer_call_begin(void)
117 DeferCallThreadState
*thread_state
= get_ptr_defer_call_thread_state();
119 assert(thread_state
->nesting_level
< UINT32_MAX
);
121 thread_state
->nesting_level
++;
125 * defer_call_end: Run any pending defer_call() functions
127 * There must have been a matching defer_call_begin() call in the same thread
128 * prior to this defer_call_end() call.
130 void defer_call_end(void)
132 DeferCallThreadState
*thread_state
= get_ptr_defer_call_thread_state();
134 assert(thread_state
->nesting_level
> 0);
136 if (--thread_state
->nesting_level
> 0) {
140 GArray
*array
= thread_state
->deferred_call_array
;
145 DeferredCall
*fns
= (DeferredCall
*)array
->data
;
147 for (guint i
= 0; i
< array
->len
; i
++) {
148 fns
[i
].fn(fns
[i
].opaque
);
152 * This resets the array without freeing memory so that appending is cheap
155 g_array_set_size(array
, 0);