blob: 85cc6410c50259398beef693ec00b649823dcde3 [file] [log] [blame]
Kevin Wolf4f999d02009-10-22 17:54:37 +02001/*
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"
Kevin Wolf9a1e9482009-10-22 17:54:38 +020026#include "qemu-aio.h"
Paolo Bonzini44a9b352011-09-12 16:44:30 +020027#include "main-loop.h"
Kevin Wolf9a1e9482009-10-22 17:54:38 +020028
Kevin Wolf384acbf2011-07-15 16:36:40 +020029/* Anchor of the list of Bottom Halves belonging to the context */
30static struct QEMUBH *first_bh;
Kevin Wolf4f999d02009-10-22 17:54:37 +020031
32/***********************************************************/
33/* bottom halves (can be seen as timers which expire ASAP) */
34
35struct QEMUBH {
36 QEMUBHFunc *cb;
37 void *opaque;
Kevin Wolf4f999d02009-10-22 17:54:37 +020038 QEMUBH *next;
Stefan Weil9b47b172012-04-29 19:08:45 +020039 bool scheduled;
40 bool idle;
41 bool deleted;
Kevin Wolf4f999d02009-10-22 17:54:37 +020042};
43
Kevin Wolf4f999d02009-10-22 17:54:37 +020044QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
45{
46 QEMUBH *bh;
Anthony Liguori7267c092011-08-20 22:09:37 -050047 bh = g_malloc0(sizeof(QEMUBH));
Kevin Wolf4f999d02009-10-22 17:54:37 +020048 bh->cb = cb;
49 bh->opaque = opaque;
Kevin Wolf384acbf2011-07-15 16:36:40 +020050 bh->next = first_bh;
51 first_bh = bh;
Kevin Wolf4f999d02009-10-22 17:54:37 +020052 return bh;
53}
54
55int qemu_bh_poll(void)
56{
Kevin Wolf7887f622011-06-07 17:51:21 +020057 QEMUBH *bh, **bhp, *next;
Kevin Wolf4f999d02009-10-22 17:54:37 +020058 int ret;
Kevin Wolf648fb0e2011-09-01 16:16:10 +020059 static int nesting = 0;
60
61 nesting++;
Kevin Wolf4f999d02009-10-22 17:54:37 +020062
63 ret = 0;
Kevin Wolf384acbf2011-07-15 16:36:40 +020064 for (bh = first_bh; bh; bh = next) {
Kevin Wolf7887f622011-06-07 17:51:21 +020065 next = bh->next;
Kevin Wolf4f999d02009-10-22 17:54:37 +020066 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
Kevin Wolf648fb0e2011-09-01 16:16:10 +020075 nesting--;
76
Kevin Wolf4f999d02009-10-22 17:54:37 +020077 /* remove deleted bhs */
Kevin Wolf648fb0e2011-09-01 16:16:10 +020078 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 }
Kevin Wolf4f999d02009-10-22 17:54:37 +020089 }
90
91 return ret;
92}
93
94void qemu_bh_schedule_idle(QEMUBH *bh)
95{
96 if (bh->scheduled)
97 return;
98 bh->scheduled = 1;
99 bh->idle = 1;
100}
101
102void 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
112void qemu_bh_cancel(QEMUBH *bh)
113{
114 bh->scheduled = 0;
115}
116
117void qemu_bh_delete(QEMUBH *bh)
118{
119 bh->scheduled = 0;
120 bh->deleted = 1;
121}
122
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100123void qemu_bh_update_timeout(uint32_t *timeout)
Kevin Wolf4f999d02009-10-22 17:54:37 +0200124{
125 QEMUBH *bh;
126
Kevin Wolf384acbf2011-07-15 16:36:40 +0200127 for (bh = first_bh; bh; bh = bh->next) {
Kevin Wolf4f999d02009-10-22 17:54:37 +0200128 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