]> git.proxmox.com Git - qemu.git/blame - async.c
q35: set fw_name
[qemu.git] / async.c
CommitLineData
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"
737e150e 26#include "block/aio.h"
9b34277d 27#include "block/thread-pool.h"
1de7afc9 28#include "qemu/main-loop.h"
9a1e9481 29
4f999d05
KW
30/***********************************************************/
31/* bottom halves (can be seen as timers which expire ASAP) */
32
33struct QEMUBH {
2f4dc3c1 34 AioContext *ctx;
4f999d05
KW
35 QEMUBHFunc *cb;
36 void *opaque;
4f999d05 37 QEMUBH *next;
9b47b17e
SW
38 bool scheduled;
39 bool idle;
40 bool deleted;
4f999d05
KW
41};
42
f627aab1 43QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
4f999d05
KW
44{
45 QEMUBH *bh;
7267c094 46 bh = g_malloc0(sizeof(QEMUBH));
2f4dc3c1 47 bh->ctx = ctx;
4f999d05
KW
48 bh->cb = cb;
49 bh->opaque = opaque;
f627aab1
PB
50 bh->next = ctx->first_bh;
51 ctx->first_bh = bh;
4f999d05
KW
52 return bh;
53}
54
f627aab1 55int aio_bh_poll(AioContext *ctx)
4f999d05 56{
7887f620 57 QEMUBH *bh, **bhp, *next;
4f999d05 58 int ret;
648fb0ea 59
f627aab1 60 ctx->walking_bh++;
4f999d05
KW
61
62 ret = 0;
f627aab1 63 for (bh = ctx->first_bh; bh; bh = next) {
7887f620 64 next = bh->next;
4f999d05
KW
65 if (!bh->deleted && bh->scheduled) {
66 bh->scheduled = 0;
67 if (!bh->idle)
68 ret = 1;
69 bh->idle = 0;
70 bh->cb(bh->opaque);
71 }
72 }
73
f627aab1 74 ctx->walking_bh--;
648fb0ea 75
4f999d05 76 /* remove deleted bhs */
f627aab1
PB
77 if (!ctx->walking_bh) {
78 bhp = &ctx->first_bh;
648fb0ea
KW
79 while (*bhp) {
80 bh = *bhp;
81 if (bh->deleted) {
82 *bhp = bh->next;
83 g_free(bh);
84 } else {
85 bhp = &bh->next;
86 }
87 }
4f999d05
KW
88 }
89
90 return ret;
91}
92
93void qemu_bh_schedule_idle(QEMUBH *bh)
94{
95 if (bh->scheduled)
96 return;
97 bh->scheduled = 1;
98 bh->idle = 1;
99}
100
101void qemu_bh_schedule(QEMUBH *bh)
102{
103 if (bh->scheduled)
104 return;
105 bh->scheduled = 1;
106 bh->idle = 0;
2f4dc3c1 107 aio_notify(bh->ctx);
4f999d05
KW
108}
109
110void qemu_bh_cancel(QEMUBH *bh)
111{
112 bh->scheduled = 0;
113}
114
115void qemu_bh_delete(QEMUBH *bh)
116{
117 bh->scheduled = 0;
118 bh->deleted = 1;
119}
120
22bfa75e
PB
121static gboolean
122aio_ctx_prepare(GSource *source, gint *timeout)
4f999d05 123{
22bfa75e 124 AioContext *ctx = (AioContext *) source;
4f999d05
KW
125 QEMUBH *bh;
126
f627aab1 127 for (bh = ctx->first_bh; bh; bh = bh->next) {
4f999d05
KW
128 if (!bh->deleted && bh->scheduled) {
129 if (bh->idle) {
130 /* idle bottom halves will be polled at least
131 * every 10ms */
22bfa75e 132 *timeout = 10;
4f999d05
KW
133 } else {
134 /* non-idle bottom halves will be executed
135 * immediately */
136 *timeout = 0;
f5022a13 137 return true;
4f999d05
KW
138 }
139 }
140 }
e3713e00 141
f5022a13 142 return false;
e3713e00
PB
143}
144
145static gboolean
146aio_ctx_check(GSource *source)
147{
148 AioContext *ctx = (AioContext *) source;
149 QEMUBH *bh;
150
151 for (bh = ctx->first_bh; bh; bh = bh->next) {
152 if (!bh->deleted && bh->scheduled) {
153 return true;
154 }
155 }
156 return aio_pending(ctx);
157}
158
159static gboolean
160aio_ctx_dispatch(GSource *source,
161 GSourceFunc callback,
162 gpointer user_data)
163{
164 AioContext *ctx = (AioContext *) source;
165
166 assert(callback == NULL);
167 aio_poll(ctx, false);
168 return true;
169}
170
2f4dc3c1
PB
171static void
172aio_ctx_finalize(GSource *source)
173{
174 AioContext *ctx = (AioContext *) source;
175
9b34277d 176 thread_pool_free(ctx->thread_pool);
2f4dc3c1
PB
177 aio_set_event_notifier(ctx, &ctx->notifier, NULL, NULL);
178 event_notifier_cleanup(&ctx->notifier);
6b5f8762 179 g_array_free(ctx->pollfds, TRUE);
2f4dc3c1
PB
180}
181
e3713e00
PB
182static GSourceFuncs aio_source_funcs = {
183 aio_ctx_prepare,
184 aio_ctx_check,
185 aio_ctx_dispatch,
2f4dc3c1 186 aio_ctx_finalize
e3713e00
PB
187};
188
189GSource *aio_get_g_source(AioContext *ctx)
190{
191 g_source_ref(&ctx->source);
192 return &ctx->source;
193}
a915f4bc 194
9b34277d
SH
195ThreadPool *aio_get_thread_pool(AioContext *ctx)
196{
197 if (!ctx->thread_pool) {
198 ctx->thread_pool = thread_pool_new(ctx);
199 }
200 return ctx->thread_pool;
201}
202
2f4dc3c1
PB
203void aio_notify(AioContext *ctx)
204{
205 event_notifier_set(&ctx->notifier);
206}
207
f627aab1
PB
208AioContext *aio_context_new(void)
209{
2f4dc3c1
PB
210 AioContext *ctx;
211 ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
6b5f8762 212 ctx->pollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
9b34277d 213 ctx->thread_pool = NULL;
2f4dc3c1
PB
214 event_notifier_init(&ctx->notifier, false);
215 aio_set_event_notifier(ctx, &ctx->notifier,
216 (EventNotifierHandler *)
217 event_notifier_test_and_clear, NULL);
218
219 return ctx;
e3713e00
PB
220}
221
222void aio_context_ref(AioContext *ctx)
223{
224 g_source_ref(&ctx->source);
225}
226
227void aio_context_unref(AioContext *ctx)
228{
229 g_source_unref(&ctx->source);
f627aab1 230}