Statistics
| Branch: | Revision:

root / async.c @ dc1c13d9

History | View | Annotate | Download (3.4 kB)

1 4f999d05 Kevin Wolf
/*
2 4f999d05 Kevin Wolf
 * QEMU System Emulator
3 4f999d05 Kevin Wolf
 *
4 4f999d05 Kevin Wolf
 * Copyright (c) 2003-2008 Fabrice Bellard
5 4f999d05 Kevin Wolf
 *
6 4f999d05 Kevin Wolf
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 4f999d05 Kevin Wolf
 * of this software and associated documentation files (the "Software"), to deal
8 4f999d05 Kevin Wolf
 * in the Software without restriction, including without limitation the rights
9 4f999d05 Kevin Wolf
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 4f999d05 Kevin Wolf
 * copies of the Software, and to permit persons to whom the Software is
11 4f999d05 Kevin Wolf
 * furnished to do so, subject to the following conditions:
12 4f999d05 Kevin Wolf
 *
13 4f999d05 Kevin Wolf
 * The above copyright notice and this permission notice shall be included in
14 4f999d05 Kevin Wolf
 * all copies or substantial portions of the Software.
15 4f999d05 Kevin Wolf
 *
16 4f999d05 Kevin Wolf
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 4f999d05 Kevin Wolf
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 4f999d05 Kevin Wolf
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 4f999d05 Kevin Wolf
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 4f999d05 Kevin Wolf
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 4f999d05 Kevin Wolf
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 4f999d05 Kevin Wolf
 * THE SOFTWARE.
23 4f999d05 Kevin Wolf
 */
