Commit d3385eb4 authored by Paolo Bonzini's avatar Paolo Bonzini Committed by Blue Swirl

main-loop: interrupt wait when data arrives on a socket

Right now, the main loop is not interrupted when data arrives on a
socket.  To fix this, register each socket to interrupt the main loop
with WSAEventSelect.  This does not replace select, it only communicates
a change in socket state that requires a select call.

Since the interrupt fires only once per recv call, or only once
after a send call returns EWOULDBLOCK we can activate it on all events
unconditionally.  If QEMU is momentarily uninterested on some condition,
the main loop will not busy wait.  Instead, it may get one extra wakeup,
but then it will ignore the condition until progress occurs and/or
qemu_set_fd_handler is called to set a callback.  At this point the
condition will be tested via select and the callback will be invoked
even if it is still disabled on the event.
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
Signed-off-by: default avatarBlue Swirl <blauwirbel@gmail.com>
parent 15455536
...@@ -392,10 +392,18 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) ...@@ -392,10 +392,18 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
} }
} }
void qemu_fd_register(int fd)
{
WSAEventSelect(fd, qemu_event_handle, FD_READ | FD_ACCEPT | FD_CLOSE |
FD_CONNECT | FD_WRITE | FD_OOB);
}
static int os_host_main_loop_wait(int timeout) static int os_host_main_loop_wait(int timeout)
{ {
int ret, ret2, i; int ret, ret2, i;
PollingEntry *pe; PollingEntry *pe;
int err;
WaitObjects *w = &wait_objects;
static struct timeval tv0; static struct timeval tv0;
/* XXX: need to suppress polling by better using win32 events */ /* XXX: need to suppress polling by better using win32 events */
...@@ -403,38 +411,49 @@ static int os_host_main_loop_wait(int timeout) ...@@ -403,38 +411,49 @@ static int os_host_main_loop_wait(int timeout)
for (pe = first_polling_entry; pe != NULL; pe = pe->next) { for (pe = first_polling_entry; pe != NULL; pe = pe->next) {
ret |= pe->func(pe->opaque); ret |= pe->func(pe->opaque);
} }
if (ret == 0) { if (ret != 0) {
int err; return ret;
WaitObjects *w = &wait_objects; }
qemu_mutex_unlock_iothread(); if (nfds >= 0) {
ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout); ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv0);
qemu_mutex_lock_iothread(); if (ret != 0) {
if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { timeout = 0;
if (w->func[ret - WAIT_OBJECT_0]) { }
w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); }
}
/* Check for additional signaled events */ qemu_mutex_unlock_iothread();
for (i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout);
/* Check if event is signaled */ qemu_mutex_lock_iothread();
ret2 = WaitForSingleObject(w->events[i], 0); if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
if (ret2 == WAIT_OBJECT_0) { if (w->func[ret - WAIT_OBJECT_0]) {
if (w->func[i]) { w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
w->func[i](w->opaque[i]); }
}
} else if (ret2 != WAIT_TIMEOUT) { /* Check for additional signaled events */
err = GetLastError(); for (i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err); /* Check if event is signaled */
ret2 = WaitForSingleObject(w->events[i], 0);
if (ret2 == WAIT_OBJECT_0) {
if (w->func[i]) {
w->func[i](w->opaque[i]);
} }
} else if (ret2 != WAIT_TIMEOUT) {
err = GetLastError();
fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
} }
} else if (ret != WAIT_TIMEOUT) {
err = GetLastError();
fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
} }
} else if (ret != WAIT_TIMEOUT) {
err = GetLastError();
fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
} }
ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv0);
/* If an edge-triggered socket event occurred, select will return a
* positive result on the next iteration. We do not need to do anything
* here.
*/
return ret; return ret;
} }
#endif #endif
......
...@@ -359,6 +359,7 @@ void qemu_mutex_unlock_iothread(void); ...@@ -359,6 +359,7 @@ void qemu_mutex_unlock_iothread(void);
/* internal interfaces */ /* internal interfaces */
void qemu_fd_register(int fd);
void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds); void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds);
void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int rc); void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int rc);
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <windows.h> #include <windows.h>
#include "config-host.h" #include "config-host.h"
#include "sysemu.h" #include "sysemu.h"
#include "main-loop.h"
#include "trace.h" #include "trace.h"
#include "qemu_socket.h" #include "qemu_socket.h"
...@@ -76,6 +77,7 @@ void qemu_vfree(void *ptr) ...@@ -76,6 +77,7 @@ void qemu_vfree(void *ptr)
void socket_set_block(int fd) void socket_set_block(int fd)
{ {
unsigned long opt = 0; unsigned long opt = 0;
WSAEventSelect(fd, NULL, 0);
ioctlsocket(fd, FIONBIO, &opt); ioctlsocket(fd, FIONBIO, &opt);
} }
...@@ -83,6 +85,7 @@ void socket_set_nonblock(int fd) ...@@ -83,6 +85,7 @@ void socket_set_nonblock(int fd)
{ {
unsigned long opt = 1; unsigned long opt = 1;
ioctlsocket(fd, FIONBIO, &opt); ioctlsocket(fd, FIONBIO, &opt);
qemu_fd_register(fd);
} }
int inet_aton(const char *cp, struct in_addr *ia) int inet_aton(const char *cp, struct in_addr *ia)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment