From patchwork Thu Apr 5 10:59:13 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kiszka X-Patchwork-Id: 150942 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 9ADB9B6FEA for ; Thu, 5 Apr 2012 21:56:45 +1000 (EST) Received: from localhost ([::1]:42577 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SFkTU-000146-BM for incoming@patchwork.ozlabs.org; Thu, 05 Apr 2012 07:03:32 -0400 Received: from eggs.gnu.org ([208.118.235.92]:45210) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SFkQp-0007jj-TN for qemu-devel@nongnu.org; Thu, 05 Apr 2012 07:03:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SFkPR-000755-IL for qemu-devel@nongnu.org; Thu, 05 Apr 2012 07:00:47 -0400 Received: from goliath.siemens.de ([192.35.17.28]:16874) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SFkPR-00073u-8l for qemu-devel@nongnu.org; Thu, 05 Apr 2012 06:59:21 -0400 Received: from mail1.siemens.de (localhost [127.0.0.1]) by goliath.siemens.de (8.13.6/8.13.6) with ESMTP id q35AxIrt020062; Thu, 5 Apr 2012 12:59:18 +0200 Received: from mchn199C.mchp.siemens.de ([139.25.109.49]) by mail1.siemens.de (8.13.6/8.13.6) with ESMTP id q35AxIcC027596; Thu, 5 Apr 2012 12:59:18 +0200 From: Jan Kiszka To: Anthony Liguori , qemu-devel@nongnu.org Date: Thu, 5 Apr 2012 12:59:13 +0200 Message-Id: X-Mailer: git-send-email 1.7.3.4 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 192.35.17.28 Cc: Kevin Wolf , Paolo Bonzini Subject: [Qemu-devel] [PATCH v3 06/10] Use QemuEvent in main loop 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 QemuEvent provides the same service, just in a central place. Signed-off-by: Jan Kiszka --- main-loop.c | 104 +++++++--------------------------------------------------- 1 files changed, 13 insertions(+), 91 deletions(-) diff --git a/main-loop.c b/main-loop.c index db23de0..998d291 100644 --- a/main-loop.c +++ b/main-loop.c @@ -23,78 +23,18 @@ */ #include "qemu-common.h" +#include "qemu-thread.h" #include "qemu-timer.h" #include "slirp/slirp.h" #include "main-loop.h" +static bool io_event_initialized; +static QemuEvent io_event; + #ifndef _WIN32 #include "compatfd.h" -static int io_thread_fd = -1; - -void qemu_notify_event(void) -{ - /* Write 8 bytes to be compatible with eventfd. */ - static const uint64_t val = 1; - ssize_t ret; - - if (io_thread_fd == -1) { - return; - } - do { - ret = write(io_thread_fd, &val, sizeof(val)); - } while (ret < 0 && errno == EINTR); - - /* EAGAIN is fine, a read must be pending. */ - if (ret < 0 && errno != EAGAIN) { - fprintf(stderr, "qemu_notify_event: write() failed: %s\n", - strerror(errno)); - exit(1); - } -} - -static void qemu_event_read(void *opaque) -{ - int fd = (intptr_t)opaque; - ssize_t len; - char buffer[512]; - - /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ - do { - len = read(fd, buffer, sizeof(buffer)); - } while ((len == -1 && errno == EINTR) || len == sizeof(buffer)); -} - -static int qemu_event_init(void) -{ - int err; - int fds[2]; - - err = qemu_eventfd(fds); - if (err == -1) { - return -errno; - } - err = fcntl_setfl(fds[0], O_NONBLOCK); - if (err < 0) { - goto fail; - } - err = fcntl_setfl(fds[1], O_NONBLOCK); - if (err < 0) { - goto fail; - } - qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, - (void *)(intptr_t)fds[0]); - - io_thread_fd = fds[1]; - return 0; - -fail: - close(fds[0]); - close(fds[1]); - return err; -} - /* If we have signalfd, we mask out the signals we want to handle and then * use signalfd to listen for them. We rely on whatever the current signal * handler is to dispatch the signals when we receive them. @@ -164,40 +104,23 @@ static int qemu_signal_init(void) #else /* _WIN32 */ -HANDLE qemu_event_handle = NULL; - -static void dummy_event_handler(void *opaque) -{ -} - -static int qemu_event_init(void) +static int qemu_signal_init(void) { - qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); - if (!qemu_event_handle) { - fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError()); - return -1; - } - qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); return 0; } +#endif void qemu_notify_event(void) { - if (!qemu_event_handle) { - return; - } - if (!SetEvent(qemu_event_handle)) { - fprintf(stderr, "qemu_notify_event: SetEvent failed: %ld\n", - GetLastError()); - exit(1); + if (io_event_initialized) { + qemu_event_signal(&io_event); } } -static int qemu_signal_init(void) +static void drain_io_event(void *opaque) { - return 0; + qemu_event_consume(&io_event); } -#endif int main_loop_init(void) { @@ -210,10 +133,9 @@ int main_loop_init(void) } /* Note eventfd must be drained before signalfd handlers run */ - ret = qemu_event_init(); - if (ret) { - return ret; - } + qemu_event_init(&io_event, false); + qemu_event_set_handler(&io_event, drain_io_event, NULL); + io_event_initialized = true; return 0; }