qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 6/8] enable event_notifier to use pipes
Date: Wed, 26 May 2010 16:09:36 +0200	[thread overview]
Message-ID: <1274882978-9875-7-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1274882978-9875-1-git-send-email-pbonzini@redhat.com>

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 event_notifier.c |   69 +++++++++++++++++++++++++++++++++++++++---------------
 event_notifier.h |    3 +-
 2 files changed, 52 insertions(+), 20 deletions(-)

diff --git a/event_notifier.c b/event_notifier.c
index 066adb9..a33f3c5 100644
--- a/event_notifier.c
+++ b/event_notifier.c
@@ -10,51 +10,82 @@
  * the COPYING file in the top-level directory.
  */
 
+#include "qemu-common.h"
 #include "event_notifier.h"
 #include "qemu-char.h"
-#ifdef CONFIG_EVENTFD
-#include <sys/eventfd.h>
-#endif
 
 int event_notifier_init(EventNotifier *e, int active)
 {
-#ifdef CONFIG_EVENTFD
-    int fd = eventfd(!!active, EFD_NONBLOCK | EFD_CLOEXEC);
-    if (fd < 0)
+    int fds[2];
+    int err;
+    if (qemu_eventfd (fds) < 0)
         return -errno;
-    e->fd = fd;
+
+    err = fcntl_setfl(fds[0], O_NONBLOCK);
+    if (err < 0)
+        goto fail;
+
+    err = fcntl_setfl(fds[1], O_NONBLOCK);
+    if (err < 0)
+        goto fail;
+
+    e->rfd = fds[0];
+    e->wfd = fds[1];
+    if (active)
+        event_notifier_set(e);
     return 0;
-#else
-    return -ENOSYS;
-#endif
+
+fail:
+    close(fds[0]);
+    close(fds[1]);
+    return err;
 }
 
 void event_notifier_cleanup(EventNotifier *e)
 {
-    close(e->fd);
+    close(e->rfd);
+    close(e->wfd);
 }
 
 int event_notifier_get_fd(EventNotifier *e)
 {
-    return e->fd;
+    return e->wfd;
 }
 
 int event_notifier_set_handler(EventNotifier *e,
                                EventNotifierHandler *handler)
 {
-    return qemu_set_fd_handler(e->fd, (IOHandler *)handler, NULL, e);
+    return qemu_set_fd_handler(e->rfd, (IOHandler *)handler, NULL, e);
 }
 
 int event_notifier_set(EventNotifier *e)
 {
-    uint64_t value = 1;
-    int r = write(e->fd, &value, sizeof(value));
-    return r == sizeof(value);
+    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;
+    }
+    return 0;
 }
 
 int event_notifier_test_and_clear(EventNotifier *e)
 {
-    uint64_t value;
-    int r = read(e->fd, &value, sizeof(value));
-    return r == sizeof(value);
+    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;
 }
diff --git a/event_notifier.h b/event_notifier.h
index f6ec9ef..ff9d6f2 100644
--- a/event_notifier.h
+++ b/event_notifier.h
@@ -4,7 +4,8 @@
 #include "qemu-common.h"
 
 struct EventNotifier {
-	int fd;
+    int rfd;
+    int wfd;
 };
 
 typedef void EventNotifierHandler(EventNotifier *);
-- 
1.6.6.1

  parent reply	other threads:[~2010-05-26 14:14 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-26 14:09 [Qemu-devel] [PATCH 0/8] Make event_notifier more useful and more used Paolo Bonzini
2010-05-26 14:09 ` [Qemu-devel] [PATCH 1/8] move event_notifier into the main directory Paolo Bonzini
2010-05-26 14:09 ` [Qemu-devel] [PATCH 2/8] add event_notifier_set Paolo Bonzini
2010-05-26 14:09 ` [Qemu-devel] [PATCH 3/8] remove event_notifier_test Paolo Bonzini
2010-05-26 14:09 ` [Qemu-devel] [PATCH 4/8] add and use virtqueue_from_guest_notifier Paolo Bonzini
2010-05-26 14:09 ` [Qemu-devel] [PATCH 5/8] add and use event_notifier_set_handler Paolo Bonzini
2010-05-26 14:09 ` Paolo Bonzini [this message]
2010-05-26 18:19   ` [Qemu-devel] [PATCH 6/8] enable event_notifier to use pipes Richard Henderson
2010-05-26 19:48     ` [Qemu-devel] " Paolo Bonzini
2010-05-26 14:09 ` [Qemu-devel] [PATCH 7/8] add Win32 implementation of event notifiers Paolo Bonzini
2010-05-26 14:09 ` [Qemu-devel] [PATCH 8/8] change ioevent to use " Paolo Bonzini
2010-05-27 14:21 ` [Qemu-devel] Re: [PATCH 0/8] Make event_notifier more useful and more used Michael S. Tsirkin
2010-05-27 15:37   ` Paolo Bonzini
2010-06-15 13:53 ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1274882978-9875-7-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).