]> git.proxmox.com Git - mirror_qemu.git/blame - async.c
all: Clean up includes
[mirror_qemu.git] / async.c
CommitLineData
4f999d05
KW
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
d38ea87a 25#include "qemu/osdep.h"
4f999d05 26#include "qemu-common.h"
737e150e 27#include "block/aio.h"
9b34277d 28#include "block/thread-pool.h"
1de7afc9 29#include "qemu/main-loop.h"
0ceb849b 30#include "qemu/atomic.h"
9a1e9481 31
4f999d05
KW
32/***********************************************************/
33/* bottom halves (can be seen as timers which expire ASAP) */
34
35struct QEMUBH {
2f4dc3c1 36 AioContext *ctx;
4f999d05
KW
37 QEMUBHFunc *cb;
38 void *opaque;
4f999d05 39 QEMUBH *next;
9b47b17e
SW
40 bool scheduled;
41 bool idle;
42 bool deleted;
4f999d05
KW
43};
44
f627aab1 45QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
4f999d05
KW
46{
47 QEMUBH *bh;
ee82310f
PB
48 bh = g_new(QEMUBH, 1);
49 *bh = (QEMUBH){
50 .ctx = ctx,
51 .cb = cb,
52 .opaque = opaque,
53 };
dcc772e2 54 qemu_mutex_lock(&ctx->bh_lock);
f627aab1 55 bh->next = ctx->first_bh;
dcc772e2
LPF
56 /* Make sure that the members are ready before putting bh into list */
57 smp_wmb();
f627aab1 58 ctx->first_bh = bh;
dcc772e2 59 qemu_mutex_unlock(&ctx->bh_lock);
4f999d05
KW
60 return bh;
61}
62
df281b80
PD
63void aio_bh_call(QEMUBH *bh)
64{
65 bh->cb(bh->opaque);
66}
67
dcc772e2 68/* Multiple occurrences of aio_bh_poll cannot be called concurrently */
f627aab1 69int aio_bh_poll(AioContext *ctx)
4f999d05 70{
7887f620 71 QEMUBH *bh, **bhp, *next;
4f999d05 72 int ret;
648fb0ea 73
f627aab1 74 ctx->walking_bh++;
4f999d05
KW
75
76 ret = 0;
f627aab1 77 for (bh = ctx->first_bh; bh; bh = next) {
dcc772e2
LPF
78 /* Make sure that fetching bh happens before accessing its members */
79 smp_read_barrier_depends();
7887f620 80 next = bh->next;
e8d3b1a2
PB
81 /* The atomic_xchg is paired with the one in qemu_bh_schedule. The
82 * implicit memory barrier ensures that the callback sees all writes
83 * done by the scheduling thread. It also ensures that the scheduling
84 * thread sees the zero before bh->cb has run, and thus will call
85 * aio_notify again if necessary.
86 */
87 if (!bh->deleted && atomic_xchg(&bh->scheduled, 0)) {
ca96ac44
SH
88 /* Idle BHs and the notify BH don't count as progress */
89 if (!bh->idle && bh != ctx->notify_dummy_bh) {
4f999d05 90 ret = 1;
ca96ac44 91 }
4f999d05 92 bh->idle = 0;
df281b80 93 aio_bh_call(bh);
4f999d05
KW
94 }
95 }
96
f627aab1 97 ctx->walking_bh--;
648fb0ea 98
4f999d05 99 /* remove deleted bhs */
f627aab1 100 if (!ctx->walking_bh) {
dcc772e2 101 qemu_mutex_lock(&ctx->bh_lock);
f627aab1 102 bhp = &ctx->first_bh;
648fb0ea
KW
103 while (*bhp) {
104 bh = *bhp;
105 if (bh->deleted) {
106 *bhp = bh->next;
107 g_free(bh);
108 } else {
109 bhp = &bh->next;
110 }
111 }
dcc772e2 112 qemu_mutex_unlock(&ctx->bh_lock);
4f999d05
KW
113 }
114
115 return ret;
116}
117
118void qemu_bh_schedule_idle(QEMUBH *bh)
119{
4f999d05 120 bh->idle = 1;
dcc772e2
LPF
121 /* Make sure that idle & any writes needed by the callback are done
122 * before the locations are read in the aio_bh_poll.
123 */
e8d3b1a2 124 atomic_mb_set(&bh->scheduled, 1);
4f999d05
KW
125}
126
127void qemu_bh_schedule(QEMUBH *bh)
128{
924fe129
SH
129 AioContext *ctx;
130
924fe129 131 ctx = bh->ctx;
4f999d05 132 bh->idle = 0;
e8d3b1a2 133 /* The memory barrier implicit in atomic_xchg makes sure that:
924fe129
SH
134 * 1. idle & any writes needed by the callback are done before the
135 * locations are read in the aio_bh_poll.
136 * 2. ctx is loaded before scheduled is set and the callback has a chance
137 * to execute.
dcc772e2 138 */
e8d3b1a2
PB
139 if (atomic_xchg(&bh->scheduled, 1) == 0) {
140 aio_notify(ctx);
141 }
4f999d05
KW
142}
143
dcc772e2
LPF
144
145/* This func is async.
146 */
4f999d05
KW
147void qemu_bh_cancel(QEMUBH *bh)
148{
149 bh->scheduled = 0;
150}
151
dcc772e2
LPF
152/* This func is async.The bottom half will do the delete action at the finial
153 * end.
154 */
4f999d05
KW
155void qemu_bh_delete(QEMUBH *bh)
156{
157 bh->scheduled = 0;
158 bh->deleted = 1;
159}
160
845ca10d
PB
161int64_t
162aio_compute_timeout(AioContext *ctx)
4f999d05 163{
845ca10d
PB
164 int64_t deadline;
165 int timeout = -1;
4f999d05
KW
166 QEMUBH *bh;
167
f627aab1 168 for (bh = ctx->first_bh; bh; bh = bh->next) {
4f999d05
KW
169 if (!bh->deleted && bh->scheduled) {
170 if (bh->idle) {
171 /* idle bottom halves will be polled at least
172 * every 10ms */
845ca10d 173 timeout = 10000000;
4f999d05
KW
174 } else {
175 /* non-idle bottom halves will be executed
176 * immediately */
845ca10d 177 return 0;
4f999d05
KW
178 }
179 }
180 }
e3713e00 181
845ca10d 182 deadline = timerlistgroup_deadline_ns(&ctx->tlg);
533a8cf3 183 if (deadline == 0) {
845ca10d 184 return 0;
533a8cf3 185 } else {
845ca10d 186 return qemu_soonest_timeout(timeout, deadline);
533a8cf3 187 }
845ca10d 188}
533a8cf3 189
845ca10d
PB
190static gboolean
191aio_ctx_prepare(GSource *source, gint *timeout)
192{
193 AioContext *ctx = (AioContext *) source;
194
eabc9779
PB
195 atomic_or(&ctx->notify_me, 1);
196
845ca10d
PB
197 /* We assume there is no timeout already supplied */
198 *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
a3462c65
PB
199
200 if (aio_prepare(ctx)) {
201 *timeout = 0;
202 }
203
845ca10d 204 return *timeout == 0;
e3713e00
PB
205}
206
207static gboolean
208aio_ctx_check(GSource *source)
209{
210 AioContext *ctx = (AioContext *) source;
211 QEMUBH *bh;
212
eabc9779 213 atomic_and(&ctx->notify_me, ~1);
05e514b1 214 aio_notify_accept(ctx);
21a03d17 215
e3713e00
PB
216 for (bh = ctx->first_bh; bh; bh = bh->next) {
217 if (!bh->deleted && bh->scheduled) {
218 return true;
219 }
220 }
533a8cf3 221 return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
e3713e00
PB
222}
223
224static gboolean
225aio_ctx_dispatch(GSource *source,
226 GSourceFunc callback,
227 gpointer user_data)
228{
229 AioContext *ctx = (AioContext *) source;
230
231 assert(callback == NULL);
e4c7e2d1 232 aio_dispatch(ctx);
e3713e00
PB
233 return true;
234}
235
2f4dc3c1
PB
236static void
237aio_ctx_finalize(GSource *source)
238{
239 AioContext *ctx = (AioContext *) source;
240
ca96ac44 241 qemu_bh_delete(ctx->notify_dummy_bh);
9b34277d 242 thread_pool_free(ctx->thread_pool);
a076972a
SH
243
244 qemu_mutex_lock(&ctx->bh_lock);
245 while (ctx->first_bh) {
246 QEMUBH *next = ctx->first_bh->next;
247
248 /* qemu_bh_delete() must have been called on BHs in this AioContext */
249 assert(ctx->first_bh->deleted);
250
251 g_free(ctx->first_bh);
252 ctx->first_bh = next;
253 }
254 qemu_mutex_unlock(&ctx->bh_lock);
255
dca21ef2 256 aio_set_event_notifier(ctx, &ctx->notifier, false, NULL);
2f4dc3c1 257 event_notifier_cleanup(&ctx->notifier);
98563fc3 258 rfifolock_destroy(&ctx->lock);
dcc772e2 259 qemu_mutex_destroy(&ctx->bh_lock);
dae21b98 260 timerlistgroup_deinit(&ctx->tlg);
2f4dc3c1
PB
261}
262
e3713e00
PB
263static GSourceFuncs aio_source_funcs = {
264 aio_ctx_prepare,
265 aio_ctx_check,
266 aio_ctx_dispatch,
2f4dc3c1 267 aio_ctx_finalize
e3713e00
PB
268};
269
270GSource *aio_get_g_source(AioContext *ctx)
271{
272 g_source_ref(&ctx->source);
273 return &ctx->source;
274}
a915f4bc 275
9b34277d
SH
276ThreadPool *aio_get_thread_pool(AioContext *ctx)
277{
278 if (!ctx->thread_pool) {
279 ctx->thread_pool = thread_pool_new(ctx);
280 }
281 return ctx->thread_pool;
282}
283
2f4dc3c1
PB
284void aio_notify(AioContext *ctx)
285{
eabc9779
PB
286 /* Write e.g. bh->scheduled before reading ctx->notify_me. Pairs
287 * with atomic_or in aio_ctx_prepare or atomic_add in aio_poll.
288 */
0ceb849b 289 smp_mb();
eabc9779 290 if (ctx->notify_me) {
0ceb849b 291 event_notifier_set(&ctx->notifier);
05e514b1
PB
292 atomic_mb_set(&ctx->notified, true);
293 }
294}
295
296void aio_notify_accept(AioContext *ctx)
297{
298 if (atomic_xchg(&ctx->notified, false)) {
299 event_notifier_test_and_clear(&ctx->notifier);
0ceb849b 300 }
2f4dc3c1
PB
301}
302
d5541d86
AB
303static void aio_timerlist_notify(void *opaque)
304{
305 aio_notify(opaque);
306}
307
da5e1de9
SH
308static void aio_rfifolock_cb(void *opaque)
309{
ca96ac44
SH
310 AioContext *ctx = opaque;
311
da5e1de9 312 /* Kick owner thread in case they are blocked in aio_poll() */
ca96ac44
SH
313 qemu_bh_schedule(ctx->notify_dummy_bh);
314}
315
316static void notify_dummy_bh(void *opaque)
317{
318 /* Do nothing, we were invoked just to force the event loop to iterate */
da5e1de9
SH
319}
320
21a03d17
PB
321static void event_notifier_dummy_cb(EventNotifier *e)
322{
323}
324
2f78e491 325AioContext *aio_context_new(Error **errp)
f627aab1 326{
2f78e491 327 int ret;
2f4dc3c1 328 AioContext *ctx;
37fcee5d
FZ
329 Error *local_err = NULL;
330
2f4dc3c1 331 ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
37fcee5d
FZ
332 aio_context_setup(ctx, &local_err);
333 if (local_err) {
334 error_propagate(errp, local_err);
335 goto fail;
336 }
2f78e491
CN
337 ret = event_notifier_init(&ctx->notifier, false);
338 if (ret < 0) {
2f78e491 339 error_setg_errno(errp, -ret, "Failed to initialize event notifier");
37fcee5d 340 goto fail;
2f78e491 341 }
fcf5def1 342 g_source_set_can_recurse(&ctx->source, true);
2f78e491 343 aio_set_event_notifier(ctx, &ctx->notifier,
dca21ef2 344 false,
2f78e491 345 (EventNotifierHandler *)
21a03d17 346 event_notifier_dummy_cb);
9b34277d 347 ctx->thread_pool = NULL;
dcc772e2 348 qemu_mutex_init(&ctx->bh_lock);
da5e1de9 349 rfifolock_init(&ctx->lock, aio_rfifolock_cb, ctx);
d5541d86 350 timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
2f4dc3c1 351
ca96ac44
SH
352 ctx->notify_dummy_bh = aio_bh_new(ctx, notify_dummy_bh, NULL);
353
2f4dc3c1 354 return ctx;
37fcee5d
FZ
355fail:
356 g_source_destroy(&ctx->source);
357 return NULL;
e3713e00
PB
358}
359
360void aio_context_ref(AioContext *ctx)
361{
362 g_source_ref(&ctx->source);
363}
364
365void aio_context_unref(AioContext *ctx)
366{
367 g_source_unref(&ctx->source);
f627aab1 368}
98563fc3
SH
369
370void aio_context_acquire(AioContext *ctx)
371{
372 rfifolock_lock(&ctx->lock);
373}
374
375void aio_context_release(AioContext *ctx)
376{
377 rfifolock_unlock(&ctx->lock);
378}