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