]>
Commit | Line | Data |
---|---|---|
41abca8c SH |
1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
2 | /* | |
ccee48aa | 3 | * Deferred calls |
41abca8c SH |
4 | * |
5 | * Copyright Red Hat. | |
6 | * | |
ccee48aa | 7 | * This API defers a function call within a defer_call_begin()/defer_call_end() |
41abca8c SH |
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: | |
11 | * | |
ccee48aa | 12 | * defer_call_begin(); <-- start of section |
41abca8c | 13 | * ... |
ccee48aa SH |
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 | |
41abca8c | 17 | * ... |
ccee48aa | 18 | * defer_call_end(); <-- end of section, my_func(my_obj) is called once |
41abca8c SH |
19 | */ |
20 | ||
21 | #include "qemu/osdep.h" | |
22 | #include "qemu/coroutine-tls.h" | |
23 | #include "qemu/notify.h" | |
24 | #include "qemu/thread.h" | |
25 | #include "sysemu/block-backend.h" | |
26 | ||
ccee48aa | 27 | /* A function call that has been deferred until defer_call_end() */ |
41abca8c SH |
28 | typedef struct { |
29 | void (*fn)(void *); | |
30 | void *opaque; | |
ccee48aa | 31 | } DeferredCall; |
41abca8c SH |
32 | |
33 | /* Per-thread state */ | |
34 | typedef struct { | |
ccee48aa SH |
35 | unsigned nesting_level; |
36 | GArray *deferred_call_array; | |
37 | } DeferCallThreadState; | |
41abca8c | 38 | |
ccee48aa SH |
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); | |
41abca8c SH |
41 | |
42 | /* Called at thread cleanup time */ | |
ccee48aa | 43 | static void defer_call_atexit(Notifier *n, void *value) |
41abca8c | 44 | { |
ccee48aa SH |
45 | DeferCallThreadState *thread_state = get_ptr_defer_call_thread_state(); |
46 | g_array_free(thread_state->deferred_call_array, TRUE); | |
41abca8c SH |
47 | } |
48 | ||
49 | /* This won't involve coroutines, so use __thread */ | |
ccee48aa | 50 | static __thread Notifier defer_call_atexit_notifier; |
41abca8c SH |
51 | |
52 | /** | |
ccee48aa | 53 | * defer_call: |
41abca8c SH |
54 | * @fn: a function pointer to be invoked |
55 | * @opaque: a user-defined argument to @fn() | |
56 | * | |
ccee48aa SH |
57 | * Call @fn(@opaque) immediately if not within a |
58 | * defer_call_begin()/defer_call_end() section. | |
41abca8c SH |
59 | * |
60 | * Otherwise defer the call until the end of the outermost | |
ccee48aa | 61 | * defer_call_begin()/defer_call_end() section in this thread. If the same |
41abca8c | 62 | * @fn/@opaque pair has already been deferred, it will only be called once upon |
ccee48aa | 63 | * defer_call_end() so that accumulated calls are batched into a single call. |
41abca8c SH |
64 | * |
65 | * The caller must ensure that @opaque is not freed before @fn() is invoked. | |
66 | */ | |
ccee48aa | 67 | void defer_call(void (*fn)(void *), void *opaque) |
41abca8c | 68 | { |
ccee48aa | 69 | DeferCallThreadState *thread_state = get_ptr_defer_call_thread_state(); |
41abca8c | 70 | |
ccee48aa SH |
71 | /* Call immediately if we're not deferring calls */ |
72 | if (thread_state->nesting_level == 0) { | |
41abca8c SH |
73 | fn(opaque); |
74 | return; | |
75 | } | |
76 | ||
ccee48aa | 77 | GArray *array = thread_state->deferred_call_array; |
41abca8c | 78 | if (!array) { |
ccee48aa SH |
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); | |
41abca8c SH |
83 | } |
84 | ||
ccee48aa SH |
85 | DeferredCall *fns = (DeferredCall *)array->data; |
86 | DeferredCall new_fn = { | |
41abca8c SH |
87 | .fn = fn, |
88 | .opaque = opaque, | |
89 | }; | |
90 | ||
91 | /* | |
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 | |
94 | * used. | |
95 | */ | |
96 | for (guint i = 0; i < array->len; i++) { | |
97 | if (memcmp(&fns[i], &new_fn, sizeof(new_fn)) == 0) { | |
98 | return; /* already exists */ | |
99 | } | |
100 | } | |
101 | ||
102 | g_array_append_val(array, new_fn); | |
103 | } | |
104 | ||
105 | /** | |
ccee48aa | 106 | * defer_call_begin: Defer defer_call() functions until defer_call_end() |
41abca8c | 107 | * |
ccee48aa SH |
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. | |
41abca8c | 111 | * |
ccee48aa SH |
112 | * Nesting is supported. defer_call() functions are only called at the |
113 | * outermost defer_call_end(). | |
41abca8c | 114 | */ |
ccee48aa | 115 | void defer_call_begin(void) |
41abca8c | 116 | { |
ccee48aa | 117 | DeferCallThreadState *thread_state = get_ptr_defer_call_thread_state(); |
41abca8c | 118 | |
ccee48aa | 119 | assert(thread_state->nesting_level < UINT32_MAX); |
41abca8c | 120 | |
ccee48aa | 121 | thread_state->nesting_level++; |
41abca8c SH |
122 | } |
123 | ||
124 | /** | |
ccee48aa | 125 | * defer_call_end: Run any pending defer_call() functions |
41abca8c | 126 | * |
ccee48aa SH |
127 | * There must have been a matching defer_call_begin() call in the same thread |
128 | * prior to this defer_call_end() call. | |
41abca8c | 129 | */ |
ccee48aa | 130 | void defer_call_end(void) |
41abca8c | 131 | { |
ccee48aa | 132 | DeferCallThreadState *thread_state = get_ptr_defer_call_thread_state(); |
41abca8c | 133 | |
ccee48aa | 134 | assert(thread_state->nesting_level > 0); |
41abca8c | 135 | |
ccee48aa | 136 | if (--thread_state->nesting_level > 0) { |
41abca8c SH |
137 | return; |
138 | } | |
139 | ||
ccee48aa | 140 | GArray *array = thread_state->deferred_call_array; |
41abca8c SH |
141 | if (!array) { |
142 | return; | |
143 | } | |
144 | ||
ccee48aa | 145 | DeferredCall *fns = (DeferredCall *)array->data; |
41abca8c SH |
146 | |
147 | for (guint i = 0; i < array->len; i++) { | |
148 | fns[i].fn(fns[i].opaque); | |
149 | } | |
150 | ||
151 | /* | |
152 | * This resets the array without freeing memory so that appending is cheap | |
153 | * in the future. | |
154 | */ | |
155 | g_array_set_size(array, 0); | |
156 | } |