block: complete all IOs before resizing a device
[qemu-kvm.git] / async.c
blob90fe906539f70815a5cfbaa95b42fab9f11ce228
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu-common.h"
26 #include "block/aio.h"
27 #include "block/thread-pool.h"
28 #include "qemu/main-loop.h"
30 /***********************************************************/
31 /* bottom halves (can be seen as timers which expire ASAP) */
33 struct QEMUBH {
34 AioContext *ctx;
35 QEMUBHFunc *cb;
36 void *opaque;
37 QEMUBH *next;
38 bool scheduled;
39 bool idle;
40 bool deleted;
43 QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
45 QEMUBH *bh;
46 bh = g_malloc0(sizeof(QEMUBH));
47 bh->ctx = ctx;
48 bh->cb = cb;
49 bh->opaque = opaque;
50 bh->next = ctx->first_bh;
51 ctx->first_bh = bh;
52 return bh;
55 int aio_bh_poll(AioContext *ctx)
57 QEMUBH *bh, **bhp, *next;
58 int ret;
60 ctx->walking_bh++;
62 ret = 0;
63 for (bh = ctx->first_bh; bh; bh = next) {
64 next = bh->next;
65 if (!bh->deleted && bh->scheduled) {
66 bh->scheduled = 0;
67 if (!bh->idle)
68 ret = 1;
69 bh->idle = 0;
70 bh->cb(bh->opaque);
74 ctx->walking_bh--;
76 /* remove deleted bhs */
77 if (!ctx->walking_bh) {
78 bhp = &ctx->first_bh;
79 while (*bhp) {
80 bh = *bhp;
81 if (bh->deleted) {
82 *bhp = bh->next;
83 g_free(bh);
84 } else {
85 bhp = &bh->next;
90 return ret;
93 void qemu_bh_schedule_idle(QEMUBH *bh)
95 if (bh->scheduled)
96 return;
97 bh->scheduled = 1;
98 bh->idle = 1;
101 void qemu_bh_schedule(QEMUBH *bh)
103 if (bh->scheduled)
104 return;
105 bh->scheduled = 1;
106 bh->idle = 0;
107 aio_notify(bh->ctx);
110 void qemu_bh_cancel(QEMUBH *bh)
112 bh->scheduled = 0;
115 void qemu_bh_delete(QEMUBH *bh)
117 bh->scheduled = 0;
118 bh->deleted = 1;
121 static gboolean
122 aio_ctx_prepare(GSource *source, gint *timeout)
124 AioContext *ctx = (AioContext *) source;
125 QEMUBH *bh;
127 for (bh = ctx->first_bh; bh; bh = bh->next) {
128 if (!bh->deleted && bh->scheduled) {
129 if (bh->idle) {
130 /* idle bottom halves will be polled at least
131 * every 10ms */
132 *timeout = 10;
133 } else {
134 /* non-idle bottom halves will be executed
135 * immediately */
136 *timeout = 0;
137 return true;
142 return false;
145 static gboolean
146 aio_ctx_check(GSource *source)
148 AioContext *ctx = (AioContext *) source;
149 QEMUBH *bh;
151 for (bh = ctx->first_bh; bh; bh = bh->next) {
152 if (!bh->deleted && bh->scheduled) {
153 return true;
156 return aio_pending(ctx);
159 static gboolean
160 aio_ctx_dispatch(GSource *source,
161 GSourceFunc callback,
162 gpointer user_data)
164 AioContext *ctx = (AioContext *) source;
166 assert(callback == NULL);
167 aio_poll(ctx, false);
168 return true;
171 static void
172 aio_ctx_finalize(GSource *source)
174 AioContext *ctx = (AioContext *) source;
176 thread_pool_free(ctx->thread_pool);
177 aio_set_event_notifier(ctx, &ctx->notifier, NULL, NULL);
178 event_notifier_cleanup(&ctx->notifier);
179 g_array_free(ctx->pollfds, TRUE);
182 static GSourceFuncs aio_source_funcs = {
183 aio_ctx_prepare,
184 aio_ctx_check,
185 aio_ctx_dispatch,
186 aio_ctx_finalize
189 GSource *aio_get_g_source(AioContext *ctx)
191 g_source_ref(&ctx->source);
192 return &ctx->source;
195 ThreadPool *aio_get_thread_pool(AioContext *ctx)
197 if (!ctx->thread_pool) {
198 ctx->thread_pool = thread_pool_new(ctx);
200 return ctx->thread_pool;
203 void aio_notify(AioContext *ctx)
205 event_notifier_set(&ctx->notifier);
208 AioContext *aio_context_new(void)
210 AioContext *ctx;
211 ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
212 ctx->pollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
213 ctx->thread_pool = NULL;
214 event_notifier_init(&ctx->notifier, false);
215 aio_set_event_notifier(ctx, &ctx->notifier,
216 (EventNotifierHandler *)
217 event_notifier_test_and_clear, NULL);
219 return ctx;
222 void aio_context_ref(AioContext *ctx)
224 g_source_ref(&ctx->source);
227 void aio_context_unref(AioContext *ctx)
229 g_source_unref(&ctx->source);