Statistics
| Branch: | Revision:

root / main-loop.h @ f8d3d128

History | View | Annotate | Download (13.5 kB)

1 44a9b356 Paolo Bonzini
/*
2 44a9b356 Paolo Bonzini
 * QEMU System Emulator
3 44a9b356 Paolo Bonzini
 *
4 44a9b356 Paolo Bonzini
 * Copyright (c) 2003-2008 Fabrice Bellard
5 44a9b356 Paolo Bonzini
 *
6 44a9b356 Paolo Bonzini
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 44a9b356 Paolo Bonzini
 * of this software and associated documentation files (the "Software"), to deal
8 44a9b356 Paolo Bonzini
 * in the Software without restriction, including without limitation the rights
9 44a9b356 Paolo Bonzini
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 44a9b356 Paolo Bonzini
 * copies of the Software, and to permit persons to whom the Software is
11 44a9b356 Paolo Bonzini
 * furnished to do so, subject to the following conditions:
12 44a9b356 Paolo Bonzini
 *
13 44a9b356 Paolo Bonzini
 * The above copyright notice and this permission notice shall be included in
14 44a9b356 Paolo Bonzini
 * all copies or substantial portions of the Software.
15 44a9b356 Paolo Bonzini
 *
16 44a9b356 Paolo Bonzini
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 44a9b356 Paolo Bonzini
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 44a9b356 Paolo Bonzini
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 44a9b356 Paolo Bonzini
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 44a9b356 Paolo Bonzini
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 44a9b356 Paolo Bonzini
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 44a9b356 Paolo Bonzini
 * THE SOFTWARE.
23 44a9b356 Paolo Bonzini
 */
24 44a9b356 Paolo Bonzini
25 44a9b356 Paolo Bonzini
#ifndef QEMU_MAIN_LOOP_H
26 44a9b356 Paolo Bonzini
#define QEMU_MAIN_LOOP_H 1
27 44a9b356 Paolo Bonzini
28 44a9b356 Paolo Bonzini
#ifdef SIGRTMIN
29 44a9b356 Paolo Bonzini
#define SIG_IPI (SIGRTMIN+4)
30 44a9b356 Paolo Bonzini
#else
31 44a9b356 Paolo Bonzini
#define SIG_IPI SIGUSR1
32 44a9b356 Paolo Bonzini
#endif
33 44a9b356 Paolo Bonzini
34 44a9b356 Paolo Bonzini
/**
35 44a9b356 Paolo Bonzini
 * qemu_init_main_loop: Set up the process so that it can run the main loop.
36 44a9b356 Paolo Bonzini
 *
37 44a9b356 Paolo Bonzini
 * This includes setting up signal handlers.  It should be called before
38 44a9b356 Paolo Bonzini
 * any other threads are created.  In addition, threads other than the
39 44a9b356 Paolo Bonzini
 * main one should block signals that are trapped by the main loop.
40 44a9b356 Paolo Bonzini
 * For simplicity, you can consider these signals to be safe: SIGUSR1,
41 44a9b356 Paolo Bonzini
 * SIGUSR2, thread signals (SIGFPE, SIGILL, SIGSEGV, SIGBUS) and real-time
42 44a9b356 Paolo Bonzini
 * signals if available.  Remember that Windows in practice does not have
43 44a9b356 Paolo Bonzini
 * signals, though.
44 d34e8f6e Michael Roth
 *
45 d34e8f6e Michael Roth
 * In the case of QEMU tools, this will also start/initialize timers.
46 44a9b356 Paolo Bonzini
 */
47 44a9b356 Paolo Bonzini
int qemu_init_main_loop(void);
48 44a9b356 Paolo Bonzini
49 44a9b356 Paolo Bonzini
/**
50 d34e8f6e Michael Roth
 * main_loop_init: Initializes main loop
51 d34e8f6e Michael Roth
 *
52 d34e8f6e Michael Roth
 * Internal (but shared for compatibility reasons) initialization routine
53 d34e8f6e Michael Roth
 * for the main loop. This should not be used by applications directly,
54 d34e8f6e Michael Roth
 * use qemu_init_main_loop() instead.
55 d34e8f6e Michael Roth
 *
56 d34e8f6e Michael Roth
 */
