From patchwork Thu Jan 31 10:53:56 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 217154 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 555252C0087 for ; Thu, 31 Jan 2013 21:54:54 +1100 (EST) Received: from localhost ([::1]:51133 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U0rnA-0000YG-5O for incoming@patchwork.ozlabs.org; Thu, 31 Jan 2013 05:54:52 -0500 Received: from eggs.gnu.org ([208.118.235.92]:38077) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U0rmj-00006J-6i for qemu-devel@nongnu.org; Thu, 31 Jan 2013 05:54:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1U0rmc-00043J-3A for qemu-devel@nongnu.org; Thu, 31 Jan 2013 05:54:25 -0500 Received: from mx1.redhat.com ([209.132.183.28]:16776) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U0rmb-00043E-RB for qemu-devel@nongnu.org; Thu, 31 Jan 2013 05:54:18 -0500 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r0VAsGYW032490 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 31 Jan 2013 05:54:16 -0500 Received: from localhost (ovpn-112-28.ams2.redhat.com [10.36.112.28]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r0VAsFqD018860; Thu, 31 Jan 2013 05:54:15 -0500 From: Stefan Hajnoczi To: Date: Thu, 31 Jan 2013 11:53:56 +0100 Message-Id: <1359629644-21920-4-git-send-email-stefanha@redhat.com> In-Reply-To: <1359629644-21920-1-git-send-email-stefanha@redhat.com> References: <1359629644-21920-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Anthony Liguori , Jan Kiszka , Fabien Chouteau , Stefan Hajnoczi , Paolo Bonzini , Amos Kong Subject: [Qemu-devel] [PATCH 03/11] poller: add poller_fill() and poller_poll() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- include/qemu/poller.h | 12 ++++++++++++ util/poller.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) 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; + } +}