Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 141 lines (124 sloc) 3.505 kb
4f999d05 » Kevin Wolf
2009-10-22 Split out bottom halves
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"
9a1e9481 » Kevin Wolf
2009-10-22 Introduce contexts for asynchronous callbacks
26 #include "qemu-aio.h"
44a9b356 » bonzini
2011-09-12 main-loop: create main-loop.h
27 #include "main-loop.h"
9a1e9481 » Kevin Wolf
2009-10-22 Introduce contexts for asynchronous callbacks
28
384acbf4 » Kevin Wolf
2011-07-15 async: Remove AsyncContext
29 /* Anchor of the list of Bottom Halves belonging to the context */
30 static struct QEMUBH *first_bh;
4f999d05 » Kevin Wolf
2009-10-22 Split out bottom halves
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;
7267c094 » Anthony Liguori
2011-08-20 Use glib memory allocation and free functions
47 bh = g_malloc0(sizeof(QEMUBH));
4f999d05 » Kevin Wolf
2009-10-22 Split out bottom halves
48 bh->cb = cb;
49 bh->opaque = opaque;
384acbf4 » Kevin Wolf
2011-07-15 async: Remove AsyncContext
50 bh->next = first_bh;
51 first_bh = bh;
4f999d05 » Kevin Wolf
2009-10-22 Split out bottom halves
52 return bh;
53 }
54
55 int qemu_bh_poll(void)
56 {
7887f620 » Kevin Wolf
2011-06-07 Allow nested qemu_bh_poll() after BH deletion
57 QEMUBH *bh, **bhp, *next;
4f999d05 » Kevin Wolf
2009-10-22 Split out bottom halves
58 int ret;
648fb0ea » Kevin Wolf
2011-09-01 async: Allow nested qemu_bh_poll calls
59 static int nesting = 0;
60
61 nesting++;
4f999d05 » Kevin Wolf
2009-10-22 Split out bottom halves
62
63 ret = 0;
384acbf4 » Kevin Wolf
2011-07-15 async: Remove AsyncContext
64 for (bh = first_bh; bh; bh = next) {
7887f620 » Kevin Wolf
2011-06-07 Allow nested qemu_bh_poll() after BH deletion
65 next = bh->next;
4f999d05 » Kevin Wolf
2009-10-22 Split out bottom halves
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
648fb0ea » Kevin Wolf
2011-09-01 async: Allow nested qemu_bh_poll calls
75 nesting--;
76
4f999d05 » Kevin Wolf
2009-10-22 Split out bottom halves
77 /* remove deleted bhs */
648fb0ea » Kevin Wolf
2011-09-01 async: Allow nested qemu_bh_poll calls
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 }
4f999d05 » Kevin Wolf
2009-10-22 Split out bottom halves
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
384acbf4 » Kevin Wolf
2011-07-15 async: Remove AsyncContext
127 for (bh = first_bh; bh; bh = bh->next) {
4f999d05 » Kevin Wolf
2009-10-22 Split out bottom halves
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
Something went wrong with that request. Please try again.