blob: 564526f57fb1d25d829f8688713851e64a43143d [file] [log] [blame]
Kevin Wolf4f999d02009-10-22 17:54:37 +02001/*
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
25#include "qemu-common.h"
Kevin Wolf9a1e9482009-10-22 17:54:38 +020026#include "qemu-aio.h"
Paolo Bonzini44a9b352011-09-12 16:44:30 +020027#include "main-loop.h"
Kevin Wolf9a1e9482009-10-22 17:54:38 +020028
Kevin Wolf4f999d02009-10-22 17:54:37 +020029/***********************************************************/
30/* bottom halves (can be seen as timers which expire ASAP) */
31
32struct QEMUBH {
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +020033 AioContext *ctx;
Kevin Wolf4f999d02009-10-22 17:54:37 +020034 QEMUBHFunc *cb;
35 void *opaque;
Kevin Wolf4f999d02009-10-22 17:54:37 +020036 QEMUBH *next;
Stefan Weil9b47b172012-04-29 19:08:45 +020037 bool scheduled;
38 bool idle;
39 bool deleted;
Kevin Wolf4f999d02009-10-22 17:54:37 +020040};
41
Paolo Bonzinif627aab2012-10-29 23:45:23 +010042QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
Kevin Wolf4f999d02009-10-22 17:54:37 +020043{
44 QEMUBH *bh;
Anthony Liguori7267c092011-08-20 22:09:37 -050045 bh = g_malloc0(sizeof(QEMUBH));
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +020046 bh->ctx = ctx;
Kevin Wolf4f999d02009-10-22 17:54:37 +020047 bh->cb = cb;
48 bh->opaque = opaque;
Paolo Bonzinif627aab2012-10-29 23:45:23 +010049 bh->next = ctx->first_bh;
50 ctx->first_bh = bh;
Kevin Wolf4f999d02009-10-22 17:54:37 +020051 return bh;
52}
53
Paolo Bonzinif627aab2012-10-29 23:45:23 +010054int aio_bh_poll(AioContext *ctx)
Kevin Wolf4f999d02009-10-22 17:54:37 +020055{
Kevin Wolf7887f622011-06-07 17:51:21 +020056 QEMUBH *bh, **bhp, *next;
Kevin Wolf4f999d02009-10-22 17:54:37 +020057 int ret;
Kevin Wolf648fb0e2011-09-01 16:16:10 +020058
Paolo Bonzinif627aab2012-10-29 23:45:23 +010059 ctx->walking_bh++;
Kevin Wolf4f999d02009-10-22 17:54:37 +020060
61 ret = 0;
Paolo Bonzinif627aab2012-10-29 23:45:23 +010062 for (bh = ctx->first_bh; bh; bh = next) {
Kevin Wolf7887f622011-06-07 17:51:21 +020063 next = bh->next;
Kevin Wolf4f999d02009-10-22 17:54:37 +020064 if (!bh->deleted && bh->scheduled) {
65 bh->scheduled = 0;
66 if (!bh->idle)
67 ret = 1;
68 bh->idle = 0;
69 bh->cb(bh->opaque);
70 }
71 }
72
Paolo Bonzinif627aab2012-10-29 23:45:23 +010073 ctx->walking_bh--;
Kevin Wolf648fb0e2011-09-01 16:16:10 +020074
Kevin Wolf4f999d02009-10-22 17:54:37 +020075 /* remove deleted bhs */
Paolo Bonzinif627aab2012-10-29 23:45:23 +010076 if (!ctx->walking_bh) {
77 bhp = &ctx->first_bh;
Kevin Wolf648fb0e2011-09-01 16:16:10 +020078 while (*bhp) {
79 bh = *bhp;
80 if (bh->deleted) {
81 *bhp = bh->next;
82 g_free(bh);
83 } else {
84 bhp = &bh->next;
85 }
86 }
Kevin Wolf4f999d02009-10-22 17:54:37 +020087 }
88
89 return ret;
90}
91
92void qemu_bh_schedule_idle(QEMUBH *bh)
93{
94 if (bh->scheduled)
95 return;
96 bh->scheduled = 1;
97 bh->idle = 1;
98}
99
100void qemu_bh_schedule(QEMUBH *bh)
101{
102 if (bh->scheduled)
103 return;
104 bh->scheduled = 1;
105 bh->idle = 0;
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200106 aio_notify(bh->ctx);
Kevin Wolf4f999d02009-10-22 17:54:37 +0200107}
108
109void qemu_bh_cancel(QEMUBH *bh)
110{
111 bh->scheduled = 0;
112}
113
114void qemu_bh_delete(QEMUBH *bh)
115{
116 bh->scheduled = 0;
117 bh->deleted = 1;
118}
119
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100120void aio_bh_update_timeout(AioContext *ctx, uint32_t *timeout)
Kevin Wolf4f999d02009-10-22 17:54:37 +0200121{
122 QEMUBH *bh;
123
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100124 for (bh = ctx->first_bh; bh; bh = bh->next) {
Kevin Wolf4f999d02009-10-22 17:54:37 +0200125 if (!bh->deleted && bh->scheduled) {
126 if (bh->idle) {
127 /* idle bottom halves will be polled at least
128 * every 10ms */
129 *timeout = MIN(10, *timeout);
130 } else {
131 /* non-idle bottom halves will be executed
132 * immediately */
133 *timeout = 0;
134 break;
135 }
136 }
137 }
138}
139
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200140static gboolean
141aio_ctx_prepare(GSource *source, gint *timeout)
142{
143 AioContext *ctx = (AioContext *) source;
144 uint32_t wait = -1;
145 aio_bh_update_timeout(ctx, &wait);
146
147 if (wait != -1) {
148 *timeout = MIN(*timeout, wait);
149 return wait == 0;
150 }
151
152 return false;
153}
154
155static gboolean
156aio_ctx_check(GSource *source)
157{
158 AioContext *ctx = (AioContext *) source;
159 QEMUBH *bh;
160
161 for (bh = ctx->first_bh; bh; bh = bh->next) {
162 if (!bh->deleted && bh->scheduled) {
163 return true;
164 }
165 }
166 return aio_pending(ctx);
167}
168
169static gboolean
170aio_ctx_dispatch(GSource *source,
171 GSourceFunc callback,
172 gpointer user_data)
173{
174 AioContext *ctx = (AioContext *) source;
175
176 assert(callback == NULL);
177 aio_poll(ctx, false);
178 return true;
179}
180
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200181static void
182aio_ctx_finalize(GSource *source)
183{
184 AioContext *ctx = (AioContext *) source;
185
186 aio_set_event_notifier(ctx, &ctx->notifier, NULL, NULL);
187 event_notifier_cleanup(&ctx->notifier);
188}
189
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200190static GSourceFuncs aio_source_funcs = {
191 aio_ctx_prepare,
192 aio_ctx_check,
193 aio_ctx_dispatch,
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200194 aio_ctx_finalize
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200195};
196
197GSource *aio_get_g_source(AioContext *ctx)
198{
199 g_source_ref(&ctx->source);
200 return &ctx->source;
201}
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200202
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200203void aio_notify(AioContext *ctx)
204{
205 event_notifier_set(&ctx->notifier);
206}
207
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100208AioContext *aio_context_new(void)
209{
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200210 AioContext *ctx;
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);
216
217 return ctx;
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200218}
219
220void aio_context_ref(AioContext *ctx)
221{
222 g_source_ref(&ctx->source);
223}
224
225void aio_context_unref(AioContext *ctx)
226{
227 g_source_unref(&ctx->source);
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100228}
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200229
230void aio_flush(AioContext *ctx)
231{
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200232 while (aio_poll(ctx, true));
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200233}