main-loop.h 14 KB

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