From patchwork Tue Jul 24 22:33:58 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nicholas A. Bellinger" X-Patchwork-Id: 173069 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 0125F2C008D for ; Wed, 25 Jul 2012 08:37:36 +1000 (EST) Received: from localhost ([::1]:36322 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1StnjS-0004YI-2B for incoming@patchwork.ozlabs.org; Tue, 24 Jul 2012 18:37:34 -0400 Received: from eggs.gnu.org ([208.118.235.92]:60193) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1StnjB-0004Qc-EZ for qemu-devel@nongnu.org; Tue, 24 Jul 2012 18:37:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Stnj8-00031o-QT for qemu-devel@nongnu.org; Tue, 24 Jul 2012 18:37:17 -0400 Received: from mail.linux-iscsi.org ([67.23.28.174]:51096 helo=linux-iscsi.org) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Stnj8-00031W-LY for qemu-devel@nongnu.org; Tue, 24 Jul 2012 18:37:14 -0400 Received: from linux-iscsi.org (localhost [127.0.0.1]) by linux-iscsi.org (Postfix) with ESMTP id AE6E422D9DD; Tue, 24 Jul 2012 22:34:12 +0000 (UTC) From: "Nicholas A. Bellinger" To: target-devel Date: Tue, 24 Jul 2012 22:33:58 +0000 Message-Id: <1343169246-17636-2-git-send-email-nab@linux-iscsi.org> X-Mailer: git-send-email 1.7.2.5 In-Reply-To: <1343169246-17636-1-git-send-email-nab@linux-iscsi.org> References: <1343169246-17636-1-git-send-email-nab@linux-iscsi.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 1) X-Received-From: 67.23.28.174 Cc: Jens Axboe , Anthony Liguori , Stefan Hajnoczi , kvm-devel , "Michael S. Tsirkin" , qemu-devel , Zhi Yong Wu , Anthony Liguori , Hannes Reinecke , Paolo Bonzini , lf-virt , Christoph Hellwig Subject: [Qemu-devel] [RFC 1/9] notifier: add validity check and notify function 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 From: Stefan Hajnoczi Event notifiers that have not had the event_notifier_init() function called on them are invalid. The event_notifier_valid() function checks whether or not an event notifier is valild. This can be used to check whether a notifier is in use or not. It sometimes useful to notify the event notifier, for example when vhost is on the receiving end of the event notifier and qemu needs to notify it. The event_notifier_notify() function will signal the eventfd and increment it by one. Signed-off-by: Stefan Hajnoczi Cc: Anthony Liguori Cc: Paolo Bonzini Signed-off-by: Nicholas Bellinger --- event_notifier.c | 21 +++++++++++++++++++++ event_notifier.h | 4 ++++ 2 files changed, 25 insertions(+), 0 deletions(-) diff --git a/event_notifier.c b/event_notifier.c index 2c207e1..efe00ea 100644 --- a/event_notifier.c +++ b/event_notifier.c @@ -25,6 +25,7 @@ void event_notifier_init_fd(EventNotifier *e, int fd) int event_notifier_init(EventNotifier *e, int active) { + assert(!event_notifier_valid(e)); #ifdef CONFIG_EVENTFD int fd = eventfd(!!active, EFD_NONBLOCK | EFD_CLOEXEC); if (fd < 0) @@ -39,6 +40,12 @@ int event_notifier_init(EventNotifier *e, int active) void event_notifier_cleanup(EventNotifier *e) { close(e->fd); + e->fd = -1; +} + +bool event_notifier_valid(EventNotifier *e) +{ + return e->fd != -1; } int event_notifier_get_fd(EventNotifier *e) @@ -65,3 +72,17 @@ int event_notifier_test_and_clear(EventNotifier *e) int r = read(e->fd, &value, sizeof(value)); return r == sizeof(value); } + +int event_notifier_notify(EventNotifier *e) +{ + uint64_t value = 1; + int r; + + assert(event_notifier_valid(e)); + r = write(e->fd, &value, sizeof(value)); + if (r < 0) { + return -errno; + } + assert(r == sizeof(value)); + return 0; +} diff --git a/event_notifier.h b/event_notifier.h index f0ec2f2..eea10a9 100644 --- a/event_notifier.h +++ b/event_notifier.h @@ -15,6 +15,8 @@ #include "qemu-common.h" +#define EVENT_NOTIFIER_INITIALIZER ((EventNotifier){ .fd = -1 }) + struct EventNotifier { int fd; }; @@ -24,9 +26,11 @@ typedef void EventNotifierHandler(EventNotifier *); void event_notifier_init_fd(EventNotifier *, int fd); int event_notifier_init(EventNotifier *, int active); void event_notifier_cleanup(EventNotifier *); +bool event_notifier_valid(EventNotifier *e); int event_notifier_get_fd(EventNotifier *); int event_notifier_set(EventNotifier *); int event_notifier_test_and_clear(EventNotifier *); int event_notifier_set_handler(EventNotifier *, EventNotifierHandler *); +int event_notifier_notify(EventNotifier *e); #endif