57 d34e8f6e Michael Roth
int main_loop_init(void);
58 d34e8f6e Michael Roth
59 d34e8f6e Michael Roth
/**
60 44a9b356 Paolo Bonzini
 * main_loop_wait: Run one iteration of the main loop.
61 44a9b356 Paolo Bonzini
 *
62 44a9b356 Paolo Bonzini
 * If @nonblocking is true, poll for events, otherwise suspend until
63 44a9b356 Paolo Bonzini
 * one actually occurs.  The main loop usually consists of a loop that
64 44a9b356 Paolo Bonzini
 * repeatedly calls main_loop_wait(false).
65 44a9b356 Paolo Bonzini
 *
66 44a9b356 Paolo Bonzini
 * Main loop services include file descriptor callbacks, bottom halves
67 44a9b356 Paolo Bonzini
 * and timers (defined in qemu-timer.h).  Bottom halves are similar to timers
68 44a9b356 Paolo Bonzini
 * that execute immediately, but have a lower overhead and scheduling them
69 44a9b356 Paolo Bonzini
 * is wait-free, thread-safe and signal-safe.
70 44a9b356 Paolo Bonzini
 *
71 44a9b356 Paolo Bonzini
 * It is sometimes useful to put a whole program in a coroutine.  In this
72 44a9b356 Paolo Bonzini
 * case, the coroutine actually should be started from within the main loop,
73 44a9b356 Paolo Bonzini
 * so that the main loop can run whenever the coroutine yields.  To do this,
74 44a9b356 Paolo Bonzini
 * you can use a bottom half to enter the coroutine as soon as the main loop
75 44a9b356 Paolo Bonzini
 * starts:
76 44a9b356 Paolo Bonzini
 *
77 44a9b356 Paolo Bonzini
 *     void enter_co_bh(void *opaque) {
78 44a9b356 Paolo Bonzini
 *         QEMUCoroutine *co = opaque;
79 44a9b356 Paolo Bonzini
 *         qemu_coroutine_enter(co, NULL);
80 44a9b356 Paolo Bonzini
 *     }
81 44a9b356 Paolo Bonzini
 *
82 44a9b356 Paolo Bonzini
 *     ...
83 44a9b356 Paolo Bonzini
 *     QEMUCoroutine *co = qemu_coroutine_create(coroutine_entry);
84 44a9b356 Paolo Bonzini
 *     QEMUBH *start_bh = qemu_bh_new(enter_co_bh, co);
85 44a9b356 Paolo Bonzini
 *     qemu_bh_schedule(start_bh);
86 44a9b356 Paolo Bonzini
 *     while (...) {
87 44a9b356 Paolo Bonzini
 *         main_loop_wait(false);
88 44a9b356 Paolo Bonzini
 *     }
89 44a9b356 Paolo Bonzini
 *
90 44a9b356 Paolo Bonzini
 * (In the future we may provide a wrapper for this).
91 44a9b356 Paolo Bonzini
 *
92 44a9b356 Paolo Bonzini
 * @nonblocking: Whether the caller should block until an event occurs.
93 44a9b356 Paolo Bonzini
 */
94 44a9b356 Paolo Bonzini
int main_loop_wait(int nonblocking);
95 44a9b356 Paolo Bonzini
96 44a9b356 Paolo Bonzini
/**
97 44a9b356 Paolo Bonzini
 * qemu_notify_event: Force processing of pending events.
98 44a9b356 Paolo Bonzini
 *
99 44a9b356 Paolo Bonzini
 * Similar to signaling a condition variable, qemu_notify_event forces
100 44a9b356 Paolo Bonzini
 * main_loop_wait to look at pending events and exit.  The caller of
101 44a9b356 Paolo Bonzini
 * main_loop_wait will usually call it again very soon, so qemu_notify_event
102 44a9b356 Paolo Bonzini
 * also has the side effect of recalculating the sets of file descriptors
103 44a9b356 Paolo Bonzini
 * that the main loop waits for.
104 44a9b356 Paolo Bonzini
 *
105 44a9b356 Paolo Bonzini
 * Calling qemu_notify_event is rarely necessary, because main loop
106 44a9b356 Paolo Bonzini
 * services (bottom halves and timers) call it themselves.  One notable
107 44a9b356 Paolo Bonzini
 * exception occurs when using qemu_set_fd_handler2 (see below).
108 44a9b356 Paolo Bonzini
 */
