async.c 6.93 KB
Newer Older
Kevin Wolf's avatar
Kevin Wolf committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * QEMU System Emulator
 *
 * Copyright (c) 2003-2008 Fabrice Bellard
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "qemu-common.h"
26
#include "block/aio.h"
27
#include "block/thread-pool.h"
28
#include "qemu/main-loop.h"
29

Kevin Wolf's avatar
Kevin Wolf committed
30 31 32 33
/***********************************************************/
/* bottom halves (can be seen as timers which expire ASAP) */

struct QEMUBH {
Paolo Bonzini's avatar
Paolo Bonzini committed
34
    AioContext *ctx;
Kevin Wolf's avatar
Kevin Wolf committed
35 36 37
    QEMUBHFunc *cb;
    void *opaque;
    QEMUBH *next;
38 39 40
    bool scheduled;
    bool idle;
    bool deleted;
Kevin Wolf's avatar
Kevin Wolf committed
41 42
};

43
QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
Kevin Wolf's avatar
Kevin Wolf committed
44 45
{
    QEMUBH *bh;
46
    bh = g_malloc0(sizeof(QEMUBH));
Paolo Bonzini's avatar
Paolo Bonzini committed
47
    bh->ctx = ctx;
Kevin Wolf's avatar
Kevin Wolf committed
48 49
    bh->cb = cb;
    bh->opaque = opaque;
50
    qemu_mutex_lock(&ctx->bh_lock);
51
    bh->next = ctx->first_bh;
52 53
    /* Make sure that the members are ready before putting bh into list */
    smp_wmb();
54
    ctx->first_bh = bh;
55
    qemu_mutex_unlock(&ctx->bh_lock);
Kevin Wolf's avatar
Kevin Wolf committed
56 57 58
    return bh;
}

59
/* Multiple occurrences of aio_bh_poll cannot be called concurrently */
60
int aio_bh_poll(AioContext *ctx)
Kevin Wolf's avatar
Kevin Wolf committed
61
{
62
    QEMUBH *bh, **bhp, *next;
Kevin Wolf's avatar
Kevin Wolf committed
63
    int ret;
64

65
    ctx->walking_bh++;
Kevin Wolf's avatar
Kevin Wolf committed
66 67

    ret = 0;
68
    for (bh = ctx->first_bh; bh; bh = next) {
69 70
        /* Make sure that fetching bh happens before accessing its members */
        smp_read_barrier_depends();
71
        next = bh->next;
Kevin Wolf's avatar
Kevin Wolf committed
72 73
        if (!bh->deleted && bh->scheduled) {
            bh->scheduled = 0;
74 75 76 77
            /* Paired with write barrier in bh schedule to ensure reading for
             * idle & callbacks coming after bh's scheduling.
             */
            smp_rmb();
Kevin Wolf's avatar
Kevin Wolf committed
78 79 80 81 82 83 84
            if (!bh->idle)
                ret = 1;
            bh->idle = 0;
            bh->cb(bh->opaque);
        }
    }

85
    ctx->walking_bh--;
86

Kevin Wolf's avatar
Kevin Wolf committed
87
    /* remove deleted bhs */
88
    if (!ctx->walking_bh) {
89
        qemu_mutex_lock(&ctx->bh_lock);
90
        bhp = &ctx->first_bh;
91 92 93 94 95 96 97 98 99
        while (*bhp) {
            bh = *bhp;
            if (bh->deleted) {
                *bhp = bh->next;
                g_free(bh);
            } else {
                bhp = &bh->next;
            }
        }
100
        qemu_mutex_unlock(&ctx->bh_lock);
Kevin Wolf's avatar
Kevin Wolf committed
101 102 103 104 105 106 107 108 109 110
    }

    return ret;
}

void qemu_bh_schedule_idle(QEMUBH *bh)
{
    if (bh->scheduled)
        return;
    bh->idle = 1;
111 112 113 114 115
    /* Make sure that idle & any writes needed by the callback are done
     * before the locations are read in the aio_bh_poll.
     */
    smp_wmb();
    bh->scheduled = 1;
Kevin Wolf's avatar
Kevin Wolf committed
116 117 118 119 120 121 122
}

void qemu_bh_schedule(QEMUBH *bh)
{
    if (bh->scheduled)
        return;
    bh->idle = 0;
123 124 125 126 127
    /* Make sure that idle & any writes needed by the callback are done
     * before the locations are read in the aio_bh_poll.
     */
    smp_wmb();
    bh->scheduled = 1;
Paolo Bonzini's avatar
Paolo Bonzini committed
128
    aio_notify(bh->ctx);
Kevin Wolf's avatar
Kevin Wolf committed
129 130
}

131 132 133

/* This func is async.
 */
Kevin Wolf's avatar
Kevin Wolf committed
134 135 136 137 138
void qemu_bh_cancel(QEMUBH *bh)
{
    bh->scheduled = 0;
}

139 140 141
/* This func is async.The bottom half will do the delete action at the finial
 * end.
 */
