4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu-common.h"
26 #include "qemu-timer.h"
27 #include "slirp/slirp.h"
28 #include "main-loop.h"
35 static int io_thread_fd
= -1;
37 void qemu_notify_event(void)
39 /* Write 8 bytes to be compatible with eventfd. */
40 static const uint64_t val
= 1;
43 if (io_thread_fd
== -1) {
47 ret
= write(io_thread_fd
, &val
, sizeof(val
));
48 } while (ret
< 0 && errno
== EINTR
);
50 /* EAGAIN is fine, a read must be pending. */
51 if (ret
< 0 && errno
!= EAGAIN
) {
52 fprintf(stderr
, "qemu_notify_event: write() failed: %s\n",
58 static void qemu_event_read(void *opaque
)
60 int fd
= (intptr_t)opaque
;
64 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
66 len
= read(fd
, buffer
, sizeof(buffer
));
67 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
70 static int qemu_event_init(void)
75 err
= qemu_eventfd(fds
);
79 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
83 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
87 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
88 (void *)(intptr_t)fds
[0]);
90 io_thread_fd
= fds
[1];
99 /* If we have signalfd, we mask out the signals we want to handle and then
100 * use signalfd to listen for them. We rely on whatever the current signal
101 * handler is to dispatch the signals when we receive them.
103 static void sigfd_handler(void *opaque
)
105 int fd
= (intptr_t)opaque
;
106 struct qemu_signalfd_siginfo info
;
107 struct sigaction action
;
112 len
= read(fd
, &info
, sizeof(info
));
113 } while (len
== -1 && errno
== EINTR
);
115 if (len
== -1 && errno
== EAGAIN
) {
119 if (len
!= sizeof(info
)) {
120 printf("read from sigfd returned %zd: %m\n", len
);
124 sigaction(info
.ssi_signo
, NULL
, &action
);
125 if ((action
.sa_flags
& SA_SIGINFO
) && action
.sa_sigaction
) {
126 action
.sa_sigaction(info
.ssi_signo
,
127 (siginfo_t
*)&info
, NULL
);
128 } else if (action
.sa_handler
) {
129 action
.sa_handler(info
.ssi_signo
);
134 static int qemu_signal_init(void)
140 * SIG_IPI must be blocked in the main thread and must not be caught
141 * by sigwait() in the signal thread. Otherwise, the cpu thread will
142 * not catch it reliably.
145 sigaddset(&set
, SIG_IPI
);
146 sigaddset(&set
, SIGIO
);
147 sigaddset(&set
, SIGALRM
);
148 sigaddset(&set
, SIGBUS
);
149 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
151 sigdelset(&set
, SIG_IPI
);
152 sigfd
= qemu_signalfd(&set
);
154 fprintf(stderr
, "failed to create signalfd\n");
158 fcntl_setfl(sigfd
, O_NONBLOCK
);
160 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
161 (void *)(intptr_t)sigfd
);
168 static HANDLE qemu_event_handle
= NULL
;
170 static void dummy_event_handler(void *opaque
)
174 static int qemu_event_init(void)
176 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
177 if (!qemu_event_handle
) {
178 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
181 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
185 void qemu_notify_event(void)
187 if (!qemu_event_handle
) {
190 if (!SetEvent(qemu_event_handle
)) {
191 fprintf(stderr
, "qemu_notify_event: SetEvent failed: %ld\n",
197 static int qemu_signal_init(void)
203 static AioContext
*qemu_aio_context
;
205 int qemu_init_main_loop(void)
213 qemu_mutex_lock_iothread();
214 ret
= qemu_signal_init();
219 /* Note eventfd must be drained before signalfd handlers run */
220 ret
= qemu_event_init();
225 qemu_aio_context
= aio_context_new();
226 src
= aio_get_g_source(qemu_aio_context
);
227 g_source_attach(src
, NULL
);
232 static fd_set rfds
, wfds
, xfds
;
234 static GPollFD poll_fds
[1024 * 2]; /* this is probably overkill */
235 static int n_poll_fds
;
236 static int max_priority
;
239 static void glib_select_fill(int *max_fd
, fd_set
*rfds
, fd_set
*wfds
,
240 fd_set
*xfds
, uint32_t *cur_timeout
)
242 GMainContext
*context
= g_main_context_default();
246 g_main_context_prepare(context
, &max_priority
);
248 n_poll_fds
= g_main_context_query(context
, max_priority
, &timeout
,
249 poll_fds
, ARRAY_SIZE(poll_fds
));
250 g_assert(n_poll_fds
<= ARRAY_SIZE(poll_fds
));
252 for (i
= 0; i
< n_poll_fds
; i
++) {
253 GPollFD
*p
= &poll_fds
[i
];
255 if ((p
->events
& G_IO_IN
)) {
257 *max_fd
= MAX(*max_fd
, p
->fd
);
259 if ((p
->events
& G_IO_OUT
)) {
261 *max_fd
= MAX(*max_fd
, p
->fd
);
263 if ((p
->events
& G_IO_ERR
)) {
265 *max_fd
= MAX(*max_fd
, p
->fd
);
269 if (timeout
>= 0 && timeout
< *cur_timeout
) {
270 *cur_timeout
= timeout
;
274 static void glib_select_poll(fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
277 GMainContext
*context
= g_main_context_default();
282 for (i
= 0; i
< n_poll_fds
; i
++) {
283 GPollFD
*p
= &poll_fds
[i
];
285 if ((p
->events
& G_IO_IN
) && FD_ISSET(p
->fd
, rfds
)) {
286 p
->revents
|= G_IO_IN
;
288 if ((p
->events
& G_IO_OUT
) && FD_ISSET(p
->fd
, wfds
)) {
289 p
->revents
|= G_IO_OUT
;
291 if ((p
->events
& G_IO_ERR
) && FD_ISSET(p
->fd
, xfds
)) {
292 p
->revents
|= G_IO_ERR
;
297 if (g_main_context_check(context
, max_priority
, poll_fds
, n_poll_fds
)) {
298 g_main_context_dispatch(context
);
302 static int os_host_main_loop_wait(uint32_t timeout
)
304 struct timeval tv
, *tvarg
= NULL
;
307 glib_select_fill(&nfds
, &rfds
, &wfds
, &xfds
, &timeout
);
309 if (timeout
< UINT32_MAX
) {
311 tv
.tv_sec
= timeout
/ 1000;
312 tv
.tv_usec
= (timeout
% 1000) * 1000;
316 qemu_mutex_unlock_iothread();
319 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, tvarg
);
322 qemu_mutex_lock_iothread();
325 glib_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
329 /***********************************************************/
330 /* Polling handling */
332 typedef struct PollingEntry
{
335 struct PollingEntry
*next
;
338 static PollingEntry
*first_polling_entry
;
340 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
342 PollingEntry
**ppe
, *pe
;
343 pe
= g_malloc0(sizeof(PollingEntry
));
346 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
351 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
353 PollingEntry
**ppe
, *pe
;
354 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
356 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
364 /***********************************************************/
365 /* Wait objects support */
366 typedef struct WaitObjects
{
368 int revents
[MAXIMUM_WAIT_OBJECTS
+ 1];
369 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
370 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
371 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
374 static WaitObjects wait_objects
= {0};
376 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
378 WaitObjects
*w
= &wait_objects
;
379 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
) {
382 w
->events
[w
->num
] = handle
;
383 w
->func
[w
->num
] = func
;
384 w
->opaque
[w
->num
] = opaque
;
385 w
->revents
[w
->num
] = 0;
390 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
393 WaitObjects
*w
= &wait_objects
;
396 for (i
= 0; i
< w
->num
; i
++) {
397 if (w
->events
[i
] == handle
) {
401 w
->events
[i
] = w
->events
[i
+ 1];
402 w
->func
[i
] = w
->func
[i
+ 1];
403 w
->opaque
[i
] = w
->opaque
[i
+ 1];
404 w
->revents
[i
] = w
->revents
[i
+ 1];
412 void qemu_fd_register(int fd
)
414 WSAEventSelect(fd
, qemu_event_handle
, FD_READ
| FD_ACCEPT
| FD_CLOSE
|
415 FD_CONNECT
| FD_WRITE
| FD_OOB
);
418 static int os_host_main_loop_wait(uint32_t timeout
)
420 GMainContext
*context
= g_main_context_default();
423 WaitObjects
*w
= &wait_objects
;
425 static struct timeval tv0
;
427 /* XXX: need to suppress polling by better using win32 events */
429 for (pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
430 ret
|= pe
->func(pe
->opaque
);
437 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv0
);
443 g_main_context_prepare(context
, &max_priority
);
444 n_poll_fds
= g_main_context_query(context
, max_priority
, &poll_timeout
,
445 poll_fds
, ARRAY_SIZE(poll_fds
));
446 g_assert(n_poll_fds
<= ARRAY_SIZE(poll_fds
));
448 for (i
= 0; i
< w
->num
; i
++) {
449 poll_fds
[n_poll_fds
+ i
].fd
= (DWORD_PTR
)w
->events
[i
];
450 poll_fds
[n_poll_fds
+ i
].events
= G_IO_IN
;
453 if (poll_timeout
< 0 || timeout
< poll_timeout
) {
454 poll_timeout
= timeout
;
457 qemu_mutex_unlock_iothread();
458 ret
= g_poll(poll_fds
, n_poll_fds
+ w
->num
, poll_timeout
);
459 qemu_mutex_lock_iothread();
461 for (i
= 0; i
< w
->num
; i
++) {
462 w
->revents
[i
] = poll_fds
[n_poll_fds
+ i
].revents
;
464 for (i
= 0; i
< w
->num
; i
++) {
465 if (w
->revents
[i
] && w
->func
[i
]) {
466 w
->func
[i
](w
->opaque
[i
]);
471 if (g_main_context_check(context
, max_priority
, poll_fds
, n_poll_fds
)) {
472 g_main_context_dispatch(context
);
475 /* If an edge-triggered socket event occurred, select will return a
476 * positive result on the next iteration. We do not need to do anything
484 int main_loop_wait(int nonblocking
)
487 uint32_t timeout
= UINT32_MAX
;
493 /* poll any events */
494 /* XXX: separate device handlers from system ones */
501 slirp_update_timeout(&timeout
);
502 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
504 qemu_iohandler_fill(&nfds
, &rfds
, &wfds
, &xfds
);
505 ret
= os_host_main_loop_wait(timeout
);
506 qemu_iohandler_poll(&rfds
, &wfds
, &xfds
, ret
);
508 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
511 qemu_run_all_timers();
516 /* Functions to operate on the main QEMU AioContext. */
518 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
520 return aio_bh_new(qemu_aio_context
, cb
, opaque
);
523 void qemu_aio_flush(void)
525 aio_flush(qemu_aio_context
);
528 bool qemu_aio_wait(void)
530 return aio_poll(qemu_aio_context
, true);
534 void qemu_aio_set_fd_handler(int fd
,
537 AioFlushHandler
*io_flush
,
540 aio_set_fd_handler(qemu_aio_context
, fd
, io_read
, io_write
, io_flush
,
545 void qemu_aio_set_event_notifier(EventNotifier
*notifier
,
546 EventNotifierHandler
*io_read
,
547 AioFlushEventNotifierHandler
*io_flush
)
549 aio_set_event_notifier(qemu_aio_context
, notifier
, io_read
, io_flush
);