109 44a9b356 Paolo Bonzini
void qemu_notify_event(void);
110 44a9b356 Paolo Bonzini
111 44a9b356 Paolo Bonzini
#ifdef _WIN32
112 44a9b356 Paolo Bonzini
/* return TRUE if no sleep should be done afterwards */
113 44a9b356 Paolo Bonzini
typedef int PollingFunc(void *opaque);
114 44a9b356 Paolo Bonzini
115 44a9b356 Paolo Bonzini
/**
116 44a9b356 Paolo Bonzini
 * qemu_add_polling_cb: Register a Windows-specific polling callback
117 44a9b356 Paolo Bonzini
 *
118 44a9b356 Paolo Bonzini
 * Currently, under Windows some events are polled rather than waited for.
119 44a9b356 Paolo Bonzini
 * Polling callbacks do not ensure that @func is called timely, because
120 44a9b356 Paolo Bonzini
 * the main loop might wait for an arbitrarily long time.  If possible,
121 44a9b356 Paolo Bonzini
 * you should instead create a separate thread that does a blocking poll
122 44a9b356 Paolo Bonzini
 * and set a Win32 event object.  The event can then be passed to
123 44a9b356 Paolo Bonzini
 * qemu_add_wait_object.
124 44a9b356 Paolo Bonzini
 *
125 44a9b356 Paolo Bonzini
 * Polling callbacks really have nothing Windows specific in them, but
126 07f35073 Dong Xu Wang
 * as they are a hack and are currently not necessary under POSIX systems,
127 44a9b356 Paolo Bonzini
 * they are only available when QEMU is running under Windows.
128 44a9b356 Paolo Bonzini
 *
129 44a9b356 Paolo Bonzini
 * @func: The function that does the polling, and returns 1 to force
130 44a9b356 Paolo Bonzini
 * immediate completion of main_loop_wait.
131 44a9b356 Paolo Bonzini
 * @opaque: A pointer-size value that is passed to @func.
132 44a9b356 Paolo Bonzini
 */
133 44a9b356 Paolo Bonzini
int qemu_add_polling_cb(PollingFunc *func, void *opaque);
134 44a9b356 Paolo Bonzini
135 44a9b356 Paolo Bonzini
/**
136 44a9b356 Paolo Bonzini
 * qemu_del_polling_cb: Unregister a Windows-specific polling callback
137 44a9b356 Paolo Bonzini
 *
138 44a9b356 Paolo Bonzini
 * This function removes a callback that was registered with
139 44a9b356 Paolo Bonzini
 * qemu_add_polling_cb.
140 44a9b356 Paolo Bonzini
 *
141 44a9b356 Paolo Bonzini
 * @func: The function that was passed to qemu_add_polling_cb.
142 44a9b356 Paolo Bonzini
 * @opaque: A pointer-size value that was passed to qemu_add_polling_cb.
143 44a9b356 Paolo Bonzini
 */
144 44a9b356 Paolo Bonzini
void qemu_del_polling_cb(PollingFunc *func, void *opaque);
145 44a9b356 Paolo Bonzini
146 44a9b356 Paolo Bonzini
/* Wait objects handling */
147 44a9b356 Paolo Bonzini
typedef void WaitObjectFunc(void *opaque);
148 44a9b356 Paolo Bonzini
149 44a9b356 Paolo Bonzini
/**
150 44a9b356 Paolo Bonzini
 * qemu_add_wait_object: Register a callback for a Windows handle
151 44a9b356 Paolo Bonzini
 *
152 44a9b356 Paolo Bonzini
 * Under Windows, the iohandler mechanism can only be used with sockets.
153 44a9b356 Paolo Bonzini
 * QEMU must use the WaitForMultipleObjects API to wait on other handles.
154 44a9b356 Paolo Bonzini
 * This function registers a #HANDLE with QEMU, so that it will be included
155 44a9b356 Paolo Bonzini
 * in the main loop's calls to WaitForMultipleObjects.  When the handle
156 44a9b356 Paolo Bonzini
 * is in a signaled state, QEMU will call @func.
157 44a9b356 Paolo Bonzini
 *
158 44a9b356 Paolo Bonzini
 * @handle: The Windows handle to be observed.
159 44a9b356 Paolo Bonzini
 * @func: A function to be called when @handle is in a signaled state.
160 44a9b356 Paolo Bonzini
 * @opaque: A pointer-size value that is passed to @func.
161 44a9b356 Paolo Bonzini
 */
