From patchwork Thu Aug 8 06:26:07 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 265661 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)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 840C62C00BA for ; Thu, 8 Aug 2013 16:28:15 +1000 (EST) Received: from localhost ([::1]:53422 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V7Jhl-000161-Ek for incoming@patchwork.ozlabs.org; Thu, 08 Aug 2013 02:28:13 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59036) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V7JgU-0008Jo-Hg for qemu-devel@nongnu.org; Thu, 08 Aug 2013 02:26:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V7JgP-00013Q-6V for qemu-devel@nongnu.org; Thu, 08 Aug 2013 02:26:54 -0400 Received: from mail-ie0-x231.google.com ([2607:f8b0:4001:c03::231]:58389) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V7JgO-00013F-UD for qemu-devel@nongnu.org; Thu, 08 Aug 2013 02:26:49 -0400 Received: by mail-ie0-f177.google.com with SMTP id a11so1213707iee.36 for ; Wed, 07 Aug 2013 23:26:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=s3wfHX+3FhgmTxIiPdw1I4DRGH78HDI6mGRZ2GQ06F8=; b=c3iJ8/UE7MUdsY5Tq/2b70smVH/SMzSW+ow2GPu4o942AAQn1mDogy/PWok1WJ9fCY pcS3QYb1U4c6X6ST/Dt11u8HCX7PCB7jIJwQwF3EF0opy19HACSca06F9SmRu+cuYPDu +x06m1neuO0v0uvg87clEyuy5VOQhs47OhxwNOUlK3kEIGENlaSPrPaWt60yJPdxxd1Z 7JqFomiqx4Jy0rK/2RObIiFzgr5SGHW3VfePB41vqOWQ3ojbTlenYQ+CvZotvgrbB2Yk /wUDpIZjwaDCkhiVeSbn8gFCEn6wIf+CDeS1DnImG7Nc2AjxgF1a9V0W16JbOShjMJGD IeyQ== X-Received: by 10.42.144.69 with SMTP id a5mr1399746icv.74.1375943208438; Wed, 07 Aug 2013 23:26:48 -0700 (PDT) Received: from localhost ([202.108.130.138]) by mx.google.com with ESMTPSA id fu2sm4661988igb.3.2013.08.07.23.26.44 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 07 Aug 2013 23:26:47 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Thu, 8 Aug 2013 14:26:07 +0800 Message-Id: <1375943171-1063-2-git-send-email-pingfank@linux.vnet.ibm.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1375943171-1063-1-git-send-email-pingfank@linux.vnet.ibm.com> References: <1375943171-1063-1-git-send-email-pingfank@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4001:c03::231 Cc: Paolo Bonzini , mdroth , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v1 1/5] util: introduce gsource event abstraction 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 Introduce struct EventsGSource. It will ease the usage of GSource associated with a group of files, which are dynamically allocated and release, ex, slirp. Signed-off-by: Liu Ping Fan --- util/Makefile.objs | 1 + util/event_gsource.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ util/event_gsource.h | 37 +++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 util/event_gsource.c create mode 100644 util/event_gsource.h diff --git a/util/Makefile.objs b/util/Makefile.objs index dc72ab0..eec55bd 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -11,3 +11,4 @@ util-obj-y += iov.o aes.o qemu-config.o qemu-sockets.o uri.o notify.o util-obj-y += qemu-option.o qemu-progress.o util-obj-y += hexdump.o util-obj-y += crc32c.o +util-obj-y += event_gsource.o diff --git a/util/event_gsource.c b/util/event_gsource.c new file mode 100644 index 0000000..4b9fa89 --- /dev/null +++ b/util/event_gsource.c @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2013 IBM + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; under version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include "event_gsource.h" +#include "qemu/bitops.h" + +GPollFD *events_source_add_pollfd(EventsGSource *src, int fd) +{ + GPollFD *retfd; + + retfd = g_slice_alloc(sizeof(GPollFD)); + retfd->events = 0; + retfd->fd = fd; + src->pollfds_list = g_list_append(src->pollfds_list, retfd); + if (fd >= 0) { + g_source_add_poll(&src->source, retfd); + } + + return retfd; +} + +void events_source_remove_pollfd(EventsGSource *src, GPollFD *pollfd) +{ + g_source_remove_poll(&src->source, pollfd); + src->pollfds_list = g_list_remove(src->pollfds_list, pollfd); + g_slice_free(GPollFD, pollfd); +} + +static gboolean events_source_check(GSource *src) +{ + EventsGSource *nsrc = (EventsGSource *)src; + GList *cur; + GPollFD *gfd; + + cur = nsrc->pollfds_list; + while (cur) { + gfd = cur->data; + if (gfd->fd >= 0 && (gfd->revents & gfd->events)) { + return true; + } + cur = g_list_next(cur); + } + + return false; +} + +static gboolean events_source_dispatch(GSource *src, GSourceFunc cb, + gpointer data) +{ + gboolean ret = false; + + if (cb) { + ret = cb(data); + } + return ret; +} + +EventsGSource *events_source_new(GPrepare prepare, GSourceFunc dispatch_cb, + void *opaque) +{ + EventsGSource *src; + GSourceFuncs *gfuncs = g_new0(GSourceFuncs, 1); + gfuncs->prepare = prepare; + gfuncs->check = events_source_check, + gfuncs->dispatch = events_source_dispatch, + + src = (EventsGSource *)g_source_new(gfuncs, sizeof(EventsGSource)); + src->gfuncs = gfuncs; + src->pollfds_list = NULL; + src->opaque = opaque; + g_source_set_callback(&src->source, dispatch_cb, src, NULL); + + return src; +} + +void events_source_release(EventsGSource *src) +{ + assert(!src->pollfds_list); + g_free(src->gfuncs); + g_source_destroy(&src->source); +} diff --git a/util/event_gsource.h b/util/event_gsource.h new file mode 100644 index 0000000..8755952 --- /dev/null +++ b/util/event_gsource.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2013 IBM + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; under version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#ifndef EVENT_GSOURCE_H +#define EVENT_GSOURCE_H +#include "qemu-common.h" + +typedef gboolean (*GPrepare)(GSource *source, gint *timeout_); + +/* multi fd drive GSource*/ +typedef struct EventsGSource { + GSource source; + /* a group of GPollFD which dynamically join or leave the GSource */ + GList *pollfds_list; + GSourceFuncs *gfuncs; + void *opaque; +} EventsGSource; + +EventsGSource *events_source_new(GPrepare prepare, GSourceFunc dispatch_cb, + void *opaque); +void events_source_release(EventsGSource *src); +GPollFD *events_source_add_pollfd(EventsGSource *src, int fd); +void events_source_remove_pollfd(EventsGSource *src, GPollFD *pollfd); +#endif