]> git.proxmox.com Git - qemu.git/blob - async.c
Merge branch 'trivial-patches' of git://github.com/stefanha/qemu
[qemu.git] / async.c
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
25 #include "qemu-common.h"
26 #include "qemu-aio.h"
27 #include "main-loop.h"
28
29 /***********************************************************/
30 /* bottom halves (can be seen as timers which expire ASAP) */
31
32 struct QEMUBH {
33 AioContext *ctx;
34 QEMUBHFunc *cb;
35 void *opaque;
36 QEMUBH *next;
37 bool scheduled;
38 bool idle;
39 bool deleted;
40 };
41
42 QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
43 {
44 QEMUBH *bh;
45 bh = g_malloc0(sizeof(QEMUBH));
46 bh->ctx = ctx;
47 bh->cb = cb;
48 bh->opaque = opaque;
49 bh->next = ctx->first_bh;
50 ctx->first_bh = bh;
51 return bh;
52 }
53
54 int aio_bh_poll(AioContext *ctx)
55 {
56 QEMUBH *bh, **bhp, *next;
57 int ret;
58
59 ctx->walking_bh++;
60
61 ret = 0;
62 for (bh = ctx->first_bh; bh; bh = next) {
63 next = bh->next;
64 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
73 ctx->walking_bh--;
74
75 /* remove deleted bhs */
76 if (!ctx->walking_bh) {
77 bhp = &ctx->first_bh;
78 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 }
87 }
88
89 return ret;
90 }
91
92 void qemu_bh_schedule_idle(QEMUBH *bh)
93 {
94 if (bh->scheduled)
95 return;
96 bh->scheduled = 1;
97 bh->idle = 1;
98 }
99
100 void qemu_bh_schedule(QEMUBH *bh)
101 {
102 if (bh->scheduled)
103 return;
104 bh->scheduled = 1;
105 bh->idle = 0;
106 aio_notify(bh->ctx);
107 }
108
109 void qemu_bh_cancel(QEMUBH *bh)
110 {
111 bh->scheduled = 0;
112 }
113
114 void qemu_bh_delete(QEMUBH *bh)
115 {
116 bh->scheduled = 0;
117 bh->deleted = 1;
118 }
119
120 static gboolean
121 aio_ctx_prepare(GSource *source, gint *timeout)
122 {
123 AioContext *ctx = (AioContext *) source;
124 QEMUBH *bh;
125 bool scheduled = false;
126
127 for (bh = ctx->first_bh; bh; bh = bh->next) {
128 if (!bh->deleted && bh->scheduled) {
129 scheduled = true;
130 if (bh->idle) {
131 /* idle bottom halves will be polled at least
132 * every 10ms */
133 *timeout = 10;
134 } else {
135 /* non-idle bottom halves will be executed
136 * immediately */
137 *timeout = 0;
138 break;
139 }
140 }
141 }
142
143 return scheduled;
144 }
145
146 static gboolean
147 aio_ctx_check(GSource *source)
148 {
149 AioContext *ctx = (AioContext *) source;
150 QEMUBH *bh;
151
152 for (bh = ctx->first_bh; bh; bh = bh->next) {
153 if (!bh->deleted && bh->scheduled) {
154 return true;
155 }
156 }
157 return aio_pending(ctx);
158 }
159
160 static gboolean
161 aio_ctx_dispatch(GSource *source,
162 GSourceFunc callback,
163 gpointer user_data)
164 {
165 AioContext *ctx = (AioContext *) source;
166
167 assert(callback == NULL);
168 aio_poll(ctx, false);
169 return true;
170 }
171
172 static void
173 aio_ctx_finalize(GSource *source)
174 {
175 AioContext *ctx = (AioContext *) source;
176
177 aio_set_event_notifier(ctx, &ctx->notifier, NULL, NULL);
178 event_notifier_cleanup(&ctx->notifier);
179 }
180
181 static GSourceFuncs aio_source_funcs = {
182 aio_ctx_prepare,
183 aio_ctx_check,
184 aio_ctx_dispatch,
185 aio_ctx_finalize
186 };
187
188 GSource *aio_get_g_source(AioContext *ctx)
189 {
190 g_source_ref(&ctx->source);
191 return &ctx->source;
192 }
193
194 void aio_notify(AioContext *ctx)
195 {
196 event_notifier_set(&ctx->notifier);
197 }
198
199 AioContext *aio_context_new(void)
200 {
201 AioContext *ctx;
202 ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
203 event_notifier_init(&ctx->notifier, false);
204 aio_set_event_notifier(ctx, &ctx->notifier,
205 (EventNotifierHandler *)
206 event_notifier_test_and_clear, NULL);
207
208 return ctx;
209 }
210
211 void aio_context_ref(AioContext *ctx)
212 {
213 g_source_ref(&ctx->source);
214 }
215
216 void aio_context_unref(AioContext *ctx)
217 {
218 g_source_unref(&ctx->source);
219 }
220
221 void aio_flush(AioContext *ctx)
222 {
223 while (aio_poll(ctx, true));
224 }