blob: 90fe906539f70815a5cfbaa95b42fab9f11ce228 [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"
Paolo Bonzini737e1502012-12-17 18:19:44 +010026#include "block/aio.h"
Stefan Hajnoczi9b342772013-03-07 13:41:47 +010027#include "block/thread-pool.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/main-loop.h"
Kevin Wolf9a1e9482009-10-22 17:54:38 +020029
Kevin Wolf4f999d02009-10-22 17:54:37 +020030/***********************************************************/
31/* bottom halves (can be seen as timers which expire ASAP) */
32
33struct QEMUBH {
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +020034 AioContext *ctx;
Kevin Wolf4f999d02009-10-22 17:54:37 +020035 QEMUBHFunc *cb;
36 void *opaque;
Kevin Wolf4f999d02009-10-22 17:54:37 +020037 QEMUBH *next;
Stefan Weil9b47b172012-04-29 19:08:45 +020038 bool scheduled;
39 bool idle;
40 bool deleted;
Kevin Wolf4f999d02009-10-22 17:54:37 +020041};
42
Paolo Bonzinif627aab2012-10-29 23:45:23 +010043QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
Kevin Wolf4f999d02009-10-22 17:54:37 +020044{
45 QEMUBH *bh;
Anthony Liguori7267c092011-08-20 22:09:37 -050046 bh = g_malloc0(sizeof(QEMUBH));
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +020047 bh->ctx = ctx;
Kevin Wolf4f999d02009-10-22 17:54:37 +020048 bh->cb = cb;
49 bh->opaque = opaque;
Paolo Bonzinif627aab2012-10-29 23:45:23 +010050 bh->next = ctx->first_bh;
51 ctx->first_bh = bh;
Kevin Wolf4f999d02009-10-22 17:54:37 +020052 return bh;
53}
54
Paolo Bonzinif627aab2012-10-29 23:45:23 +010055int aio_bh_poll(AioContext *ctx)
Kevin Wolf4f999d02009-10-22 17:54:37 +020056{
Kevin Wolf7887f622011-06-07 17:51:21 +020057 QEMUBH *bh, **bhp, *next;
Kevin Wolf4f999d02009-10-22 17:54:37 +020058 int ret;
Kevin Wolf648fb0e2011-09-01 16:16:10 +020059
Paolo Bonzinif627aab2012-10-29 23:45:23 +010060 ctx->walking_bh++;
Kevin Wolf4f999d02009-10-22 17:54:37 +020061
62 ret = 0;
Paolo Bonzinif627aab2012-10-29 23:45:23 +010063 for (bh = ctx->first_bh; bh; bh = next) {
Kevin Wolf7887f622011-06-07 17:51:21 +020064 next = bh->next;
Kevin Wolf4f999d02009-10-22 17:54:37 +020065 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
Paolo Bonzinif627aab2012-10-29 23:45:23 +010074 ctx->walking_bh--;
Kevin Wolf648fb0e2011-09-01 16:16:10 +020075
Kevin Wolf4f999d02009-10-22 17:54:37 +020076 /* remove deleted bhs */
Paolo Bonzinif627aab2012-10-29 23:45:23 +010077 if (!ctx->walking_bh) {
78 bhp = &ctx->first_bh;
Kevin Wolf648fb0e2011-09-01 16:16:10 +020079 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 }
Kevin Wolf4f999d02009-10-22 17:54:37 +020088 }
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;
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200107 aio_notify(bh->ctx);
Kevin Wolf4f999d02009-10-22 17:54:37 +0200108}
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
Paolo Bonzini22bfa752012-09-24 15:11:48 +0200121static gboolean
122aio_ctx_prepare(GSource *source, gint *timeout)
Kevin Wolf4f999d02009-10-22 17:54:37 +0200123{
Paolo Bonzini22bfa752012-09-24 15:11:48 +0200124 AioContext *ctx = (AioContext *) source;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200125 QEMUBH *bh;
126
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100127 for (bh = ctx->first_bh; bh; bh = bh->next) {
Kevin Wolf4f999d02009-10-22 17:54:37 +0200128 if (!bh->deleted && bh->scheduled) {
129 if (bh->idle) {
130 /* idle bottom halves will be polled at least
131 * every 10ms */
Paolo Bonzini22bfa752012-09-24 15:11:48 +0200132 *timeout = 10;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200133 } else {
134 /* non-idle bottom halves will be executed
135 * immediately */
136 *timeout = 0;
Paolo Bonzinif5022a12012-11-12 13:30:10 +0100137 return true;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200138 }
139 }
140 }
Kevin Wolf4f999d02009-10-22 17:54:37 +0200141
Paolo Bonzinif5022a12012-11-12 13:30:10 +0100142 return false;
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200143}
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
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200171static void
172aio_ctx_finalize(GSource *source)
173{
174 AioContext *ctx = (AioContext *) source;
175
Stefan Hajnoczi9b342772013-03-07 13:41:47 +0100176 thread_pool_free(ctx->thread_pool);
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200177 aio_set_event_notifier(ctx, &ctx->notifier, NULL, NULL);
178 event_notifier_cleanup(&ctx->notifier);
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100179 g_array_free(ctx->pollfds, TRUE);
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200180}
181
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200182static GSourceFuncs aio_source_funcs = {
183 aio_ctx_prepare,
184 aio_ctx_check,
185 aio_ctx_dispatch,
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200186 aio_ctx_finalize
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200187};
188
189GSource *aio_get_g_source(AioContext *ctx)
190{
191 g_source_ref(&ctx->source);
192 return &ctx->source;
193}
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200194
Stefan Hajnoczi9b342772013-03-07 13:41:47 +0100195ThreadPool *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
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));
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100212 ctx->pollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
Stefan Hajnoczi9b342772013-03-07 13:41:47 +0100213 ctx->thread_pool = NULL;
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200214 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;
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200220}
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);
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100230}