Statistics
| Branch: | Revision:

root / async.c @ a2247f8e

History | View | Annotate | Download (5.3 kB)

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"
26
#include "block/aio.h"
27
#include "block/thread-pool.h"
28
#include "qemu/main-loop.h"
29

    
30
/***********************************************************/
31
/* bottom halves (can be seen as timers which expire ASAP) */
32

    
33
struct QEMUBH {
34
    AioContext *ctx;
35
    QEMUBHFunc *cb;
36
    void *opaque;
37
    QEMUBH *next;
38
    bool scheduled;
39
    bool idle;
40
    bool deleted;
41
};
42

    
43
QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
44
{
45
    QEMUBH *bh;
46
    bh = g_malloc0(sizeof(QEMUBH));
47
    bh->ctx = ctx;
48
    bh->cb = cb;
49
    bh->opaque = opaque;
50
    bh->next = ctx->first_bh;
51
    ctx->first_bh = bh;
52
    return bh;
53
}
54

    
55
int aio_bh_poll(AioContext *ctx)
56
{
57
    QEMUBH *bh, **bhp, *next;
58
    int ret;
59

    
60
    ctx->walking_bh++;
61

    
62
    ret = 0;
63
    for (bh = ctx->first_bh; bh; bh = next) {
64
        next = bh->next;
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

    
74
    ctx->walking_bh--;
75

    
76
    /* remove deleted bhs */
77
    if (!ctx->walking_bh) {
78
        bhp = &ctx->first_bh;
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
        }
88
    }
89

    
90
    return ret;
91
}
92

    
93
void qemu_bh_schedule_idle(QEMUBH *bh)
94
{
95
    if (bh->scheduled)
96
        return;
97
    bh->scheduled = 1;
98
    bh->idle = 1;
99
}
100

    
101
void qemu_bh_schedule(QEMUBH *bh)
102
{
103
    if (bh->scheduled)
104
        return;
105
    bh->scheduled = 1;
106
    bh->idle = 0;
107
    aio_notify(bh->ctx);
108
}
109

    
110
void qemu_bh_cancel(QEMUBH *bh)
111
{
112
    bh->scheduled = 0;
113
}
114

    
115
void qemu_bh_delete(QEMUBH *bh)
116
{
117
    bh->scheduled = 0;
118
    bh->deleted = 1;
119
}
120

    
121
static gboolean
122
aio_ctx_prepare(GSource *source, gint    *timeout)
123
{
124
    AioContext *ctx = (AioContext *) source;
125
    QEMUBH *bh;
126

    
127
    for (bh = ctx->first_bh; bh; bh = bh->next) {
128
        if (!bh->deleted && bh->scheduled) {
129
            if (bh->idle) {
130
                /* idle bottom halves will be polled at least
131
                 * every 10ms */
132
                *timeout = 10;
133
            } else {
134
                /* non-idle bottom halves will be executed
135
                 * immediately */
136
                *timeout = 0;
137
                return true;
138
            }
139
        }
140
    }
141

    
142
    return false;
143
}
144

    
145
static gboolean
146
aio_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

    
159
static gboolean
160
aio_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

    
171
static void
172
aio_ctx_finalize(GSource     *source)
173
{
174
    AioContext *ctx = (AioContext *) source;
175

    
176
    thread_pool_free(ctx->thread_pool);
177
    aio_set_event_notifier(ctx, &ctx->notifier, NULL, NULL);
178
    event_notifier_cleanup(&ctx->notifier);
179
    g_array_free(ctx->pollfds, TRUE);
180
}
181

    
182
static GSourceFuncs aio_source_funcs = {
183
    aio_ctx_prepare,
184
    aio_ctx_check,
185
    aio_ctx_dispatch,
186
    aio_ctx_finalize
187
};
188

    
189
GSource *aio_get_g_source(AioContext *ctx)
190
{
191
    g_source_ref(&ctx->source);
192
    return &ctx->source;
193
}
194

    
195
ThreadPool *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

    
203
void aio_notify(AioContext *ctx)
204
{
205
    event_notifier_set(&ctx->notifier);
206
}
207

    
208
AioContext *aio_context_new(void)
209
{
210
    AioContext *ctx;
211
    ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
212
    ctx->pollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
213
    ctx->thread_pool = NULL;
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;
220
}
221

    
222
void aio_context_ref(AioContext *ctx)
223
{
224
    g_source_ref(&ctx->source);
225
}
226

    
227
void aio_context_unref(AioContext *ctx)
228
{
229
    g_source_unref(&ctx->source);
230
}