162 44a9b356 Paolo Bonzini
int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
163 44a9b356 Paolo Bonzini
164 44a9b356 Paolo Bonzini
/**
165 44a9b356 Paolo Bonzini
 * qemu_del_wait_object: Unregister a callback for a Windows handle
166 44a9b356 Paolo Bonzini
 *
167 44a9b356 Paolo Bonzini
 * This function removes a callback that was registered with
168 44a9b356 Paolo Bonzini
 * qemu_add_wait_object.
169 44a9b356 Paolo Bonzini
 *
170 44a9b356 Paolo Bonzini
 * @func: The function that was passed to qemu_add_wait_object.
171 44a9b356 Paolo Bonzini
 * @opaque: A pointer-size value that was passed to qemu_add_wait_object.
172 44a9b356 Paolo Bonzini
 */
173 44a9b356 Paolo Bonzini
void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
174 44a9b356 Paolo Bonzini
#endif
175 44a9b356 Paolo Bonzini
176 44a9b356 Paolo Bonzini
/* async I/O support */
177 44a9b356 Paolo Bonzini
178 44a9b356 Paolo Bonzini
typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
179 44a9b356 Paolo Bonzini
typedef int IOCanReadHandler(void *opaque);
180 44a9b356 Paolo Bonzini
typedef void IOHandler(void *opaque);
181 44a9b356 Paolo Bonzini
182 44a9b356 Paolo Bonzini
/**
183 44a9b356 Paolo Bonzini
 * qemu_set_fd_handler2: Register a file descriptor with the main loop
184 44a9b356 Paolo Bonzini
 *
185 44a9b356 Paolo Bonzini
 * This function tells the main loop to wake up whenever one of the
186 44a9b356 Paolo Bonzini
 * following conditions is true:
187 44a9b356 Paolo Bonzini
 *
188 44a9b356 Paolo Bonzini
 * 1) if @fd_write is not %NULL, when the file descriptor is writable;
189 44a9b356 Paolo Bonzini
 *
190 44a9b356 Paolo Bonzini
 * 2) if @fd_read is not %NULL, when the file descriptor is readable.
191 44a9b356 Paolo Bonzini
 *
192 44a9b356 Paolo Bonzini
 * @fd_read_poll can be used to disable the @fd_read callback temporarily.
193 44a9b356 Paolo Bonzini
 * This is useful to avoid calling qemu_set_fd_handler2 every time the
194 44a9b356 Paolo Bonzini
 * client becomes interested in reading (or dually, stops being interested).
195 44a9b356 Paolo Bonzini
 * A typical example is when @fd is a listening socket and you want to bound
196 44a9b356 Paolo Bonzini
 * the number of active clients.  Remember to call qemu_notify_event whenever
197 44a9b356 Paolo Bonzini
 * the condition may change from %false to %true.
198 44a9b356 Paolo Bonzini
 *
199 44a9b356 Paolo Bonzini
 * The callbacks that are set up by qemu_set_fd_handler2 are level-triggered.
200 44a9b356 Paolo Bonzini
 * If @fd_read does not read from @fd, or @fd_write does not write to @fd
201 44a9b356 Paolo Bonzini
 * until its buffers are full, they will be called again on the next
202 44a9b356 Paolo Bonzini
 * iteration.
203 44a9b356 Paolo Bonzini
 *
204 44a9b356 Paolo Bonzini
 * @fd: The file descriptor to be observed.  Under Windows it must be
205 44a9b356 Paolo Bonzini
 * a #SOCKET.
206 44a9b356 Paolo Bonzini
 *
207 44a9b356 Paolo Bonzini
 * @fd_read_poll: A function that returns 1 if the @fd_read callback
208 44a9b356 Paolo Bonzini
 * should be fired.  If the function returns 0, the main loop will not
209 44a9b356 Paolo Bonzini
 * end its iteration even if @fd becomes readable.
210 44a9b356 Paolo Bonzini
 *
211 44a9b356 Paolo Bonzini
 * @fd_read: A level-triggered callback that is fired if @fd is readable
212 44a9b356 Paolo Bonzini
 * at the beginning of a main loop iteration, or if it becomes readable
213 44a9b356 Paolo Bonzini
 * during one.
214 44a9b356 Paolo Bonzini
 *
215 44a9b356 Paolo Bonzini
 * @fd_write: A level-triggered callback that is fired when @fd is writable
216 44a9b356 Paolo Bonzini
 * at the beginning of a main loop iteration, or if it becomes writable
217 44a9b356 Paolo Bonzini
 * during one.
218 44a9b356 Paolo Bonzini
 *
219 44a9b356 Paolo Bonzini
 * @opaque: A pointer-sized value that is passed to @fd_read_poll,
220 44a9b356 Paolo Bonzini
 * @fd_read and @fd_write.
221 44a9b356 Paolo Bonzini
 */
