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
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) */
43 QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
46 bh = g_malloc0(sizeof(QEMUBH));
50 bh->next = ctx->first_bh;
55 int aio_bh_poll(AioContext *ctx)
57 QEMUBH *bh, **bhp, *next;
63 for (bh = ctx->first_bh; bh; bh = next) {
65 if (!bh->deleted && bh->scheduled) {
76 /* remove deleted bhs */
77 if (!ctx->walking_bh) {
93 void qemu_bh_schedule_idle(QEMUBH *bh)
101 void qemu_bh_schedule(QEMUBH *bh)
110 void qemu_bh_cancel(QEMUBH *bh)
115 void qemu_bh_delete(QEMUBH *bh)
122 aio_ctx_prepare(GSource *source, gint *timeout)
124 AioContext *ctx = (AioContext *) source;
127 for (bh = ctx->first_bh; bh; bh = bh->next) {
128 if (!bh->deleted && bh->scheduled) {
130 /* idle bottom halves will be polled at least
134 /* non-idle bottom halves will be executed
146 aio_ctx_check(GSource *source)
148 AioContext *ctx = (AioContext *) source;
151 for (bh = ctx->first_bh; bh; bh = bh->next) {
152 if (!bh->deleted && bh->scheduled) {
156 return aio_pending(ctx);
160 aio_ctx_dispatch(GSource *source,
161 GSourceFunc callback,
164 AioContext *ctx = (AioContext *) source;
166 assert(callback == NULL);
167 aio_poll(ctx, false);
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 = {
189 GSource *aio_get_g_source(AioContext *ctx)
191 g_source_ref(&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)
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);
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);