From patchwork Thu Apr 16 04:57:30 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 461711 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 6DCBD1402E1 for ; Thu, 16 Apr 2015 14:58:17 +1000 (AEST) Received: from localhost ([::1]:35284 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YibsV-0005hR-FF for incoming@patchwork.ozlabs.org; Thu, 16 Apr 2015 00:58:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58856) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yibs5-0004zP-Eu for qemu-devel@nongnu.org; Thu, 16 Apr 2015 00:57:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yibs3-000398-RD for qemu-devel@nongnu.org; Thu, 16 Apr 2015 00:57:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57710) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yibs3-00038p-Jq for qemu-devel@nongnu.org; Thu, 16 Apr 2015 00:57:47 -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 (Postfix) with ESMTPS id 3098339A5A3 for ; Thu, 16 Apr 2015 04:57:47 +0000 (UTC) Received: from ad.nay.redhat.com (dhcp-14-137.nay.redhat.com [10.66.14.137]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t3G4vdeN025840; Thu, 16 Apr 2015 00:57:43 -0400 From: Fam Zheng To: qemu-devel@nongnu.org Date: Thu, 16 Apr 2015 12:57:30 +0800 Message-Id: <1429160256-27231-2-git-send-email-famz@redhat.com> In-Reply-To: <1429160256-27231-1-git-send-email-famz@redhat.com> References: <1429160256-27231-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Paolo Bonzini , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v3 1/7] poll: Introduce QEMU Poll API 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 This is abstract of underlying poll implementation. A glib implementation is included. Signed-off-by: Fam Zheng --- Makefile.objs | 2 +- include/qemu/poll.h | 40 +++++++++++++++ include/qemu/typedefs.h | 4 +- poll-glib.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 include/qemu/poll.h create mode 100644 poll-glib.c diff --git a/Makefile.objs b/Makefile.objs index 28999d3..77a56d0 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -9,7 +9,7 @@ util-obj-y = util/ qobject/ qapi/ qapi-types.o qapi-visit.o qapi-event.o block-obj-y = async.o thread-pool.o block-obj-y += nbd.o block.o blockjob.o block-obj-y += main-loop.o iohandler.o qemu-timer.o -block-obj-$(CONFIG_POSIX) += aio-posix.o +block-obj-$(CONFIG_POSIX) += aio-posix.o poll-glib.o block-obj-$(CONFIG_WIN32) += aio-win32.o block-obj-y += block/ block-obj-y += qemu-io-cmds.o diff --git a/include/qemu/poll.h b/include/qemu/poll.h new file mode 100644 index 0000000..597975f --- /dev/null +++ b/include/qemu/poll.h @@ -0,0 +1,40 @@ +#ifndef QEMU_POLL_H +#define QEMU_POLL_H + +#include "qemu/typedefs.h" +#include "qemu-common.h" + +struct QEMUPollEvent { + int fd; + int events; + int revents; + void *opaque; +}; + +QEMUPoll *qemu_poll_new(void); +void qemu_poll_free(QEMUPoll *qpoll); +int qemu_poll(QEMUPoll *qpoll, int64_t timeout_ns); + +/* Add an fd to poll. Return -EEXIST if fd already registered. */ +int qemu_poll_add(QEMUPoll *qpoll, int fd, int gio_events, void *opaque); + +/* Delete a previously added fd. Return -ENOENT if fd not registered. */ +int qemu_poll_del(QEMUPoll *qpoll, int fd); + +/** + * A shortcut to: + * 1) remove all the existing fds; + * 2) add all in @fds, while setting each fd's opaque pointer to the pollfd + * struct itself. + */ +int qemu_poll_set_fds(QEMUPoll *qpoll, GPollFD *fds, int nfds); + +/** + * Query the events from last qemu_poll. ONLY revent and opaque are valid in + * @events after return. + */ +int qemu_poll_get_events(QEMUPoll *qpoll, + QEMUPollEvent *events, + int max_events); + +#endif diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h index cde3314..8e638cb 100644 --- a/include/qemu/typedefs.h +++ b/include/qemu/typedefs.h @@ -64,10 +64,12 @@ typedef struct QEMUBH QEMUBH; typedef struct QemuConsole QemuConsole; typedef struct QEMUFile QEMUFile; typedef struct QEMUMachine QEMUMachine; +typedef struct QEMUPoll QEMUPoll; +typedef struct QEMUPollEvent QEMUPollEvent; typedef struct QEMUSGList QEMUSGList; typedef struct QEMUSizedBuffer QEMUSizedBuffer; -typedef struct QEMUTimerListGroup QEMUTimerListGroup; typedef struct QEMUTimer QEMUTimer; +typedef struct QEMUTimerListGroup QEMUTimerListGroup; typedef struct Range Range; typedef struct SerialState SerialState; typedef struct SHPCDevice SHPCDevice; diff --git a/poll-glib.c b/poll-glib.c new file mode 100644 index 0000000..64fde69 --- /dev/null +++ b/poll-glib.c @@ -0,0 +1,130 @@ +/* + * g_poll implementation for QEMU Poll API + * + * Copyright Red Hat, Inc. 2014 + * + * Authors: + * Fam Zheng + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#include "qemu-common.h" +#include "qemu/timer.h" +#include "qemu/poll.h" + +struct QEMUPoll { + /* Array of GPollFD for g_poll() */ + GArray *gpollfds; + + /* Array of opaque pointers, should be in sync with gpollfds */ + GArray *opaque; +}; + +QEMUPoll *qemu_poll_new(void) +{ + QEMUPoll *qpoll = g_new0(QEMUPoll, 1); + qpoll->gpollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD)); + qpoll->opaque = g_array_new(FALSE, FALSE, sizeof(void *)); + return qpoll; +} + + +void qemu_poll_free(QEMUPoll *qpoll) +{ + g_array_unref(qpoll->gpollfds); + g_array_unref(qpoll->opaque); + g_free(qpoll); +} + +int qemu_poll(QEMUPoll *qpoll, int64_t timeout_ns) +{ + int i; + for (i = 0; i < qpoll->gpollfds->len; i++) { + GPollFD *p = &g_array_index(qpoll->gpollfds, GPollFD, i); + p->revents = 0; + } + return g_poll((GPollFD *)qpoll->gpollfds->data, + qpoll->gpollfds->len, + qemu_timeout_ns_to_ms(timeout_ns)); +} + +/* Add an fd to poll. Return -EEXIST if fd already registered. */ +int qemu_poll_add(QEMUPoll *qpoll, int fd, int gio_events, void *opaque) +{ + int i; + GPollFD pfd = (GPollFD) { + .fd = fd, + .revents = 0, + .events = gio_events, + }; + + for (i = 0; i < qpoll->gpollfds->len; i++) { + GPollFD *p = &g_array_index(qpoll->gpollfds, GPollFD, i); + if (p->fd == fd) { + return -EEXIST; + } + } + g_array_append_val(qpoll->gpollfds, pfd); + g_array_append_val(qpoll->opaque, opaque); + assert(qpoll->gpollfds->len == qpoll->opaque->len); + return 0; +} + +/* Delete a previously added fd. Return -ENOENT if fd not registered. */ +int qemu_poll_del(QEMUPoll *qpoll, int fd) +{ + int i; + int found = -1; + + for (i = 0; i < qpoll->gpollfds->len; i++) { + GPollFD *p = &g_array_index(qpoll->gpollfds, GPollFD, i); + if (p->fd == fd) { + found = i; + break; + } + } + if (found >= 0) { + g_array_remove_index(qpoll->gpollfds, found); + g_array_remove_index(qpoll->opaque, found); + assert(qpoll->gpollfds->len == qpoll->opaque->len); + return 0; + } else { + return -ENOENT; + } +} + +int qemu_poll_set_fds(QEMUPoll *qpoll, GPollFD *fds, int nfds) +{ + int i; + g_array_set_size(qpoll->gpollfds, 0); + g_array_set_size(qpoll->opaque, 0); + for (i = 0; i < nfds; i++) { + void *opaque = &fds[i]; + g_array_append_val(qpoll->gpollfds, fds[i]); + g_array_append_val(qpoll->opaque, opaque); + assert(qpoll->gpollfds->len == qpoll->opaque->len); + } + return nfds; +} + +int qemu_poll_get_events(QEMUPoll *qpoll, + QEMUPollEvent *events, + int max_events) +{ + int i; + int r = 0; + for (i = 0; i < qpoll->gpollfds->len && r < max_events; i++) { + GPollFD *fd = &g_array_index(qpoll->gpollfds, GPollFD, i); + if (fd->revents & fd->events) { + events[r].fd = fd->fd; + events[r].revents = fd->revents; + events[r].events = fd->events; + events[r].opaque = g_array_index(qpoll->opaque, void *, i); + r++; + } + } + return r; +}