]> git.proxmox.com Git - qemu.git/blame - async.c
Split out bottom halves
[qemu.git] / async.c
CommitLineData
4f999d05
KW
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
27/***********************************************************/
28/* bottom halves (can be seen as timers which expire ASAP) */
29
30struct QEMUBH {
31 QEMUBHFunc *cb;
32 void *opaque;
33 int scheduled;
34 int idle;
35 int deleted;
36 QEMUBH *next;
37};
38
39static QEMUBH *first_bh = NULL;
40
41QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
42{
43 QEMUBH *bh;
44 bh = qemu_mallocz(sizeof(QEMUBH));
45 bh->cb = cb;
46 bh->opaque = opaque;
47 bh->next = first_bh;
48 first_bh = bh;
49 return bh;
50}
51
52int qemu_bh_poll(void)
53{
54 QEMUBH *bh, **bhp;
55 int ret;
56
57 ret = 0;
58 for (bh = first_bh; bh; bh = bh->next) {
59 if (!bh->deleted && bh->scheduled) {
60 bh->scheduled = 0;
61 if (!bh->idle)
62 ret = 1;
63 bh->idle = 0;
64 bh->cb(bh->opaque);
65 }
66 }
67
68 /* remove deleted bhs */
69 bhp = &first_bh;
70 while (*bhp) {
71 bh = *bhp;
72 if (bh->deleted) {
73 *bhp = bh->next;
74 qemu_free(bh);
75 } else
76 bhp = &bh->next;
77 }
78
79 return ret;
80}
81
82void qemu_bh_schedule_idle(QEMUBH *bh)
83{
84 if (bh->scheduled)
85 return;
86 bh->scheduled = 1;
87 bh->idle = 1;
88}
89
90void qemu_bh_schedule(QEMUBH *bh)
91{
92 if (bh->scheduled)
93 return;
94 bh->scheduled = 1;
95 bh->idle = 0;
96 /* stop the currently executing CPU to execute the BH ASAP */
97 qemu_notify_event();
98}
99
100void qemu_bh_cancel(QEMUBH *bh)
101{
102 bh->scheduled = 0;
103}
104
105void qemu_bh_delete(QEMUBH *bh)
106{
107 bh->scheduled = 0;
108 bh->deleted = 1;
109}
110
111void qemu_bh_update_timeout(int *timeout)
112{
113 QEMUBH *bh;
114
115 for (bh = first_bh; bh; bh = bh->next) {
116 if (!bh->deleted && bh->scheduled) {
117 if (bh->idle) {
118 /* idle bottom halves will be polled at least
119 * every 10ms */
120 *timeout = MIN(10, *timeout);
121 } else {
122 /* non-idle bottom halves will be executed
123 * immediately */
124 *timeout = 0;
125 break;
126 }
127 }
128 }
129}
130