Convert io_mem_watch to be a MemoryRegion
[qemu-kvm.git] / async.c
blob332d511ed5e138fd27a7cecc0ee42ba1450a9605
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu-common.h"
26 #include "qemu-aio.h"
27 #include "main-loop.h"
29 /* Anchor of the list of Bottom Halves belonging to the context */
30 static struct QEMUBH *first_bh;
32 /***********************************************************/
33 /* bottom halves (can be seen as timers which expire ASAP) */
35 struct QEMUBH {
36 QEMUBHFunc *cb;
37 void *opaque;
38 int scheduled;
39 int idle;
40 int deleted;
41 QEMUBH *next;
44 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
46 QEMUBH *bh;
47 bh = g_malloc0(sizeof(QEMUBH));
48 bh->cb = cb;
49 bh->opaque = opaque;
50 bh->next = first_bh;
51 first_bh = bh;
52 return bh;
55 int qemu_bh_poll(void)
57 QEMUBH *bh, **bhp, *next;
58 int ret;
59 static int nesting = 0;
61 nesting++;
63 ret = 0;
64 for (bh = first_bh; bh; bh = next) {
65 next = bh->next;
66 if (!bh->deleted && bh->scheduled) {
67 bh->scheduled = 0;
68 if (!bh->idle)
69 ret = 1;
70 bh->idle = 0;
71 bh->cb(bh->opaque);
75 nesting--;
77 /* remove deleted bhs */
78 if (!nesting) {
79 bhp = &first_bh;
80 while (*bhp) {
81 bh = *bhp;
82 if (bh->deleted) {
83 *bhp = bh->next;
84 g_free(bh);
85 } else {
86 bhp = &bh->next;
91 return ret;
94 void qemu_bh_schedule_idle(QEMUBH *bh)
96 if (bh->scheduled)
97 return;
98 bh->scheduled = 1;
99 bh->idle = 1;
102 void qemu_bh_schedule(QEMUBH *bh)
104 if (bh->scheduled)
105 return;
106 bh->scheduled = 1;
107 bh->idle = 0;
108 /* stop the currently executing CPU to execute the BH ASAP */
109 qemu_notify_event();
112 void qemu_bh_cancel(QEMUBH *bh)
114 bh->scheduled = 0;
117 void qemu_bh_delete(QEMUBH *bh)
119 bh->scheduled = 0;
120 bh->deleted = 1;
123 void qemu_bh_update_timeout(int *timeout)
125 QEMUBH *bh;
127 for (bh = 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 = MIN(10, *timeout);
133 } else {
134 /* non-idle bottom halves will be executed
135 * immediately */
136 *timeout = 0;
137 break;