]>
Commit | Line | Data |
---|---|---|
4f999d05 KW |
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" | |
9a1e9481 | 26 | #include "qemu-aio.h" |
44a9b356 | 27 | #include "main-loop.h" |
9a1e9481 | 28 | |
4f999d05 KW |
29 | /***********************************************************/ |
30 | /* bottom halves (can be seen as timers which expire ASAP) */ | |
31 | ||
32 | struct QEMUBH { | |
2f4dc3c1 | 33 | AioContext *ctx; |
4f999d05 KW |
34 | QEMUBHFunc *cb; |
35 | void *opaque; | |
4f999d05 | 36 | QEMUBH *next; |
9b47b17e SW |
37 | bool scheduled; |
38 | bool idle; | |
39 | bool deleted; | |
4f999d05 KW |
40 | }; |
41 | ||
f627aab1 | 42 | QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque) |
4f999d05 KW |
43 | { |
44 | QEMUBH *bh; | |
7267c094 | 45 | bh = g_malloc0(sizeof(QEMUBH)); |
2f4dc3c1 | 46 | bh->ctx = ctx; |
4f999d05 KW |
47 | bh->cb = cb; |
48 | bh->opaque = opaque; | |
f627aab1 PB |
49 | bh->next = ctx->first_bh; |
50 | ctx->first_bh = bh; | |
4f999d05 KW |
51 | return bh; |
52 | } | |
53 | ||
f627aab1 | 54 | int aio_bh_poll(AioContext *ctx) |
4f999d05 | 55 | { |
7887f620 | 56 | QEMUBH *bh, **bhp, *next; |
4f999d05 | 57 | int ret; |
648fb0ea | 58 | |
f627aab1 | 59 | ctx->walking_bh++; |
4f999d05 KW |
60 | |
61 | ret = 0; | |
f627aab1 | 62 | for (bh = ctx->first_bh; bh; bh = next) { |
7887f620 | 63 | next = bh->next; |
4f999d05 KW |
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 | ||
f627aab1 | 73 | ctx->walking_bh--; |
648fb0ea | 74 | |
4f999d05 | 75 | /* remove deleted bhs */ |
f627aab1 PB |
76 | if (!ctx->walking_bh) { |
77 | bhp = &ctx->first_bh; | |
648fb0ea KW |
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 | } | |
4f999d05 KW |
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; | |
2f4dc3c1 | 106 | aio_notify(bh->ctx); |
4f999d05 KW |
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 | ||
f627aab1 | 120 | void aio_bh_update_timeout(AioContext *ctx, uint32_t *timeout) |
4f999d05 KW |
121 | { |
122 | QEMUBH *bh; | |
123 | ||
f627aab1 | 124 | for (bh = ctx->first_bh; bh; bh = bh->next) { |
4f999d05 KW |
125 | 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 | ||
e3713e00 PB |
140 | static gboolean |
141 | aio_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 | ||
155 | static gboolean | |
156 | aio_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 | ||
169 | static gboolean | |
170 | aio_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 | ||
2f4dc3c1 PB |
181 | static void |
182 | aio_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 | ||
e3713e00 PB |
190 | static GSourceFuncs aio_source_funcs = { |
191 | aio_ctx_prepare, | |
192 | aio_ctx_check, | |
193 | aio_ctx_dispatch, | |
2f4dc3c1 | 194 | aio_ctx_finalize |
e3713e00 PB |
195 | }; |
196 | ||
197 | GSource *aio_get_g_source(AioContext *ctx) | |
198 | { | |
199 | g_source_ref(&ctx->source); | |
200 | return &ctx->source; | |
201 | } | |
a915f4bc | 202 | |
2f4dc3c1 PB |
203 | void aio_notify(AioContext *ctx) |
204 | { | |
205 | event_notifier_set(&ctx->notifier); | |
206 | } | |
207 | ||
f627aab1 PB |
208 | AioContext *aio_context_new(void) |
209 | { | |
2f4dc3c1 PB |
210 | 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; | |
e3713e00 PB |
218 | } |
219 | ||
220 | void aio_context_ref(AioContext *ctx) | |
221 | { | |
222 | g_source_ref(&ctx->source); | |
223 | } | |
224 | ||
225 | void aio_context_unref(AioContext *ctx) | |
226 | { | |
227 | g_source_unref(&ctx->source); | |
f627aab1 | 228 | } |
a915f4bc PB |
229 | |
230 | void aio_flush(AioContext *ctx) | |
231 | { | |
7c0628b2 | 232 | while (aio_poll(ctx, true)); |
a915f4bc | 233 | } |