Statistics
| Branch: | Revision:

root / main-loop.h @ 136be99e

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