xref: /qemu/include/qemu/main-loop.h (revision ccfaf783)
11de7afc9SPaolo Bonzini /*
21de7afc9SPaolo Bonzini  * QEMU System Emulator
31de7afc9SPaolo Bonzini  *
41de7afc9SPaolo Bonzini  * Copyright (c) 2003-2008 Fabrice Bellard
51de7afc9SPaolo Bonzini  *
61de7afc9SPaolo Bonzini  * Permission is hereby granted, free of charge, to any person obtaining a copy
71de7afc9SPaolo Bonzini  * of this software and associated documentation files (the "Software"), to deal
81de7afc9SPaolo Bonzini  * in the Software without restriction, including without limitation the rights
91de7afc9SPaolo Bonzini  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
101de7afc9SPaolo Bonzini  * copies of the Software, and to permit persons to whom the Software is
111de7afc9SPaolo Bonzini  * furnished to do so, subject to the following conditions:
121de7afc9SPaolo Bonzini  *
131de7afc9SPaolo Bonzini  * The above copyright notice and this permission notice shall be included in
141de7afc9SPaolo Bonzini  * all copies or substantial portions of the Software.
151de7afc9SPaolo Bonzini  *
161de7afc9SPaolo Bonzini  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171de7afc9SPaolo Bonzini  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181de7afc9SPaolo Bonzini  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
191de7afc9SPaolo Bonzini  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
201de7afc9SPaolo Bonzini  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
211de7afc9SPaolo Bonzini  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
221de7afc9SPaolo Bonzini  * THE SOFTWARE.
231de7afc9SPaolo Bonzini  */
241de7afc9SPaolo Bonzini 
251de7afc9SPaolo Bonzini #ifndef QEMU_MAIN_LOOP_H
26175de524SMarkus Armbruster #define QEMU_MAIN_LOOP_H
271de7afc9SPaolo Bonzini 
281de7afc9SPaolo Bonzini #include "block/aio.h"
291de7afc9SPaolo Bonzini 
301de7afc9SPaolo Bonzini #define SIG_IPI SIGUSR1
311de7afc9SPaolo Bonzini 
321de7afc9SPaolo Bonzini /**
331de7afc9SPaolo Bonzini  * qemu_init_main_loop: Set up the process so that it can run the main loop.
341de7afc9SPaolo Bonzini  *
351de7afc9SPaolo Bonzini  * This includes setting up signal handlers.  It should be called before
361de7afc9SPaolo Bonzini  * any other threads are created.  In addition, threads other than the
371de7afc9SPaolo Bonzini  * main one should block signals that are trapped by the main loop.
381de7afc9SPaolo Bonzini  * For simplicity, you can consider these signals to be safe: SIGUSR1,
391de7afc9SPaolo Bonzini  * SIGUSR2, thread signals (SIGFPE, SIGILL, SIGSEGV, SIGBUS) and real-time
401de7afc9SPaolo Bonzini  * signals if available.  Remember that Windows in practice does not have
411de7afc9SPaolo Bonzini  * signals, though.
421de7afc9SPaolo Bonzini  *
431de7afc9SPaolo Bonzini  * In the case of QEMU tools, this will also start/initialize timers.
441de7afc9SPaolo Bonzini  */
452f78e491SChrysostomos Nanakos int qemu_init_main_loop(Error **errp);
461de7afc9SPaolo Bonzini 
471de7afc9SPaolo Bonzini /**
481de7afc9SPaolo Bonzini  * main_loop_wait: Run one iteration of the main loop.
491de7afc9SPaolo Bonzini  *
501de7afc9SPaolo Bonzini  * If @nonblocking is true, poll for events, otherwise suspend until
511de7afc9SPaolo Bonzini  * one actually occurs.  The main loop usually consists of a loop that
521de7afc9SPaolo Bonzini  * repeatedly calls main_loop_wait(false).
531de7afc9SPaolo Bonzini  *
541de7afc9SPaolo Bonzini  * Main loop services include file descriptor callbacks, bottom halves
55a275e934SStefan Weil  * and timers (defined in qemu/timer.h).  Bottom halves are similar to timers
561de7afc9SPaolo Bonzini  * that execute immediately, but have a lower overhead and scheduling them
571de7afc9SPaolo Bonzini  * is wait-free, thread-safe and signal-safe.
581de7afc9SPaolo Bonzini  *
591de7afc9SPaolo Bonzini  * It is sometimes useful to put a whole program in a coroutine.  In this
601de7afc9SPaolo Bonzini  * case, the coroutine actually should be started from within the main loop,
611de7afc9SPaolo Bonzini  * so that the main loop can run whenever the coroutine yields.  To do this,
621de7afc9SPaolo Bonzini  * you can use a bottom half to enter the coroutine as soon as the main loop
631de7afc9SPaolo Bonzini  * starts:
641de7afc9SPaolo Bonzini  *
651de7afc9SPaolo Bonzini  *     void enter_co_bh(void *opaque) {
661de7afc9SPaolo Bonzini  *         QEMUCoroutine *co = opaque;
670b8b8753SPaolo Bonzini  *         qemu_coroutine_enter(co);
681de7afc9SPaolo Bonzini  *     }
691de7afc9SPaolo Bonzini  *
701de7afc9SPaolo Bonzini  *     ...
710b8b8753SPaolo Bonzini  *     QEMUCoroutine *co = qemu_coroutine_create(coroutine_entry, NULL);
721de7afc9SPaolo Bonzini  *     QEMUBH *start_bh = qemu_bh_new(enter_co_bh, co);
731de7afc9SPaolo Bonzini  *     qemu_bh_schedule(start_bh);
741de7afc9SPaolo Bonzini  *     while (...) {
751de7afc9SPaolo Bonzini  *         main_loop_wait(false);
761de7afc9SPaolo Bonzini  *     }
771de7afc9SPaolo Bonzini  *
781de7afc9SPaolo Bonzini  * (In the future we may provide a wrapper for this).
791de7afc9SPaolo Bonzini  *
801de7afc9SPaolo Bonzini  * @nonblocking: Whether the caller should block until an event occurs.
811de7afc9SPaolo Bonzini  */
82de5f852fSPeter Maydell void main_loop_wait(int nonblocking);
831de7afc9SPaolo Bonzini 
841de7afc9SPaolo Bonzini /**
855f3aa1ffSStefan Hajnoczi  * qemu_get_aio_context: Return the main loop's AioContext
865f3aa1ffSStefan Hajnoczi  */
875f3aa1ffSStefan Hajnoczi AioContext *qemu_get_aio_context(void);
885f3aa1ffSStefan Hajnoczi 
895f3aa1ffSStefan Hajnoczi /**
901de7afc9SPaolo Bonzini  * qemu_notify_event: Force processing of pending events.
911de7afc9SPaolo Bonzini  *
921de7afc9SPaolo Bonzini  * Similar to signaling a condition variable, qemu_notify_event forces
931de7afc9SPaolo Bonzini  * main_loop_wait to look at pending events and exit.  The caller of
941de7afc9SPaolo Bonzini  * main_loop_wait will usually call it again very soon, so qemu_notify_event
951de7afc9SPaolo Bonzini  * also has the side effect of recalculating the sets of file descriptors
961de7afc9SPaolo Bonzini  * that the main loop waits for.
971de7afc9SPaolo Bonzini  *
981de7afc9SPaolo Bonzini  * Calling qemu_notify_event is rarely necessary, because main loop
996484e422SFam Zheng  * services (bottom halves and timers) call it themselves.
1001de7afc9SPaolo Bonzini  */
1011de7afc9SPaolo Bonzini void qemu_notify_event(void);
1021de7afc9SPaolo Bonzini 
1031de7afc9SPaolo Bonzini #ifdef _WIN32
1041de7afc9SPaolo Bonzini /* return TRUE if no sleep should be done afterwards */
1051de7afc9SPaolo Bonzini typedef int PollingFunc(void *opaque);
1061de7afc9SPaolo Bonzini 
1071de7afc9SPaolo Bonzini /**
1081de7afc9SPaolo Bonzini  * qemu_add_polling_cb: Register a Windows-specific polling callback
1091de7afc9SPaolo Bonzini  *
1101de7afc9SPaolo Bonzini  * Currently, under Windows some events are polled rather than waited for.
1111de7afc9SPaolo Bonzini  * Polling callbacks do not ensure that @func is called timely, because
1121de7afc9SPaolo Bonzini  * the main loop might wait for an arbitrarily long time.  If possible,
1131de7afc9SPaolo Bonzini  * you should instead create a separate thread that does a blocking poll
1141de7afc9SPaolo Bonzini  * and set a Win32 event object.  The event can then be passed to
1151de7afc9SPaolo Bonzini  * qemu_add_wait_object.
1161de7afc9SPaolo Bonzini  *
1171de7afc9SPaolo Bonzini  * Polling callbacks really have nothing Windows specific in them, but
1181de7afc9SPaolo Bonzini  * as they are a hack and are currently not necessary under POSIX systems,
1191de7afc9SPaolo Bonzini  * they are only available when QEMU is running under Windows.
1201de7afc9SPaolo Bonzini  *
1211de7afc9SPaolo Bonzini  * @func: The function that does the polling, and returns 1 to force
1221de7afc9SPaolo Bonzini  * immediate completion of main_loop_wait.
1231de7afc9SPaolo Bonzini  * @opaque: A pointer-size value that is passed to @func.
1241de7afc9SPaolo Bonzini  */
1251de7afc9SPaolo Bonzini int qemu_add_polling_cb(PollingFunc *func, void *opaque);
1261de7afc9SPaolo Bonzini 
1271de7afc9SPaolo Bonzini /**
1281de7afc9SPaolo Bonzini  * qemu_del_polling_cb: Unregister a Windows-specific polling callback
1291de7afc9SPaolo Bonzini  *
1301de7afc9SPaolo Bonzini  * This function removes a callback that was registered with
1311de7afc9SPaolo Bonzini  * qemu_add_polling_cb.
1321de7afc9SPaolo Bonzini  *
1331de7afc9SPaolo Bonzini  * @func: The function that was passed to qemu_add_polling_cb.
1341de7afc9SPaolo Bonzini  * @opaque: A pointer-size value that was passed to qemu_add_polling_cb.
1351de7afc9SPaolo Bonzini  */
1361de7afc9SPaolo Bonzini void qemu_del_polling_cb(PollingFunc *func, void *opaque);
1371de7afc9SPaolo Bonzini 
1381de7afc9SPaolo Bonzini /* Wait objects handling */
1391de7afc9SPaolo Bonzini typedef void WaitObjectFunc(void *opaque);
1401de7afc9SPaolo Bonzini 
1411de7afc9SPaolo Bonzini /**
1421de7afc9SPaolo Bonzini  * qemu_add_wait_object: Register a callback for a Windows handle
1431de7afc9SPaolo Bonzini  *
1441de7afc9SPaolo Bonzini  * Under Windows, the iohandler mechanism can only be used with sockets.
1451de7afc9SPaolo Bonzini  * QEMU must use the WaitForMultipleObjects API to wait on other handles.
1461de7afc9SPaolo Bonzini  * This function registers a #HANDLE with QEMU, so that it will be included
1471de7afc9SPaolo Bonzini  * in the main loop's calls to WaitForMultipleObjects.  When the handle
1481de7afc9SPaolo Bonzini  * is in a signaled state, QEMU will call @func.
1491de7afc9SPaolo Bonzini  *
1501de7afc9SPaolo Bonzini  * @handle: The Windows handle to be observed.
1511de7afc9SPaolo Bonzini  * @func: A function to be called when @handle is in a signaled state.
1521de7afc9SPaolo Bonzini  * @opaque: A pointer-size value that is passed to @func.
1531de7afc9SPaolo Bonzini  */
1541de7afc9SPaolo Bonzini int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
1551de7afc9SPaolo Bonzini 
1561de7afc9SPaolo Bonzini /**
1571de7afc9SPaolo Bonzini  * qemu_del_wait_object: Unregister a callback for a Windows handle
1581de7afc9SPaolo Bonzini  *
1591de7afc9SPaolo Bonzini  * This function removes a callback that was registered with
1601de7afc9SPaolo Bonzini  * qemu_add_wait_object.
1611de7afc9SPaolo Bonzini  *
1621de7afc9SPaolo Bonzini  * @func: The function that was passed to qemu_add_wait_object.
1631de7afc9SPaolo Bonzini  * @opaque: A pointer-size value that was passed to qemu_add_wait_object.
1641de7afc9SPaolo Bonzini  */
1651de7afc9SPaolo Bonzini void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
1661de7afc9SPaolo Bonzini #endif
1671de7afc9SPaolo Bonzini 
1681de7afc9SPaolo Bonzini /* async I/O support */
1691de7afc9SPaolo Bonzini 
1701de7afc9SPaolo Bonzini typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
171d29a8a1bSStefan Hajnoczi 
172d29a8a1bSStefan Hajnoczi /**
173d29a8a1bSStefan Hajnoczi  * IOCanReadHandler: Return the number of bytes that #IOReadHandler can accept
174d29a8a1bSStefan Hajnoczi  *
175d29a8a1bSStefan Hajnoczi  * This function reports how many bytes #IOReadHandler is prepared to accept.
176d29a8a1bSStefan Hajnoczi  * #IOReadHandler may be invoked with up to this number of bytes.  If this
177d29a8a1bSStefan Hajnoczi  * function returns 0 then #IOReadHandler is not invoked.
178d29a8a1bSStefan Hajnoczi  *
179d29a8a1bSStefan Hajnoczi  * This function is typically called from an event loop.  If the number of
180d29a8a1bSStefan Hajnoczi  * bytes changes outside the event loop (e.g. because a vcpu thread drained the
181d29a8a1bSStefan Hajnoczi  * buffer), then it is necessary to kick the event loop so that this function
182d29a8a1bSStefan Hajnoczi  * is called again.  aio_notify() or qemu_notify_event() can be used to kick
183d29a8a1bSStefan Hajnoczi  * the event loop.
184d29a8a1bSStefan Hajnoczi  */
1851de7afc9SPaolo Bonzini typedef int IOCanReadHandler(void *opaque);
1861de7afc9SPaolo Bonzini 
1871de7afc9SPaolo Bonzini /**
1881de7afc9SPaolo Bonzini  * qemu_set_fd_handler: Register a file descriptor with the main loop
1891de7afc9SPaolo Bonzini  *
1901de7afc9SPaolo Bonzini  * This function tells the main loop to wake up whenever one of the
1911de7afc9SPaolo Bonzini  * following conditions is true:
1921de7afc9SPaolo Bonzini  *
1931de7afc9SPaolo Bonzini  * 1) if @fd_write is not %NULL, when the file descriptor is writable;
1941de7afc9SPaolo Bonzini  *
1951de7afc9SPaolo Bonzini  * 2) if @fd_read is not %NULL, when the file descriptor is readable.
1961de7afc9SPaolo Bonzini  *
1971de7afc9SPaolo Bonzini  * The callbacks that are set up by qemu_set_fd_handler are level-triggered.
1981de7afc9SPaolo Bonzini  * If @fd_read does not read from @fd, or @fd_write does not write to @fd
1991de7afc9SPaolo Bonzini  * until its buffers are full, they will be called again on the next
2001de7afc9SPaolo Bonzini  * iteration.
2011de7afc9SPaolo Bonzini  *
2021de7afc9SPaolo Bonzini  * @fd: The file descriptor to be observed.  Under Windows it must be
2031de7afc9SPaolo Bonzini  * a #SOCKET.
2041de7afc9SPaolo Bonzini  *
2051de7afc9SPaolo Bonzini  * @fd_read: A level-triggered callback that is fired if @fd is readable
2061de7afc9SPaolo Bonzini  * at the beginning of a main loop iteration, or if it becomes readable
2071de7afc9SPaolo Bonzini  * during one.
2081de7afc9SPaolo Bonzini  *
2091de7afc9SPaolo Bonzini  * @fd_write: A level-triggered callback that is fired when @fd is writable
2101de7afc9SPaolo Bonzini  * at the beginning of a main loop iteration, or if it becomes writable
2111de7afc9SPaolo Bonzini  * during one.
2121de7afc9SPaolo Bonzini  *
2131de7afc9SPaolo Bonzini  * @opaque: A pointer-sized value that is passed to @fd_read and @fd_write.
2141de7afc9SPaolo Bonzini  */
215f4d248bdSFam Zheng void qemu_set_fd_handler(int fd,
2161de7afc9SPaolo Bonzini                          IOHandler *fd_read,
2171de7afc9SPaolo Bonzini                          IOHandler *fd_write,
2181de7afc9SPaolo Bonzini                          void *opaque);
2191de7afc9SPaolo Bonzini 
220d6da1e9eSPaolo Bonzini 
221d6da1e9eSPaolo Bonzini /**
222d6da1e9eSPaolo Bonzini  * event_notifier_set_handler: Register an EventNotifier with the main loop
223d6da1e9eSPaolo Bonzini  *
224d6da1e9eSPaolo Bonzini  * This function tells the main loop to wake up whenever the
225d6da1e9eSPaolo Bonzini  * #EventNotifier was set.
226d6da1e9eSPaolo Bonzini  *
227d6da1e9eSPaolo Bonzini  * @e: The #EventNotifier to be observed.
228d6da1e9eSPaolo Bonzini  *
229d6da1e9eSPaolo Bonzini  * @handler: A level-triggered callback that is fired when @e
230d6da1e9eSPaolo Bonzini  * has been set.  @e is passed to it as a parameter.
231d6da1e9eSPaolo Bonzini  */
232d6da1e9eSPaolo Bonzini void event_notifier_set_handler(EventNotifier *e,
233d6da1e9eSPaolo Bonzini                                 EventNotifierHandler *handler);
234d6da1e9eSPaolo Bonzini 
235f3926945SFam Zheng GSource *iohandler_get_g_source(void);
236bcd82a96SFam Zheng AioContext *iohandler_get_aio_context(void);
2371de7afc9SPaolo Bonzini 
2381de7afc9SPaolo Bonzini /**
239afbe7053SPaolo Bonzini  * qemu_mutex_iothread_locked: Return lock status of the main loop mutex.
240afbe7053SPaolo Bonzini  *
241afbe7053SPaolo Bonzini  * The main loop mutex is the coarsest lock in QEMU, and as such it
242afbe7053SPaolo Bonzini  * must always be taken outside other locks.  This function helps
243afbe7053SPaolo Bonzini  * functions take different paths depending on whether the current
244afbe7053SPaolo Bonzini  * thread is running within the main loop mutex.
2456538692eSEmanuele Giuseppe Esposito  *
2466538692eSEmanuele Giuseppe Esposito  * This function should never be used in the block layer, because
2476538692eSEmanuele Giuseppe Esposito  * unit tests, block layer tools and qemu-storage-daemon do not
2486538692eSEmanuele Giuseppe Esposito  * have a BQL.
2496538692eSEmanuele Giuseppe Esposito  * Please instead refer to qemu_in_main_thread().
250afbe7053SPaolo Bonzini  */
251afbe7053SPaolo Bonzini bool qemu_mutex_iothread_locked(void);
252afbe7053SPaolo Bonzini 
253afbe7053SPaolo Bonzini /**
2546538692eSEmanuele Giuseppe Esposito  * qemu_in_main_thread: return whether it's possible to safely access
2556538692eSEmanuele Giuseppe Esposito  * the global state of the block layer.
2566538692eSEmanuele Giuseppe Esposito  *
2576538692eSEmanuele Giuseppe Esposito  * Global state of the block layer is not accessible from I/O threads
2586538692eSEmanuele Giuseppe Esposito  * or worker threads; only from threads that "own" the default
2596538692eSEmanuele Giuseppe Esposito  * AioContext that qemu_get_aio_context() returns.  For tests, block
2606538692eSEmanuele Giuseppe Esposito  * layer tools and qemu-storage-daemon there is a designated thread that
2616538692eSEmanuele Giuseppe Esposito  * runs the event loop for qemu_get_aio_context(), and that is the
2626538692eSEmanuele Giuseppe Esposito  * main thread.
2636538692eSEmanuele Giuseppe Esposito  *
2646538692eSEmanuele Giuseppe Esposito  * For emulators, however, any thread that holds the BQL can act
2656538692eSEmanuele Giuseppe Esposito  * as the block layer main thread; this will be any of the actual
2666538692eSEmanuele Giuseppe Esposito  * main thread, the vCPU threads or the RCU thread.
2676538692eSEmanuele Giuseppe Esposito  *
2686538692eSEmanuele Giuseppe Esposito  * For clarity, do not use this function outside the block layer.
2696538692eSEmanuele Giuseppe Esposito  */
2706538692eSEmanuele Giuseppe Esposito bool qemu_in_main_thread(void);
2716538692eSEmanuele Giuseppe Esposito 
272ac7798f2SEmanuele Giuseppe Esposito /* Mark and check that the function is part of the global state API. */
27347281859SPhilippe Mathieu-Daudé #ifdef CONFIG_COCOA
27447281859SPhilippe Mathieu-Daudé /*
27547281859SPhilippe Mathieu-Daudé  * When using the Cocoa UI, addRemovableDevicesMenuItems() is called from
27647281859SPhilippe Mathieu-Daudé  * a thread different from the QEMU main thread and can not take the BQL,
27747281859SPhilippe Mathieu-Daudé  * triggering this assertions in the block layer (commit 0439c5a462).
27847281859SPhilippe Mathieu-Daudé  * As the Cocoa fix is not trivial, disable this assertion for the v7.0.0
27947281859SPhilippe Mathieu-Daudé  * release (when using Cocoa); we will restore it immediately after the
28047281859SPhilippe Mathieu-Daudé  * release.
28147281859SPhilippe Mathieu-Daudé  * This issue is tracked as https://gitlab.com/qemu-project/qemu/-/issues/926
28247281859SPhilippe Mathieu-Daudé  */
28347281859SPhilippe Mathieu-Daudé #define GLOBAL_STATE_CODE()
28447281859SPhilippe Mathieu-Daudé #else
285ac7798f2SEmanuele Giuseppe Esposito #define GLOBAL_STATE_CODE()                                         \
286ac7798f2SEmanuele Giuseppe Esposito     do {                                                            \
287ccfaf783SHanna Reitz         assert(qemu_in_main_thread());                              \
288ac7798f2SEmanuele Giuseppe Esposito     } while (0)
28947281859SPhilippe Mathieu-Daudé #endif /* CONFIG_COCOA */
290ac7798f2SEmanuele Giuseppe Esposito 
291ac7798f2SEmanuele Giuseppe Esposito /* Mark and check that the function is part of the I/O API. */
292ac7798f2SEmanuele Giuseppe Esposito #define IO_CODE()                                                   \
293ac7798f2SEmanuele Giuseppe Esposito     do {                                                            \
294ac7798f2SEmanuele Giuseppe Esposito         /* nop */                                                   \
295ac7798f2SEmanuele Giuseppe Esposito     } while (0)
296ac7798f2SEmanuele Giuseppe Esposito 
297ac7798f2SEmanuele Giuseppe Esposito /* Mark and check that the function is part of the "I/O OR GS" API. */
298ac7798f2SEmanuele Giuseppe Esposito #define IO_OR_GS_CODE()                                             \
299ac7798f2SEmanuele Giuseppe Esposito     do {                                                            \
300ac7798f2SEmanuele Giuseppe Esposito         /* nop */                                                   \
301ac7798f2SEmanuele Giuseppe Esposito     } while (0)
302ac7798f2SEmanuele Giuseppe Esposito 
3036538692eSEmanuele Giuseppe Esposito /**
3041de7afc9SPaolo Bonzini  * qemu_mutex_lock_iothread: Lock the main loop mutex.
3051de7afc9SPaolo Bonzini  *
3061de7afc9SPaolo Bonzini  * This function locks the main loop mutex.  The mutex is taken by
30711717bc9SYaowei Bai  * main() in vl.c and always taken except while waiting on
3081de7afc9SPaolo Bonzini  * external events (such as with select).  The mutex should be taken
3091de7afc9SPaolo Bonzini  * by threads other than the main loop thread when calling
3101de7afc9SPaolo Bonzini  * qemu_bh_new(), qemu_set_fd_handler() and basically all other
3111de7afc9SPaolo Bonzini  * functions documented in this file.
3121de7afc9SPaolo Bonzini  *
3131de7afc9SPaolo Bonzini  * NOTE: tools currently are single-threaded and qemu_mutex_lock_iothread
3141de7afc9SPaolo Bonzini  * is a no-op there.
3151de7afc9SPaolo Bonzini  */
316cb764d06SEmilio G. Cota #define qemu_mutex_lock_iothread()                      \
317cb764d06SEmilio G. Cota     qemu_mutex_lock_iothread_impl(__FILE__, __LINE__)
318cb764d06SEmilio G. Cota void qemu_mutex_lock_iothread_impl(const char *file, int line);
3191de7afc9SPaolo Bonzini 
3201de7afc9SPaolo Bonzini /**
3211de7afc9SPaolo Bonzini  * qemu_mutex_unlock_iothread: Unlock the main loop mutex.
3221de7afc9SPaolo Bonzini  *
3231de7afc9SPaolo Bonzini  * This function unlocks the main loop mutex.  The mutex is taken by
32411717bc9SYaowei Bai  * main() in vl.c and always taken except while waiting on
3251de7afc9SPaolo Bonzini  * external events (such as with select).  The mutex should be unlocked
3261de7afc9SPaolo Bonzini  * as soon as possible by threads other than the main loop thread,
3271de7afc9SPaolo Bonzini  * because it prevents the main loop from processing callbacks,
3281de7afc9SPaolo Bonzini  * including timers and bottom halves.
3291de7afc9SPaolo Bonzini  *
3301de7afc9SPaolo Bonzini  * NOTE: tools currently are single-threaded and qemu_mutex_unlock_iothread
3311de7afc9SPaolo Bonzini  * is a no-op there.
3321de7afc9SPaolo Bonzini  */
3331de7afc9SPaolo Bonzini void qemu_mutex_unlock_iothread(void);
3341de7afc9SPaolo Bonzini 
33519e067e0SAravinda Prasad /*
33619e067e0SAravinda Prasad  * qemu_cond_wait_iothread: Wait on condition for the main loop mutex
33719e067e0SAravinda Prasad  *
33819e067e0SAravinda Prasad  * This function atomically releases the main loop mutex and causes
33919e067e0SAravinda Prasad  * the calling thread to block on the condition.
34019e067e0SAravinda Prasad  */
34119e067e0SAravinda Prasad void qemu_cond_wait_iothread(QemuCond *cond);
34219e067e0SAravinda Prasad 
343b0c3cf94SClaudio Fontana /*
344b0c3cf94SClaudio Fontana  * qemu_cond_timedwait_iothread: like the previous, but with timeout
345b0c3cf94SClaudio Fontana  */
346b0c3cf94SClaudio Fontana void qemu_cond_timedwait_iothread(QemuCond *cond, int ms);
347b0c3cf94SClaudio Fontana 
3481de7afc9SPaolo Bonzini /* internal interfaces */
3491de7afc9SPaolo Bonzini 
3501de7afc9SPaolo Bonzini void qemu_fd_register(int fd);
3511de7afc9SPaolo Bonzini 
3520f08586cSStefan Hajnoczi #define qemu_bh_new(cb, opaque) \
3530f08586cSStefan Hajnoczi     qemu_bh_new_full((cb), (opaque), (stringify(cb)))
3540f08586cSStefan Hajnoczi QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name);
3551de7afc9SPaolo Bonzini void qemu_bh_schedule_idle(QEMUBH *bh);
3561de7afc9SPaolo Bonzini 
3571ab67b98SMarc-André Lureau enum {
3581ab67b98SMarc-André Lureau     MAIN_LOOP_POLL_FILL,
3591ab67b98SMarc-André Lureau     MAIN_LOOP_POLL_ERR,
3601ab67b98SMarc-André Lureau     MAIN_LOOP_POLL_OK,
3611ab67b98SMarc-André Lureau };
3621ab67b98SMarc-André Lureau 
3631ab67b98SMarc-André Lureau typedef struct MainLoopPoll {
3641ab67b98SMarc-André Lureau     int state;
3651ab67b98SMarc-André Lureau     uint32_t timeout;
3661ab67b98SMarc-André Lureau     GArray *pollfds;
3671ab67b98SMarc-André Lureau } MainLoopPoll;
3681ab67b98SMarc-André Lureau 
3691ab67b98SMarc-André Lureau void main_loop_poll_add_notifier(Notifier *notify);
3701ab67b98SMarc-André Lureau void main_loop_poll_remove_notifier(Notifier *notify);
3711ab67b98SMarc-André Lureau 
3721de7afc9SPaolo Bonzini #endif
373