From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54378) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X1yih-0002R0-3E for qemu-devel@nongnu.org; Tue, 01 Jul 2014 10:07:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X1yiZ-0003HE-BV for qemu-devel@nongnu.org; Tue, 01 Jul 2014 10:07:39 -0400 Received: from mx1.redhat.com ([209.132.183.28]:22702) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X1yiZ-0003Gt-2m for qemu-devel@nongnu.org; Tue, 01 Jul 2014 10:07:31 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s61E7UYH020001 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 1 Jul 2014 10:07:30 -0400 From: Gerd Hoffmann Date: Tue, 1 Jul 2014 16:07:26 +0200 Message-Id: <1404223646-7899-5-git-send-email-kraxel@redhat.com> In-Reply-To: <1404223646-7899-1-git-send-email-kraxel@redhat.com> References: <1404223646-7899-1-git-send-email-kraxel@redhat.com> Subject: [Qemu-devel] [PULL 4/4] ccid-card-emulated: use EventNotifier List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Gerd Hoffmann From: Paolo Bonzini Shut up Coverity's complaint about unchecked fcntl return values, and especially make the code simpler and more efficient. Signed-off-by: Paolo Bonzini Signed-off-by: Gerd Hoffmann --- hw/usb/ccid-card-emulated.c | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/hw/usb/ccid-card-emulated.c b/hw/usb/ccid-card-emulated.c index 7213c89..aa1c37a 100644 --- a/hw/usb/ccid-card-emulated.c +++ b/hw/usb/ccid-card-emulated.c @@ -126,7 +126,7 @@ struct EmulatedState { QemuMutex vreader_mutex; /* and guest_apdu_list mutex */ QemuMutex handle_apdu_mutex; QemuCond handle_apdu_cond; - int pipe[2]; + EventNotifier notifier; int quit_apdu_thread; QemuThread apdu_thread_id; }; @@ -162,9 +162,7 @@ static void emulated_push_event(EmulatedState *card, EmulEvent *event) qemu_mutex_lock(&card->event_list_mutex); QSIMPLEQ_INSERT_TAIL(&(card->event_list), event, entry); qemu_mutex_unlock(&card->event_list_mutex); - if (write(card->pipe[1], card, 1) != 1) { - DPRINTF(card, 1, "write to pipe failed\n"); - } + event_notifier_set(&card->notifier); } static void emulated_push_type(EmulatedState *card, uint32_t type) @@ -358,16 +356,12 @@ static void *event_thread(void *arg) return NULL; } -static void pipe_read(void *opaque) +static void card_event_handler(EventNotifier *notifier) { - EmulatedState *card = opaque; + EmulatedState *card = container_of(notifier, EmulatedState, notifier); EmulEvent *event, *next; - char dummy; - int len; - do { - len = read(card->pipe[0], &dummy, sizeof(dummy)); - } while (len == sizeof(dummy)); + event_notifier_test_and_clear(&card->notifier); qemu_mutex_lock(&card->event_list_mutex); QSIMPLEQ_FOREACH_SAFE(event, &card->event_list, entry, next) { DPRINTF(card, 2, "event %s\n", emul_event_to_string(event->p.gen.type)); @@ -404,16 +398,13 @@ static void pipe_read(void *opaque) qemu_mutex_unlock(&card->event_list_mutex); } -static int init_pipe_signaling(EmulatedState *card) +static int init_event_notifier(EmulatedState *card) { - if (pipe(card->pipe) < 0) { - DPRINTF(card, 2, "pipe creation failed\n"); + if (event_notifier_init(&card->notifier, false) < 0) { + DPRINTF(card, 2, "event notifier creation failed\n"); return -1; } - fcntl(card->pipe[0], F_SETFL, O_NONBLOCK); - fcntl(card->pipe[1], F_SETFL, O_NONBLOCK); - fcntl(card->pipe[0], F_SETOWN, getpid()); - qemu_set_fd_handler(card->pipe[0], pipe_read, NULL, card); + event_notifier_set_handler(&card->notifier, card_event_handler); return 0; } @@ -500,7 +491,7 @@ static int emulated_initfn(CCIDCardState *base) qemu_cond_init(&card->handle_apdu_cond); card->reader = NULL; card->quit_apdu_thread = 0; - if (init_pipe_signaling(card) < 0) { + if (init_event_notifier(card) < 0) { return -1; } -- 1.8.3.1