From patchwork Wed May 26 14:09:36 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 53619 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9B220B6EFF for ; Thu, 27 May 2010 00:44:58 +1000 (EST) Received: from localhost ([127.0.0.1]:51481 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OHHqo-0004T4-Be for incoming@patchwork.ozlabs.org; Wed, 26 May 2010 10:44:54 -0400 Received: from [140.186.70.92] (port=45304 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OHHN6-0003KG-CB for qemu-devel@nongnu.org; Wed, 26 May 2010 10:14:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OHHJ2-000489-VP for qemu-devel@nongnu.org; Wed, 26 May 2010 10:10:06 -0400 Received: from mail-ww0-f45.google.com ([74.125.82.45]:39026) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OHHJ2-00044Y-RL for qemu-devel@nongnu.org; Wed, 26 May 2010 10:10:00 -0400 Received: by mail-ww0-f45.google.com with SMTP id 39so860125wwb.4 for ; Wed, 26 May 2010 07:10:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:sender:from:to:subject:date :message-id:x-mailer:in-reply-to:references; bh=NcCjBQ93vezWEGKXlfhsS5ulMJiLodWg+kQbaNt5ZiA=; b=Tfy/hoZhsfy8vbAGngGuhdwMDkVmPvoSYUom30HFVrbCGsEK9zH+qPjyXGoVFGtoS+ g4/U64+nd0Z7wln6WnEwtnv0MdwNOf6CHdiPPGogdIsIer8AlBfdtKju3S/MjdI8yxWz DYc70LOfiaREOMEVVHXYxz/1Dj1SaUfpf8k5I= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:from:to:subject:date:message-id:x-mailer:in-reply-to :references; b=d1JumpxV9XuS/TbmvPwK7y50XXSmW0IPS8p5f0/vSb/wZFsvRm81RwtVf4ZmZfBhmU mmDBncFtZNJSum4X9Ko7DY91EljtWulm1O47REo4F9853roJ2Pmd1H6G1T+N1bTps/l7 itaG8RYSEo3KfhNab9+6SKmDC7/umXjBCMKDI= Received: by 10.227.128.208 with SMTP id l16mr8439997wbs.175.1274882998455; Wed, 26 May 2010 07:09:58 -0700 (PDT) Received: from localhost.localdomain (nat-pool-brq-t.redhat.com [209.132.186.34]) by mx.google.com with ESMTPS id u32sm318964wbc.11.2010.05.26.07.09.57 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 26 May 2010 07:09:57 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 26 May 2010 16:09:36 +0200 Message-Id: <1274882978-9875-7-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.6.6.1 In-Reply-To: <1274882978-9875-1-git-send-email-pbonzini@redhat.com> References: <1274882978-9875-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) Subject: [Qemu-devel] [PATCH 6/8] enable event_notifier to use pipes X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Paolo Bonzini --- 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 -#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 *);