222 44a9b356 Paolo Bonzini
int qemu_set_fd_handler2(int fd,
223 44a9b356 Paolo Bonzini
                         IOCanReadHandler *fd_read_poll,
224 44a9b356 Paolo Bonzini
                         IOHandler *fd_read,
225 44a9b356 Paolo Bonzini
                         IOHandler *fd_write,
226 44a9b356 Paolo Bonzini
                         void *opaque);
227 44a9b356 Paolo Bonzini
228 44a9b356 Paolo Bonzini
/**
229 44a9b356 Paolo Bonzini
 * qemu_set_fd_handler: Register a file descriptor with the main loop
230 44a9b356 Paolo Bonzini
 *
231 44a9b356 Paolo Bonzini
 * This function tells the main loop to wake up whenever one of the
232 44a9b356 Paolo Bonzini
 * following conditions is true:
233 44a9b356 Paolo Bonzini
 *
234 44a9b356 Paolo Bonzini
 * 1) if @fd_write is not %NULL, when the file descriptor is writable;
235 44a9b356 Paolo Bonzini
 *
236 44a9b356 Paolo Bonzini
 * 2) if @fd_read is not %NULL, when the file descriptor is readable.
237 44a9b356 Paolo Bonzini
 *
238 44a9b356 Paolo Bonzini
 * The callbacks that are set up by qemu_set_fd_handler are level-triggered.
239 44a9b356 Paolo Bonzini
 * If @fd_read does not read from @fd, or @fd_write does not write to @fd
240 44a9b356 Paolo Bonzini
 * until its buffers are full, they will be called again on the next
241 44a9b356 Paolo Bonzini
 * iteration.
242 44a9b356 Paolo Bonzini
 *
243 44a9b356 Paolo Bonzini
 * @fd: The file descriptor to be observed.  Under Windows it must be
244 44a9b356 Paolo Bonzini
 * a #SOCKET.
245 44a9b356 Paolo Bonzini
 *
246 44a9b356 Paolo Bonzini
 * @fd_read: A level-triggered callback that is fired if @fd is readable
247 44a9b356 Paolo Bonzini
 * at the beginning of a main loop iteration, or if it becomes readable
248 44a9b356 Paolo Bonzini
 * during one.
249 44a9b356 Paolo Bonzini
 *
250 44a9b356 Paolo Bonzini
 * @fd_write: A level-triggered callback that is fired when @fd is writable
251 44a9b356 Paolo Bonzini
 * at the beginning of a main loop iteration, or if it becomes writable
252 44a9b356 Paolo Bonzini
 * during one.
253 44a9b356 Paolo Bonzini
 *
254 44a9b356 Paolo Bonzini
 * @opaque: A pointer-sized value that is passed to @fd_read and @fd_write.
255 44a9b356 Paolo Bonzini
 */
256 44a9b356 Paolo Bonzini
int qemu_set_fd_handler(int fd,
257 44a9b356 Paolo Bonzini
                        IOHandler *fd_read,
258 44a9b356 Paolo Bonzini
                        IOHandler *fd_write,
259 44a9b356 Paolo Bonzini
                        void *opaque);