Kevin Wolf's avatar
Kevin Wolf committed
142 143 144 145 146 147
void qemu_bh_delete(QEMUBH *bh)
{
    bh->scheduled = 0;
    bh->deleted = 1;
}

148 149
static gboolean
aio_ctx_prepare(GSource *source, gint    *timeout)
Kevin Wolf's avatar
Kevin Wolf committed
150
{
151
    AioContext *ctx = (AioContext *) source;
Kevin Wolf's avatar
Kevin Wolf committed
152
    QEMUBH *bh;
153
    int deadline;
Kevin Wolf's avatar
Kevin Wolf committed
154

155 156
    /* We assume there is no timeout already supplied */
    *timeout = -1;
157
    for (bh = ctx->first_bh; bh; bh = bh->next) {
Kevin Wolf's avatar
Kevin Wolf committed
158 159 160 161
        if (!bh->deleted && bh->scheduled) {
            if (bh->idle) {
                /* idle bottom halves will be polled at least
                 * every 10ms */
162
                *timeout = 10;
Kevin Wolf's avatar
Kevin Wolf committed
163 164 165 166
            } else {
                /* non-idle bottom halves will be executed
                 * immediately */
                *timeout = 0;
167
                return true;
Kevin Wolf's avatar
Kevin Wolf committed
168 169 170
            }
        }
    }
171

172 173 174 175 176 177 178 179
    deadline = qemu_timeout_ns_to_ms(timerlistgroup_deadline_ns(&ctx->tlg));
    if (deadline == 0) {
        *timeout = 0;
        return true;
    } else {
        *timeout = qemu_soonest_timeout(*timeout, deadline);
    }

180
    return false;
181 182 183 184 185 186 187 188 189 190 191 192 193
}

static gboolean
aio_ctx_check(GSource *source)
{
    AioContext *ctx = (AioContext *) source;
    QEMUBH *bh;

    for (bh = ctx->first_bh; bh; bh = bh->next) {
        if (!bh->deleted && bh->scheduled) {
            return true;
	}
    }
194
    return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
195 196 197 198 199 200 201 202 203 204 205 206 207 208
}

static gboolean
aio_ctx_dispatch(GSource     *source,
                 GSourceFunc  callback,
                 gpointer     user_data)
{
    AioContext *ctx = (AioContext *) source;

    assert(callback == NULL);
    aio_poll(ctx, false);
    return true;
}

Paolo Bonzini's avatar
Paolo Bonzini committed
209 210 211 212 213
static void
aio_ctx_finalize(GSource     *source)
{
    AioContext *ctx = (AioContext *) source;

214
    thread_pool_free(ctx->thread_pool);
215
    aio_set_event_notifier(ctx, &ctx->notifier, NULL);
Paolo Bonzini's avatar
Paolo Bonzini committed
216
    event_notifier_cleanup(&ctx->notifier);
217
    qemu_mutex_destroy(&ctx->bh_lock);
218
    g_array_free(ctx->pollfds, TRUE);
219
    timerlistgroup_deinit(&ctx->tlg);
Paolo Bonzini's avatar
Paolo Bonzini committed
220 221
}

222 223 224 225
static GSourceFuncs aio_source_funcs = {
    aio_ctx_prepare,
    aio_ctx_check,
    aio_ctx_dispatch,
Paolo Bonzini's avatar
Paolo Bonzini committed
226
    aio_ctx_finalize
227 228 229 230 231 232 233
};

GSource *aio_get_g_source(AioContext *ctx)
{
    g_source_ref(&ctx->source);
    return &ctx->source;
}
234

235 236 237 238 239 240 241 242
ThreadPool *aio_get_thread_pool(AioContext *ctx)
{
    if (!ctx->thread_pool) {
        ctx->thread_pool = thread_pool_new(ctx);
    }
    return ctx->thread_pool;
}

Paolo Bonzini's avatar
Paolo Bonzini committed
243 244 245 246 247
void aio_notify(AioContext *ctx)
{
    event_notifier_set(&ctx->notifier);
}

248 249 250 251 252
static void aio_timerlist_notify(void *opaque)
{
    aio_notify(opaque);
}

253 254
AioContext *aio_context_new(void)
{
Paolo Bonzini's avatar
Paolo Bonzini committed
255 256
    AioContext *ctx;
    ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
257
    ctx->pollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
258
    ctx->thread_pool = NULL;
259
    qemu_mutex_init(&ctx->bh_lock);
Paolo Bonzini's avatar
Paolo Bonzini committed
260 261 262
    event_notifier_init(&ctx->notifier, false);
    aio_set_event_notifier(ctx, &ctx->notifier, 
                           (EventNotifierHandler *)
263
                           event_notifier_test_and_clear);
264
    timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
Paolo Bonzini's avatar
Paolo Bonzini committed
265 266

    return ctx;
267 268 269 270 271 272 273 274 275 276
}

void aio_context_ref(AioContext *ctx)
{
    g_source_ref(&ctx->source);
}

void aio_context_unref(AioContext *ctx)
{
    g_source_unref(&ctx->source);
277
}