Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 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" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 26 | #include "block/aio.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 27 | #include "qemu/main-loop.h" |
Kevin Wolf | 9a1e948 | 2009-10-22 17:54:38 +0200 | [diff] [blame] | 28 | |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 29 | /***********************************************************/ |
| 30 | /* bottom halves (can be seen as timers which expire ASAP) */ |
| 31 | |
| 32 | struct QEMUBH { |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 33 | AioContext *ctx; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 34 | QEMUBHFunc *cb; |
| 35 | void *opaque; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 36 | QEMUBH *next; |
Stefan Weil | 9b47b17 | 2012-04-29 19:08:45 +0200 | [diff] [blame] | 37 | bool scheduled; |
| 38 | bool idle; |
| 39 | bool deleted; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
Paolo Bonzini | f627aab | 2012-10-29 23:45:23 +0100 | [diff] [blame] | 42 | QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque) |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 43 | { |
| 44 | QEMUBH *bh; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 45 | bh = g_malloc0(sizeof(QEMUBH)); |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 46 | bh->ctx = ctx; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 47 | bh->cb = cb; |
| 48 | bh->opaque = opaque; |
Paolo Bonzini | f627aab | 2012-10-29 23:45:23 +0100 | [diff] [blame] | 49 | bh->next = ctx->first_bh; |
| 50 | ctx->first_bh = bh; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 51 | return bh; |
| 52 | } |
| 53 | |
Paolo Bonzini | f627aab | 2012-10-29 23:45:23 +0100 | [diff] [blame] | 54 | int aio_bh_poll(AioContext *ctx) |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 55 | { |
Kevin Wolf | 7887f62 | 2011-06-07 17:51:21 +0200 | [diff] [blame] | 56 | QEMUBH *bh, **bhp, *next; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 57 | int ret; |
Kevin Wolf | 648fb0e | 2011-09-01 16:16:10 +0200 | [diff] [blame] | 58 | |
Paolo Bonzini | f627aab | 2012-10-29 23:45:23 +0100 | [diff] [blame] | 59 | ctx->walking_bh++; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 60 | |
| 61 | ret = 0; |
Paolo Bonzini | f627aab | 2012-10-29 23:45:23 +0100 | [diff] [blame] | 62 | for (bh = ctx->first_bh; bh; bh = next) { |
Kevin Wolf | 7887f62 | 2011-06-07 17:51:21 +0200 | [diff] [blame] | 63 | next = bh->next; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 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 | |
Paolo Bonzini | f627aab | 2012-10-29 23:45:23 +0100 | [diff] [blame] | 73 | ctx->walking_bh--; |
Kevin Wolf | 648fb0e | 2011-09-01 16:16:10 +0200 | [diff] [blame] | 74 | |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 75 | /* remove deleted bhs */ |
Paolo Bonzini | f627aab | 2012-10-29 23:45:23 +0100 | [diff] [blame] | 76 | if (!ctx->walking_bh) { |
| 77 | bhp = &ctx->first_bh; |
Kevin Wolf | 648fb0e | 2011-09-01 16:16:10 +0200 | [diff] [blame] | 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 | } |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 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; |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 106 | aio_notify(bh->ctx); |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 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 | |
Paolo Bonzini | 22bfa75 | 2012-09-24 15:11:48 +0200 | [diff] [blame] | 120 | static gboolean |
| 121 | aio_ctx_prepare(GSource *source, gint *timeout) |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 122 | { |
Paolo Bonzini | 22bfa75 | 2012-09-24 15:11:48 +0200 | [diff] [blame] | 123 | AioContext *ctx = (AioContext *) source; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 124 | QEMUBH *bh; |
| 125 | |
Paolo Bonzini | f627aab | 2012-10-29 23:45:23 +0100 | [diff] [blame] | 126 | for (bh = ctx->first_bh; bh; bh = bh->next) { |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 127 | if (!bh->deleted && bh->scheduled) { |
| 128 | if (bh->idle) { |
| 129 | /* idle bottom halves will be polled at least |
| 130 | * every 10ms */ |
Paolo Bonzini | 22bfa75 | 2012-09-24 15:11:48 +0200 | [diff] [blame] | 131 | *timeout = 10; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 132 | } else { |
| 133 | /* non-idle bottom halves will be executed |
| 134 | * immediately */ |
| 135 | *timeout = 0; |
Paolo Bonzini | f5022a1 | 2012-11-12 13:30:10 +0100 | [diff] [blame] | 136 | return true; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 140 | |
Paolo Bonzini | f5022a1 | 2012-11-12 13:30:10 +0100 | [diff] [blame] | 141 | return false; |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static gboolean |
| 145 | aio_ctx_check(GSource *source) |
| 146 | { |
| 147 | AioContext *ctx = (AioContext *) source; |
| 148 | QEMUBH *bh; |
| 149 | |
| 150 | for (bh = ctx->first_bh; bh; bh = bh->next) { |
| 151 | if (!bh->deleted && bh->scheduled) { |
| 152 | return true; |
| 153 | } |
| 154 | } |
| 155 | return aio_pending(ctx); |
| 156 | } |
| 157 | |
| 158 | static gboolean |
| 159 | aio_ctx_dispatch(GSource *source, |
| 160 | GSourceFunc callback, |
| 161 | gpointer user_data) |
| 162 | { |
| 163 | AioContext *ctx = (AioContext *) source; |
| 164 | |
| 165 | assert(callback == NULL); |
| 166 | aio_poll(ctx, false); |
| 167 | return true; |
| 168 | } |
| 169 | |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 170 | static void |
| 171 | aio_ctx_finalize(GSource *source) |
| 172 | { |
| 173 | AioContext *ctx = (AioContext *) source; |
| 174 | |
| 175 | aio_set_event_notifier(ctx, &ctx->notifier, NULL, NULL); |
| 176 | event_notifier_cleanup(&ctx->notifier); |
| 177 | } |
| 178 | |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 179 | static GSourceFuncs aio_source_funcs = { |
| 180 | aio_ctx_prepare, |
| 181 | aio_ctx_check, |
| 182 | aio_ctx_dispatch, |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 183 | aio_ctx_finalize |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | GSource *aio_get_g_source(AioContext *ctx) |
| 187 | { |
| 188 | g_source_ref(&ctx->source); |
| 189 | return &ctx->source; |
| 190 | } |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 191 | |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 192 | void aio_notify(AioContext *ctx) |
| 193 | { |
| 194 | event_notifier_set(&ctx->notifier); |
| 195 | } |
| 196 | |
Paolo Bonzini | f627aab | 2012-10-29 23:45:23 +0100 | [diff] [blame] | 197 | AioContext *aio_context_new(void) |
| 198 | { |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 199 | AioContext *ctx; |
| 200 | ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext)); |
| 201 | event_notifier_init(&ctx->notifier, false); |
| 202 | aio_set_event_notifier(ctx, &ctx->notifier, |
| 203 | (EventNotifierHandler *) |
| 204 | event_notifier_test_and_clear, NULL); |
| 205 | |
| 206 | return ctx; |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void aio_context_ref(AioContext *ctx) |
| 210 | { |
| 211 | g_source_ref(&ctx->source); |
| 212 | } |
| 213 | |
| 214 | void aio_context_unref(AioContext *ctx) |
| 215 | { |
| 216 | g_source_unref(&ctx->source); |
Paolo Bonzini | f627aab | 2012-10-29 23:45:23 +0100 | [diff] [blame] | 217 | } |