From patchwork Mon Feb 4 12:12:46 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 217895 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 5FC7A2C02AF for ; Mon, 4 Feb 2013 23:39:39 +1100 (EST) Received: from localhost ([::1]:36886 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U2KvX-0000bi-GT for incoming@patchwork.ozlabs.org; Mon, 04 Feb 2013 07:13:35 -0500 Received: from eggs.gnu.org ([208.118.235.92]:42324) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U2KvC-0000AT-C5 for qemu-devel@nongnu.org; Mon, 04 Feb 2013 07:13:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1U2Kv6-0000x3-Ei for qemu-devel@nongnu.org; Mon, 04 Feb 2013 07:13:14 -0500 Received: from mx1.redhat.com ([209.132.183.28]:49295) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U2Kv6-0000wy-7O for qemu-devel@nongnu.org; Mon, 04 Feb 2013 07:13:08 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r14CD5T3006684 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 4 Feb 2013 07:13:05 -0500 Received: from localhost (ovpn-112-30.ams2.redhat.com [10.36.112.30]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r14CD4c9018775; Mon, 4 Feb 2013 07:13:04 -0500 From: Stefan Hajnoczi To: Date: Mon, 4 Feb 2013 13:12:46 +0100 Message-Id: <1359979973-31338-4-git-send-email-stefanha@redhat.com> In-Reply-To: <1359979973-31338-1-git-send-email-stefanha@redhat.com> References: <1359979973-31338-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 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 , Blue Swirl , Stefan Hajnoczi , Paolo Bonzini , Amos Kong , Laszlo Ersek Subject: [Qemu-devel] [PATCH v3 03/10] main-loop: switch POSIX glib integration to GPollFD 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 Convert glib file descriptor polling from rfds/wfds/xfds to GPollFD. The Windows code still needs poll_fds[] and n_poll_fds but they can now become local variables. Signed-off-by: Stefan Hajnoczi Reviewed-by: Laszlo Ersek --- main-loop.c | 71 +++++++++++++++++++------------------------------------------ 1 file changed, 22 insertions(+), 49 deletions(-) diff --git a/main-loop.c b/main-loop.c index f1dcd14..12b0213 100644 --- a/main-loop.c +++ b/main-loop.c @@ -145,8 +145,6 @@ int qemu_init_main_loop(void) static fd_set rfds, wfds, xfds; static int nfds; -static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */ -static int n_poll_fds; static int max_priority; /* Load rfds/wfds/xfds into gpollfds. Will be removed a few commits later. */ @@ -206,65 +204,39 @@ static void gpollfds_to_select(int ret) } #ifndef _WIN32 -static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds, - fd_set *xfds, uint32_t *cur_timeout) +static int glib_pollfds_idx; +static int glib_n_poll_fds; + +static void glib_pollfds_fill(uint32_t *cur_timeout) { GMainContext *context = g_main_context_default(); - int i; int timeout = 0; + int n; g_main_context_prepare(context, &max_priority); - n_poll_fds = g_main_context_query(context, max_priority, &timeout, - poll_fds, ARRAY_SIZE(poll_fds)); - g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds)); - - for (i = 0; i < n_poll_fds; i++) { - GPollFD *p = &poll_fds[i]; - - if ((p->events & G_IO_IN)) { - FD_SET(p->fd, rfds); - *max_fd = MAX(*max_fd, p->fd); - } - if ((p->events & G_IO_OUT)) { - FD_SET(p->fd, wfds); - *max_fd = MAX(*max_fd, p->fd); - } - if ((p->events & G_IO_ERR)) { - FD_SET(p->fd, xfds); - *max_fd = MAX(*max_fd, p->fd); - } - } + glib_pollfds_idx = gpollfds->len; + n = glib_n_poll_fds; + do { + GPollFD *pfds; + glib_n_poll_fds = n; + g_array_set_size(gpollfds, glib_pollfds_idx + glib_n_poll_fds); + pfds = &g_array_index(gpollfds, GPollFD, glib_pollfds_idx); + n = g_main_context_query(context, max_priority, &timeout, pfds, + glib_n_poll_fds); + } while (n != glib_n_poll_fds); if (timeout >= 0 && timeout < *cur_timeout) { *cur_timeout = timeout; } } -static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds, - bool err) +static void glib_pollfds_poll(void) { GMainContext *context = g_main_context_default(); + GPollFD *pfds = &g_array_index(gpollfds, GPollFD, glib_pollfds_idx); - if (!err) { - int i; - - for (i = 0; i < n_poll_fds; i++) { - GPollFD *p = &poll_fds[i]; - - if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) { - p->revents |= G_IO_IN; - } - if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) { - p->revents |= G_IO_OUT; - } - if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) { - p->revents |= G_IO_ERR; - } - } - } - - if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) { + if (g_main_context_check(context, max_priority, pfds, glib_n_poll_fds)) { g_main_context_dispatch(context); } } @@ -273,7 +245,7 @@ static int os_host_main_loop_wait(uint32_t timeout) { int ret; - glib_select_fill(&nfds, &rfds, &wfds, &xfds, &timeout); + glib_pollfds_fill(&timeout); if (timeout > 0) { qemu_mutex_unlock_iothread(); @@ -292,7 +264,7 @@ static int os_host_main_loop_wait(uint32_t timeout) qemu_mutex_lock_iothread(); } - glib_select_poll(&rfds, &wfds, &xfds, (ret < 0)); + glib_pollfds_poll(); return ret; } #else @@ -438,8 +410,9 @@ static void pollfds_poll(GArray *pollfds, int nfds, fd_set *rfds, static int os_host_main_loop_wait(uint32_t timeout) { GMainContext *context = g_main_context_default(); + GPollFD poll_fds[1024 * 2]; /* this is probably overkill */ int select_ret = 0; - int g_poll_ret, ret, i; + int g_poll_ret, ret, i, n_poll_fds; PollingEntry *pe; WaitObjects *w = &wait_objects; gint poll_timeout;