Patchwork [03/17] event_notifier: add Win32 implementation

login
register
mail settings
Submitter Paolo Bonzini
Date Sept. 25, 2012, 12:55 p.m.
Message ID <1348577763-12920-4-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/186804/
State New
Headers show

Comments

Paolo Bonzini - Sept. 25, 2012, 12:55 p.m.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 Makefile.objs                              |  4 +-
 event_notifier.c => event_notifier-posix.c |  0
 event_notifier.c => event_notifier-win32.c | 97 ++++++------------------------
 event_notifier.h                           | 17 +++++-
 qemu-os-win32.h                            |  1 -
 5 file modificati, 37 inserzioni(+), 82 rimozioni(-)
 copy event_notifier.c => event_notifier-posix.c (100%)
 rename event_notifier.c => event_notifier-win32.c (29%)

Patch

diff --git a/Makefile.objs b/Makefile.objs
index cf00fd5..a99378c 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -91,7 +91,9 @@  common-obj-y += bt-host.o bt-vhci.o
 
 common-obj-y += acl.o
 common-obj-$(CONFIG_POSIX) += compatfd.o
-common-obj-y += notify.o event_notifier.o
+common-obj-y += notify.o
+common-obj-$(CONFIG_POSIX) += event_notifier-posix.o
+common-obj-$(CONFIG_WIN32) += event_notifier-win32.o
 common-obj-y += qemu-timer.o qemu-timer-common.o
 
 common-obj-$(CONFIG_SLIRP) += slirp/
diff --git a/event_notifier.c b/event_notifier-posix.c
similarity index 100%
copy from event_notifier.c
copy to event_notifier-posix.c
diff --git a/event_notifier.c b/event_notifier-win32.c
similarity index 29%
rename from event_notifier.c
rename to event_notifier-win32.c
index 88c114b..c723dad 100644
--- a/event_notifier.c
+++ b/event_notifier-win32.c
@@ -12,107 +12,48 @@ 
 
 #include "qemu-common.h"
 #include "event_notifier.h"
-#include "qemu-char.h"
-
-#ifdef CONFIG_EVENTFD
-#include <sys/eventfd.h>
-#endif
-
-void event_notifier_init_fd(EventNotifier *e, int fd)
-{
-    e->rfd = fd;
-    e->wfd = fd;
-}
+#include "main-loop.h"
 
 int event_notifier_init(EventNotifier *e, int active)
 {
-    int fds[2];
-    int ret;
-
-#ifdef CONFIG_EVENTFD
-    ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
-#else
-    ret = -1;
-    errno = ENOSYS;
-#endif
-    if (ret >= 0) {
-        e->rfd = e->wfd = ret;
-    } else {
-        if (errno != ENOSYS) {
-            return -errno;
-        }
-        if (qemu_pipe(fds) < 0) {
-            return -errno;
-        }
-        ret = fcntl_setfl(fds[0], O_NONBLOCK);
-        if (ret < 0) {
-            goto fail;
-        }
-        ret = fcntl_setfl(fds[1], O_NONBLOCK);
-        if (ret < 0) {
-            goto fail;
-        }
-        e->rfd = fds[0];
-        e->wfd = fds[1];
-    }
-    if (active) {
-        event_notifier_set(e);
-    }
+    e->event = CreateEvent(NULL, FALSE, FALSE, NULL);
+    assert(e->event);
     return 0;
-
-fail:
-    close(fds[0]);
-    close(fds[1]);
-    return ret;
 }
 
 void event_notifier_cleanup(EventNotifier *e)
 {
-    if (e->rfd != e->wfd) {
-        close(e->rfd);
-    }
-    close(e->wfd);
+    CloseHandle(e->event);
 }
 
-int event_notifier_get_fd(EventNotifier *e)
+HANDLE event_notifier_get_handle(EventNotifier *e)
 {
-    return e->rfd;
+    return e->event;
 }
 
 int event_notifier_set_handler(EventNotifier *e,
                                EventNotifierHandler *handler)
 {
-    return qemu_set_fd_handler(e->rfd, (IOHandler *)handler, NULL, e);
+    if (handler) {
+        return qemu_add_wait_object(e->event, (IOHandler *)handler, e);
+    } else {
+        qemu_del_wait_object(e->event, (IOHandler *)handler, e);
+        return 0;
+    }
 }
 
 int event_notifier_set(EventNotifier *e)
 {
-    static const uint64_t value = 1;
-    ssize_t ret;
-
-    do {
-        ret = write(e->wfd, &value, sizeof(value));
-    } while (ret < 0 && errno == EINTR);
-
-    /* EAGAIN is fine, a read must be pending.  */
-    if (ret < 0 && errno != EAGAIN) {
-        return -1;
-    }
+    SetEvent(e->event);
     return 0;
 }
 
 int event_notifier_test_and_clear(EventNotifier *e)
 {
-    int value;
-    ssize_t len;
-    char buffer[512];
-
-    /* Drain the notify pipe.  For eventfd, only 8 bytes will be read.  */
-    value = 0;
-    do {
-        len = read(e->rfd, buffer, sizeof(buffer));
-        value |= (len > 0);
-    } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
-
-    return value;
+    int ret = WaitForSingleObject(e->event, 0);
+    if (ret == WAIT_OBJECT_0) {
+        ResetEvent(e->event);
+        return true;
+    }
+    return false;
 }
diff --git a/event_notifier.h b/event_notifier.h
index f04d12d..88b57af 100644
--- a/event_notifier.h
+++ b/event_notifier.h
@@ -15,19 +15,32 @@ 
 
 #include "qemu-common.h"
 
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
 struct EventNotifier {
+#ifdef _WIN32
+    HANDLE event;
+#else
     int rfd;
     int wfd;
+#endif
 };
 
 typedef void EventNotifierHandler(EventNotifier *);
 
-void event_notifier_init_fd(EventNotifier *, int fd);
 int event_notifier_init(EventNotifier *, int active);
 void event_notifier_cleanup(EventNotifier *);
-int event_notifier_get_fd(EventNotifier *);
 int event_notifier_set(EventNotifier *);
 int event_notifier_test_and_clear(EventNotifier *);
 int event_notifier_set_handler(EventNotifier *, EventNotifierHandler *);
 
+#ifdef CONFIG_POSIX
+void event_notifier_init_fd(EventNotifier *, int fd);
+int event_notifier_get_fd(EventNotifier *);
+#else
+HANDLE event_notifier_get_handle(EventNotifier *);
+#endif
+
 #endif
diff --git a/qemu-os-win32.h b/qemu-os-win32.h
index 3b5a35b..400264e 100644
--- a/qemu-os-win32.h
+++ b/qemu-os-win32.h
@@ -28,7 +28,6 @@ 
 
 #include <windows.h>
 #include <winsock2.h>
-#include "main-loop.h"
 
 /* Workaround for older versions of MinGW. */
 #ifndef ECONNREFUSED