main-loop.h 13 KB

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