260 44a9b356 Paolo Bonzini
261 44a9b356 Paolo Bonzini
typedef struct QEMUBH QEMUBH;
262 44a9b356 Paolo Bonzini
typedef void QEMUBHFunc(void *opaque);
263 44a9b356 Paolo Bonzini
264 44a9b356 Paolo Bonzini
/**
265 44a9b356 Paolo Bonzini
 * qemu_bh_new: Allocate a new bottom half structure.
266 44a9b356 Paolo Bonzini
 *
267 44a9b356 Paolo Bonzini
 * Bottom halves are lightweight callbacks whose invocation is guaranteed
268 44a9b356 Paolo Bonzini
 * to be wait-free, thread-safe and signal-safe.  The #QEMUBH structure
269 44a9b356 Paolo Bonzini
 * is opaque and must be allocated prior to its use.
270 44a9b356 Paolo Bonzini
 */
271 44a9b356 Paolo Bonzini
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
272 44a9b356 Paolo Bonzini
273 44a9b356 Paolo Bonzini
/**
274 44a9b356 Paolo Bonzini
 * qemu_bh_schedule: Schedule a bottom half.
275 44a9b356 Paolo Bonzini
 *
276 44a9b356 Paolo Bonzini
 * Scheduling a bottom half interrupts the main loop and causes the
277 44a9b356 Paolo Bonzini
 * execution of the callback that was passed to qemu_bh_new.
278 44a9b356 Paolo Bonzini
 *
279 44a9b356 Paolo Bonzini
 * Bottom halves that are scheduled from a bottom half handler are instantly
280 44a9b356 Paolo Bonzini
 * invoked.  This can create an infinite loop if a bottom half handler
281 44a9b356 Paolo Bonzini
 * schedules itself.
282 44a9b356 Paolo Bonzini
 *
283 44a9b356 Paolo Bonzini
 * @bh: The bottom half to be scheduled.
284 44a9b356 Paolo Bonzini
 */
285 44a9b356 Paolo Bonzini
void qemu_bh_schedule(QEMUBH *bh);
286 44a9b356 Paolo Bonzini
287 44a9b356 Paolo Bonzini
/**
288 44a9b356 Paolo Bonzini
 * qemu_bh_cancel: Cancel execution of a bottom half.
289 44a9b356 Paolo Bonzini
 *
290 44a9b356 Paolo Bonzini
 * Canceling execution of a bottom half undoes the effect of calls to
291 44a9b356 Paolo Bonzini
 * qemu_bh_schedule without freeing its resources yet.  While cancellation
292 44a9b356 Paolo Bonzini
 * itself is also wait-free and thread-safe, it can of course race with the
293 44a9b356 Paolo Bonzini
 * loop that executes bottom halves unless you are holding the iothread
294 44a9b356 Paolo Bonzini
 * mutex.  This makes it mostly useless if you are not holding the mutex.
295 44a9b356 Paolo Bonzini
 *
296 44a9b356 Paolo Bonzini
 * @bh: The bottom half to be canceled.
297 44a9b356 Paolo Bonzini
 */
298 44a9b356 Paolo Bonzini
void qemu_bh_cancel(QEMUBH *bh);
299 44a9b356 Paolo Bonzini
300 44a9b356 Paolo Bonzini
/**
301 44a9b356 Paolo Bonzini
 *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
302 44a9b356 Paolo Bonzini
 *
303 44a9b356 Paolo Bonzini
 * Deleting a bottom half frees the memory that was allocated for it by
304 44a9b356 Paolo Bonzini
 * qemu_bh_new.  It also implies canceling the bottom half if it was
305 44a9b356 Paolo Bonzini
 * scheduled.
306 44a9b356 Paolo Bonzini
 *
307 44a9b356 Paolo Bonzini
 * @bh: The bottom half to be deleted.
308 44a9b356 Paolo Bonzini
 */
