]> git.proxmox.com Git - qemu.git/blob - async.c
Version 1.0.1
[qemu.git] / async.c
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 "qemu-aio.h"
27 #include "main-loop.h"
28
29 /* Anchor of the list of Bottom Halves belonging to the context */
30 static struct QEMUBH *first_bh;
31
32 /***********************************************************/
33 /* bottom halves (can be seen as timers which expire ASAP) */
34
35 struct QEMUBH {
36 QEMUBHFunc *cb;
37 void *opaque;
38 int scheduled;
39 int idle;
40 int deleted;
41 QEMUBH *next;
42 };
43
44 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
45 {
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;
53 }
54
55 int qemu_bh_poll(void)
56 {
57 QEMUBH *bh, **bhp, *next;
58 int ret;
59 static int nesting = 0;
60
61 nesting++;
62
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);
72 }
73 }
74
75 nesting--;
76
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;
87 }
88 }
89 }
90
91 return ret;
92 }
93
94 void qemu_bh_schedule_idle(QEMUBH *bh)
95 {
96 if (bh->scheduled)
97 return;
98 bh->scheduled = 1;
99 bh->idle = 1;
100 }
101
102 void qemu_bh_schedule(QEMUBH *bh)
103 {
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();
110 }
111
112 void qemu_bh_cancel(QEMUBH *bh)
113 {
114 bh->scheduled = 0;
115 }
116
117 void qemu_bh_delete(QEMUBH *bh)
118 {
119 bh->scheduled = 0;
120 bh->deleted = 1;
121 }
122
123 void qemu_bh_update_timeout(int *timeout)
124 {
125 QEMUBH *bh;
126
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;
138 }
139 }
140 }
141 }
142