diff mbox

[03/11] poller: add poller_fill() and poller_poll()

Message ID 1359629644-21920-4-git-send-email-stefanha@redhat.com
State New
Headers show

Commit Message

Stefan Hajnoczi Jan. 31, 2013, 10:53 a.m. UTC
The Windows event loop cannot be converted entirely to g_poll(3).  It
will still need select(2) so add functions that convert between Poller
and rfds/wfds/xfds.  This way most code only needs to be aware of Poller
but it will still be possible to invoke select(2).

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 include/qemu/poller.h | 12 ++++++++++++
 util/poller.c         | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)
diff mbox

Patch

diff --git a/include/qemu/poller.h b/include/qemu/poller.h
index 7630099..39e07d2 100644
--- a/include/qemu/poller.h
+++ b/include/qemu/poller.h
@@ -53,4 +53,16 @@  int poller_add_fd(Poller *p, int fd, int events);
  */
 int poller_get_revents(Poller *p, int index);
 
+/**
+ * poller_fill: Fill fd_sets from a Poller
+ *
+ * Return the maximum file descriptor number.
+ */
+int poller_fill(Poller *p, fd_set *rfds, fd_set *wfds, fd_set *xfds);
+
+/**
+ * poller_poll: Bitwise OR fd_set results into Poller revents
+ */
+void poller_poll(Poller *p, int nfds, fd_set *rfds, fd_set *wfds, fd_set *xfds);
+
 #endif /* QEMU_POLLER_H */
diff --git a/util/poller.c b/util/poller.c
index 663e7a9..41e9b96 100644
--- a/util/poller.c
+++ b/util/poller.c
@@ -52,3 +52,49 @@  int poller_get_revents(Poller *p, int index)
     assert(index < p->nfds);
     return p->poll_fds[index].revents;
 }
+
+int poller_fill(Poller *p, fd_set *rfds, fd_set *wfds, fd_set *xfds)
+{
+    int nfds = -1;
+    int i;
+
+    for (i = 0; i < p->nfds; i++) {
+        int fd = p->poll_fds[i].fd;
+        int events = p->poll_fds[i].events;
+        if (events & (G_IO_IN | G_IO_HUP | G_IO_ERR)) {
+            FD_SET(fd, rfds);
+            nfds = MAX(nfds, fd);
+        }
+        if (events & (G_IO_OUT | G_IO_ERR)) {
+            FD_SET(fd, wfds);
+            nfds = MAX(nfds, fd);
+        }
+        if (events & G_IO_PRI) {
+            FD_SET(fd, xfds);
+            nfds = MAX(nfds, fd);
+        }
+    }
+    return nfds;
+}
+
+void poller_poll(Poller *p, int nfds, fd_set *rfds, fd_set *wfds, fd_set *xfds)
+{
+    int i;
+
+    for (i = 0; i < p->nfds; i++) {
+        int fd = p->poll_fds[i].fd;
+        int revents = 0;
+
+        if (FD_ISSET(fd, rfds)) {
+            revents |= G_IO_IN | G_IO_HUP | G_IO_ERR;
+        }
+        if (FD_ISSET(fd, wfds)) {
+            revents |= G_IO_OUT | G_IO_ERR;
+        }
+        if (FD_ISSET(fd, xfds)) {
+            revents |= G_IO_PRI;
+        }
+        revents &= p->poll_fds[i].events;
+        p->poll_fds[i].revents |= revents;
+    }
+}