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 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 */ 45int qemu_init_main_loop(Error **errp); 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); 68 * } 69 * 70 * ... 71 * QEMUCoroutine *co = qemu_coroutine_create(coroutine_entry, NULL); 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 */ 82void main_loop_wait(int nonblocking); 83 84/** 85 * qemu_get_aio_context: Return the main loop's AioContext 86 */ 87AioContext *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. 100 */ 101void qemu_notify_event(void); 102 103#ifdef _WIN32 104/* return TRUE if no sleep should be done afterwards */ 105typedef int PollingFunc(void *opaque); 106 107/** 108 * qemu_add_polling_cb: Register a Windows-specific polling callback 109 * 110 * Currently, under Windows some events are polled rather than waited for. 111 * Polling callbacks do not ensure that @func is called timely, because 112 * the main loop might wait for an arbitrarily long time. If possible, 113 * you should instead create a separate thread that does a blocking poll 114 * and set a Win32 event object. The event can then be passed to 115 * qemu_add_wait_object. 116 * 117 * Polling callbacks really have nothing Windows specific in them, but 118 * as they are a hack and are currently not necessary under POSIX systems, 119 * they are only available when QEMU is running under Windows. 120 * 121 * @func: The function that does the polling, and returns 1 to force 122 * immediate completion of main_loop_wait. 123 * @opaque: A pointer-size value that is passed to @func. 124 */ 125int qemu_add_polling_cb(PollingFunc *func, void *opaque); 126 127/** 128 * qemu_del_polling_cb: Unregister a Windows-specific polling callback 129 * 130 * This function removes a callback that was registered with 131 * qemu_add_polling_cb. 132 * 133 * @func: The function that was passed to qemu_add_polling_cb. 134 * @opaque: A pointer-size value that was passed to qemu_add_polling_cb. 135 */ 136void qemu_del_polling_cb(PollingFunc *func, void *opaque); 137 138/* Wait objects handling */ 139typedef void WaitObjectFunc(void *opaque); 140 141/** 142 * qemu_add_wait_object: Register a callback for a Windows handle 143 * 144 * Under Windows, the iohandler mechanism can only be used with sockets. 145 * QEMU must use the WaitForMultipleObjects API to wait on other handles. 146 * This function registers a #HANDLE with QEMU, so that it will be included 147 * in the main loop's calls to WaitForMultipleObjects. When the handle 148 * is in a signaled state, QEMU will call @func. 149 * 150 * @handle: The Windows handle to be observed. 151 * @func: A function to be called when @handle is in a signaled state. 152 * @opaque: A pointer-size value that is passed to @func. 153 */ 154int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque); 155 156/** 157 * qemu_del_wait_object: Unregister a callback for a Windows handle 158 * 159 * This function removes a callback that was registered with 160 * qemu_add_wait_object. 161 * 162 * @func: The function that was passed to qemu_add_wait_object. 163 * @opaque: A pointer-size value that was passed to qemu_add_wait_object. 164 */ 165void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque); 166#endif 167 168/* async I/O support */ 169 170typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size); 171 172/** 173 * IOCanReadHandler: Return the number of bytes that #IOReadHandler can accept 174 * 175 * This function reports how many bytes #IOReadHandler is prepared to accept. 176 * #IOReadHandler may be invoked with up to this number of bytes. If this 177 * function returns 0 then #IOReadHandler is not invoked. 178 * 179 * This function is typically called from an event loop. If the number of 180 * bytes changes outside the event loop (e.g. because a vcpu thread drained the 181 * buffer), then it is necessary to kick the event loop so that this function 182 * is called again. aio_notify() or qemu_notify_event() can be used to kick 183 * the event loop. 184 */ 185typedef int IOCanReadHandler(void *opaque); 186 187/** 188 * qemu_set_fd_handler: Register a file descriptor with the main loop 189 * 190 * This function tells the main loop to wake up whenever one of the 191 * following conditions is true: 192 * 193 * 1) if @fd_write is not %NULL, when the file descriptor is writable; 194 * 195 * 2) if @fd_read is not %NULL, when the file descriptor is readable. 196 * 197 * The callbacks that are set up by qemu_set_fd_handler are level-triggered. 198 * If @fd_read does not read from @fd, or @fd_write does not write to @fd 199 * until its buffers are full, they will be called again on the next 200 * iteration. 201 * 202 * @fd: The file descriptor to be observed. Under Windows it must be 203 * a #SOCKET. 204 * 205 * @fd_read: A level-triggered callback that is fired if @fd is readable 206 * at the beginning of a main loop iteration, or if it becomes readable 207 * during one. 208 * 209 * @fd_write: A level-triggered callback that is fired when @fd is writable 210 * at the beginning of a main loop iteration, or if it becomes writable 211 * during one. 212 * 213 * @opaque: A pointer-sized value that is passed to @fd_read and @fd_write. 214 */ 215void qemu_set_fd_handler(int fd, 216 IOHandler *fd_read, 217 IOHandler *fd_write, 218 void *opaque); 219 220 221/** 222 * event_notifier_set_handler: Register an EventNotifier with the main loop 223 * 224 * This function tells the main loop to wake up whenever the 225 * #EventNotifier was set. 226 * 227 * @e: The #EventNotifier to be observed. 228 * 229 * @handler: A level-triggered callback that is fired when @e 230 * has been set. @e is passed to it as a parameter. 231 */ 232void event_notifier_set_handler(EventNotifier *e, 233 EventNotifierHandler *handler); 234 235GSource *iohandler_get_g_source(void); 236AioContext *iohandler_get_aio_context(void); 237 238/** 239 * qemu_mutex_iothread_locked: Return lock status of the main loop mutex. 240 * 241 * The main loop mutex is the coarsest lock in QEMU, and as such it 242 * must always be taken outside other locks. This function helps 243 * functions take different paths depending on whether the current 244 * thread is running within the main loop mutex. 245 * 246 * This function should never be used in the block layer, because 247 * unit tests, block layer tools and qemu-storage-daemon do not 248 * have a BQL. 249 * Please instead refer to qemu_in_main_thread(). 250 */ 251bool qemu_mutex_iothread_locked(void); 252 253/** 254 * qemu_in_main_thread: return whether it's possible to safely access 255 * the global state of the block layer. 256 * 257 * Global state of the block layer is not accessible from I/O threads 258 * or worker threads; only from threads that "own" the default 259 * AioContext that qemu_get_aio_context() returns. For tests, block 260 * layer tools and qemu-storage-daemon there is a designated thread that 261 * runs the event loop for qemu_get_aio_context(), and that is the 262 * main thread. 263 * 264 * For emulators, however, any thread that holds the BQL can act 265 * as the block layer main thread; this will be any of the actual 266 * main thread, the vCPU threads or the RCU thread. 267 * 268 * For clarity, do not use this function outside the block layer. 269 */ 270bool qemu_in_main_thread(void); 271 272/* Mark and check that the function is part of the global state API. */ 273#ifdef CONFIG_COCOA 274/* 275 * When using the Cocoa UI, addRemovableDevicesMenuItems() is called from 276 * a thread different from the QEMU main thread and can not take the BQL, 277 * triggering this assertions in the block layer (commit 0439c5a462). 278 * As the Cocoa fix is not trivial, disable this assertion for the v7.0.0 279 * release (when using Cocoa); we will restore it immediately after the 280 * release. 281 * This issue is tracked as https://gitlab.com/qemu-project/qemu/-/issues/926 282 */ 283#define GLOBAL_STATE_CODE() 284#else 285#define GLOBAL_STATE_CODE() \ 286 do { \ 287 /* FIXME: Re-enable after 7.0 release */ \ 288 /* assert(qemu_in_main_thread()); */ \ 289 } while (0) 290#endif /* CONFIG_COCOA */ 291 292/* Mark and check that the function is part of the I/O API. */ 293#define IO_CODE() \ 294 do { \ 295 /* nop */ \ 296 } while (0) 297 298/* Mark and check that the function is part of the "I/O OR GS" API. */ 299#define IO_OR_GS_CODE() \ 300 do { \ 301 /* nop */ \ 302 } while (0) 303 304/** 305 * qemu_mutex_lock_iothread: Lock the main loop mutex. 306 * 307 * This function locks the main loop mutex. The mutex is taken by 308 * main() in vl.c and always taken except while waiting on 309 * external events (such as with select). The mutex should be taken 310 * by threads other than the main loop thread when calling 311 * qemu_bh_new(), qemu_set_fd_handler() and basically all other 312 * functions documented in this file. 313 * 314 * NOTE: tools currently are single-threaded and qemu_mutex_lock_iothread 315 * is a no-op there. 316 */ 317#define qemu_mutex_lock_iothread() \ 318 qemu_mutex_lock_iothread_impl(__FILE__, __LINE__) 319void qemu_mutex_lock_iothread_impl(const char *file, int line); 320 321/** 322 * qemu_mutex_unlock_iothread: Unlock the main loop mutex. 323 * 324 * This function unlocks the main loop mutex. The mutex is taken by 325 * main() in vl.c and always taken except while waiting on 326 * external events (such as with select). The mutex should be unlocked 327 * as soon as possible by threads other than the main loop thread, 328 * because it prevents the main loop from processing callbacks, 329 * including timers and bottom halves. 330 * 331 * NOTE: tools currently are single-threaded and qemu_mutex_unlock_iothread 332 * is a no-op there. 333 */ 334void qemu_mutex_unlock_iothread(void); 335 336/* 337 * qemu_cond_wait_iothread: Wait on condition for the main loop mutex 338 * 339 * This function atomically releases the main loop mutex and causes 340 * the calling thread to block on the condition. 341 */ 342void qemu_cond_wait_iothread(QemuCond *cond); 343 344/* 345 * qemu_cond_timedwait_iothread: like the previous, but with timeout 346 */ 347void qemu_cond_timedwait_iothread(QemuCond *cond, int ms); 348 349/* internal interfaces */ 350 351void qemu_fd_register(int fd); 352 353#define qemu_bh_new(cb, opaque) \ 354 qemu_bh_new_full((cb), (opaque), (stringify(cb))) 355QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name); 356void qemu_bh_schedule_idle(QEMUBH *bh); 357 358enum { 359 MAIN_LOOP_POLL_FILL, 360 MAIN_LOOP_POLL_ERR, 361 MAIN_LOOP_POLL_OK, 362}; 363 364typedef struct MainLoopPoll { 365 int state; 366 uint32_t timeout; 367 GArray *pollfds; 368} MainLoopPoll; 369 370void main_loop_poll_add_notifier(Notifier *notify); 371void main_loop_poll_remove_notifier(Notifier *notify); 372 373#endif 374