309 44a9b356 Paolo Bonzini
void qemu_bh_delete(QEMUBH *bh);
310 44a9b356 Paolo Bonzini
311 44a9b356 Paolo Bonzini
#ifdef CONFIG_POSIX
312 44a9b356 Paolo Bonzini
/**
313 44a9b356 Paolo Bonzini
 * qemu_add_child_watch: Register a child process for reaping.
314 44a9b356 Paolo Bonzini
 *
315 44a9b356 Paolo Bonzini
 * Under POSIX systems, a parent process must read the exit status of
316 44a9b356 Paolo Bonzini
 * its child processes using waitpid, or the operating system will not
317 44a9b356 Paolo Bonzini
 * free some of the resources attached to that process.
318 44a9b356 Paolo Bonzini
 *
319 44a9b356 Paolo Bonzini
 * This function directs the QEMU main loop to observe a child process
320 44a9b356 Paolo Bonzini
 * and call waitpid as soon as it exits; the watch is then removed
321 44a9b356 Paolo Bonzini
 * automatically.  It is useful whenever QEMU forks a child process
322 44a9b356 Paolo Bonzini
 * but will find out about its termination by other means such as a
323 44a9b356 Paolo Bonzini
 * "broken pipe".
324 44a9b356 Paolo Bonzini
 *
325 44a9b356 Paolo Bonzini
 * @pid: The pid that QEMU should observe.
326 44a9b356 Paolo Bonzini
 */
327 44a9b356 Paolo Bonzini
int qemu_add_child_watch(pid_t pid);
328 44a9b356 Paolo Bonzini
#endif
329 44a9b356 Paolo Bonzini
330 d3b12f5d Paolo Bonzini
/**
331 d3b12f5d Paolo Bonzini
 * qemu_mutex_lock_iothread: Lock the main loop mutex.
332 d3b12f5d Paolo Bonzini
 *
333 d3b12f5d Paolo Bonzini
 * This function locks the main loop mutex.  The mutex is taken by
334 d3b12f5d Paolo Bonzini
 * qemu_init_main_loop and always taken except while waiting on
335 d3b12f5d Paolo Bonzini
 * external events (such as with select).  The mutex should be taken
336 d3b12f5d Paolo Bonzini
 * by threads other than the main loop thread when calling
337 d3b12f5d Paolo Bonzini
 * qemu_bh_new(), qemu_set_fd_handler() and basically all other
338 d3b12f5d Paolo Bonzini
 * functions documented in this file.
339 cbcfa041 Paolo Bonzini
 *
340 cbcfa041 Paolo Bonzini
 * NOTE: tools currently are single-threaded and qemu_mutex_lock_iothread
341 cbcfa041 Paolo Bonzini
 * is a no-op there.
342 d3b12f5d Paolo Bonzini
 */
343 d3b12f5d Paolo Bonzini
void qemu_mutex_lock_iothread(void);
344 d3b12f5d Paolo Bonzini
345 d3b12f5d Paolo Bonzini
/**
346 d3b12f5d Paolo Bonzini
 * qemu_mutex_unlock_iothread: Unlock the main loop mutex.
347 d3b12f5d Paolo Bonzini
 *
348 d3b12f5d Paolo Bonzini
 * This function unlocks the main loop mutex.  The mutex is taken by
349 d3b12f5d Paolo Bonzini
 * qemu_init_main_loop and always taken except while waiting on
350 d3b12f5d Paolo Bonzini
 * external events (such as with select).  The mutex should be unlocked
351 d3b12f5d Paolo Bonzini
 * as soon as possible by threads other than the main loop thread,
352 d3b12f5d Paolo Bonzini
 * because it prevents the main loop from processing callbacks,
353 d3b12f5d Paolo Bonzini
 * including timers and bottom halves.
354 cbcfa041 Paolo Bonzini
 *
355 cbcfa041 Paolo Bonzini
 * NOTE: tools currently are single-threaded and qemu_mutex_unlock_iothread
356 cbcfa041 Paolo Bonzini
 * is a no-op there.
357 d3b12f5d Paolo Bonzini
 */
358 d3b12f5d Paolo Bonzini
void qemu_mutex_unlock_iothread(void);
359 d3b12f5d Paolo Bonzini
360 44a9b356 Paolo Bonzini
/* internal interfaces */
361 44a9b356 Paolo Bonzini
362 44a9b356 Paolo Bonzini
void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds);
363 44a9b356 Paolo Bonzini
void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int rc);
364 44a9b356 Paolo Bonzini
365 44a9b356 Paolo Bonzini
void qemu_bh_schedule_idle(QEMUBH *bh);
366 44a9b356 Paolo Bonzini
int qemu_bh_poll(void);
367 44a9b356 Paolo Bonzini
void qemu_bh_update_timeout(int *timeout);
368 44a9b356 Paolo Bonzini
369 44a9b356 Paolo Bonzini
#endif