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"
27 #include "main-loop.h"
29 /***********************************************************/
30 /* bottom halves (can be seen as timers which expire ASAP) */
42 QEMUBH
*aio_bh_new(AioContext
*ctx
, QEMUBHFunc
*cb
, void *opaque
)
45 bh
= g_malloc0(sizeof(QEMUBH
));
49 bh
->next
= ctx
->first_bh
;
54 int aio_bh_poll(AioContext
*ctx
)
56 QEMUBH
*bh
, **bhp
, *next
;
62 for (bh
= ctx
->first_bh
; bh
; bh
= next
) {
64 if (!bh
->deleted
&& bh
->scheduled
) {
75 /* remove deleted bhs */
76 if (!ctx
->walking_bh
) {
92 void qemu_bh_schedule_idle(QEMUBH
*bh
)
100 void qemu_bh_schedule(QEMUBH
*bh
)
109 void qemu_bh_cancel(QEMUBH
*bh
)
114 void qemu_bh_delete(QEMUBH
*bh
)
120 void aio_bh_update_timeout(AioContext
*ctx
, uint32_t *timeout
)
124 for (bh
= ctx
->first_bh
; bh
; bh
= bh
->next
) {
125 if (!bh
->deleted
&& bh
->scheduled
) {
127 /* idle bottom halves will be polled at least
129 *timeout
= MIN(10, *timeout
);
131 /* non-idle bottom halves will be executed
141 aio_ctx_prepare(GSource
*source
, gint
*timeout
)
143 AioContext
*ctx
= (AioContext
*) source
;
145 aio_bh_update_timeout(ctx
, &wait
);
148 *timeout
= MIN(*timeout
, wait
);
156 aio_ctx_check(GSource
*source
)
158 AioContext
*ctx
= (AioContext
*) source
;
161 for (bh
= ctx
->first_bh
; bh
; bh
= bh
->next
) {
162 if (!bh
->deleted
&& bh
->scheduled
) {
166 return aio_pending(ctx
);
170 aio_ctx_dispatch(GSource
*source
,
171 GSourceFunc callback
,
174 AioContext
*ctx
= (AioContext
*) source
;
176 assert(callback
== NULL
);
177 aio_poll(ctx
, false);
182 aio_ctx_finalize(GSource
*source
)
184 AioContext
*ctx
= (AioContext
*) source
;
186 aio_set_event_notifier(ctx
, &ctx
->notifier
, NULL
, NULL
);
187 event_notifier_cleanup(&ctx
->notifier
);
190 static GSourceFuncs aio_source_funcs
= {
197 GSource
*aio_get_g_source(AioContext
*ctx
)
199 g_source_ref(&ctx
->source
);
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 event_notifier_init(&ctx
->notifier
, false);
213 aio_set_event_notifier(ctx
, &ctx
->notifier
,
214 (EventNotifierHandler
*)
215 event_notifier_test_and_clear
, NULL
);
220 void aio_context_ref(AioContext
*ctx
)
222 g_source_ref(&ctx
->source
);
225 void aio_context_unref(AioContext
*ctx
)
227 g_source_unref(&ctx
->source
);
230 void aio_flush(AioContext
*ctx
)
232 while (aio_poll(ctx
, true));