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