24 4f999d05 Kevin Wolf
25 4f999d05 Kevin Wolf
#include "qemu-common.h"
26 9a1e9481 Kevin Wolf
#include "qemu-aio.h"
27 44a9b356 Paolo Bonzini
#include "main-loop.h"
28 9a1e9481 Kevin Wolf
29 384acbf4 Kevin Wolf
/* Anchor of the list of Bottom Halves belonging to the context */
30 384acbf4 Kevin Wolf
static struct QEMUBH *first_bh;
31 4f999d05 Kevin Wolf
32 4f999d05 Kevin Wolf
/***********************************************************/
33 4f999d05 Kevin Wolf
/* bottom halves (can be seen as timers which expire ASAP) */
34 4f999d05 Kevin Wolf
35 4f999d05 Kevin Wolf
struct QEMUBH {
36 4f999d05 Kevin Wolf
    QEMUBHFunc *cb;
37 4f999d05 Kevin Wolf
    void *opaque;
38 4f999d05 Kevin Wolf
    QEMUBH *next;
39 9b47b17e Stefan Weil
    bool scheduled;
40 9b47b17e Stefan Weil
    bool idle;
41 9b47b17e Stefan Weil
    bool deleted;
42 4f999d05 Kevin Wolf
};
43 4f999d05 Kevin Wolf
44 4f999d05 Kevin Wolf
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
45 4f999d05 Kevin Wolf
{
46 4f999d05 Kevin Wolf
    QEMUBH *bh;
47 7267c094 Anthony Liguori
    bh = g_malloc0(sizeof(QEMUBH));
48 4f999d05 Kevin Wolf
    bh->cb = cb;
49 4f999d05 Kevin Wolf
    bh->opaque = opaque;
50 384acbf4 Kevin Wolf
    bh->next = first_bh;
51 384acbf4 Kevin Wolf
    first_bh = bh;
52 4f999d05 Kevin Wolf
    return bh;
53 4f999d05 Kevin Wolf
}
54 4f999d05 Kevin Wolf
55 4f999d05 Kevin Wolf
int qemu_bh_poll(void)
56 4f999d05 Kevin Wolf
{
57 7887f620 Kevin Wolf
    QEMUBH *bh, **bhp, *next;
58 4f999d05 Kevin Wolf
    int ret;
59 648fb0ea Kevin Wolf
    static int nesting = 0;
60 648fb0ea Kevin Wolf
61 648fb0ea Kevin Wolf
    nesting++;
62 4f999d05 Kevin Wolf
63 4f999d05 Kevin Wolf
    ret = 0;
64 384acbf4 Kevin Wolf
    for (bh = first_bh; bh; bh = next) {
65 7887f620 Kevin Wolf
        next = bh->next;
66 4f999d05 Kevin Wolf
        if (!bh->deleted && bh->scheduled) {
67 4f999d05 Kevin Wolf
            bh->scheduled = 0;
68 4f999d05 Kevin Wolf
            if (!bh->idle)
69 4f999d05 Kevin Wolf
                ret = 1;
70 4f999d05 Kevin Wolf
            bh->idle = 0;
71 4f999d05 Kevin Wolf
            bh->cb(bh->opaque);
72 4f999d05 Kevin Wolf
        }
73 4f999d05 Kevin Wolf
    }
74 4f999d05 Kevin Wolf
75 648fb0ea Kevin Wolf
    nesting--;
76 648fb0ea Kevin Wolf
77 4f999d05 Kevin Wolf
    /* remove deleted bhs */
78 648fb0ea Kevin Wolf
    if (!nesting) {
79 648fb0ea Kevin Wolf
        bhp = &first_bh;
80 648fb0ea Kevin Wolf
        while (*bhp) {
81 648fb0ea Kevin Wolf
            bh = *bhp;
82 648fb0ea Kevin Wolf
            if (bh->deleted) {
83 648fb0ea Kevin Wolf
                *bhp = bh->next;
84 648fb0ea Kevin Wolf
                g_free(bh);
85 648fb0ea Kevin Wolf
            } else {
86 648fb0ea Kevin Wolf
                bhp = &bh->next;
87 648fb0ea Kevin Wolf
            }
88 648fb0ea Kevin Wolf
        }
89 4f999d05 Kevin Wolf
    }
90 4f999d05 Kevin Wolf
91 4f999d05 Kevin Wolf
    return ret;
92 4f999d05 Kevin Wolf
}
93 4f999d05 Kevin Wolf
94 4f999d05 Kevin Wolf
void qemu_bh_schedule_idle(QEMUBH *bh)
95 4f999d05 Kevin Wolf
{
96 4f999d05 Kevin Wolf
    if (bh->scheduled)
97 4f999d05 Kevin Wolf
        return;
98 4f999d05 Kevin Wolf
    bh->scheduled = 1;
99 4f999d05 Kevin Wolf
    bh->idle = 1;
100 4f999d05 Kevin Wolf
}
101 4f999d05 Kevin Wolf
102 4f999d05 Kevin Wolf
void qemu_bh_schedule(QEMUBH *bh)
103 4f999d05 Kevin Wolf
{
104 4f999d05 Kevin Wolf
    if (bh->scheduled)
105 4f999d05 Kevin Wolf
        return;
106 4f999d05 Kevin Wolf
    bh->scheduled = 1;
107 4f999d05 Kevin Wolf
    bh->idle = 0;
108 4f999d05 Kevin Wolf
    /* stop the currently executing CPU to execute the BH ASAP */
109 4f999d05 Kevin Wolf
    qemu_notify_event();
110 4f999d05 Kevin Wolf
}
111 4f999d05 Kevin Wolf
112 4f999d05 Kevin Wolf
void qemu_bh_cancel(QEMUBH *bh)
113 4f999d05 Kevin Wolf
{
114 4f999d05 Kevin Wolf
    bh->scheduled = 0;
115 4f999d05 Kevin Wolf
}
116 4f999d05 Kevin Wolf
117 4f999d05 Kevin Wolf
void qemu_bh_delete(QEMUBH *bh)
118 4f999d05 Kevin Wolf
{
119 4f999d05 Kevin Wolf
    bh->scheduled = 0;
120 4f999d05 Kevin Wolf
    bh->deleted = 1;
121 4f999d05 Kevin Wolf
}
122 4f999d05 Kevin Wolf
123 7c7db755 Stefano Stabellini
void qemu_bh_update_timeout(uint32_t *timeout)
124 4f999d05 Kevin Wolf
{
125 4f999d05 Kevin Wolf
    QEMUBH *bh;
126 4f999d05 Kevin Wolf
127 384acbf4 Kevin Wolf
    for (bh = first_bh; bh; bh = bh->next) {
128 4f999d05 Kevin Wolf
        if (!bh->deleted && bh->scheduled) {
129 4f999d05 Kevin Wolf
            if (bh->idle) {
130 4f999d05 Kevin Wolf
                /* idle bottom halves will be polled at least
131 4f999d05 Kevin Wolf
                 * every 10ms */
132 4f999d05 Kevin Wolf
                *timeout = MIN(10, *timeout);
133 4f999d05 Kevin Wolf
            } else {
134 4f999d05 Kevin Wolf
                /* non-idle bottom halves will be executed
135 4f999d05 Kevin Wolf
                 * immediately */
136 4f999d05 Kevin Wolf
                *timeout = 0;
137 4f999d05 Kevin Wolf
                break;
138 4f999d05 Kevin Wolf
            }
139 4f999d05 Kevin Wolf
        }
140 4f999d05 Kevin Wolf
    }
141 4f999d05 Kevin Wolf
}