From patchwork Sun Jun 10 01:09:05 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Andreas_F=C3=A4rber?= X-Patchwork-Id: 163968 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 AA75DB6FF4 for ; Sun, 10 Jun 2012 11:31:41 +1000 (EST) Received: from localhost ([::1]:35586 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SdWfW-0005Wa-5D for incoming@patchwork.ozlabs.org; Sat, 09 Jun 2012 21:10:14 -0400 Received: from eggs.gnu.org ([208.118.235.92]:55435) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SdWek-0003TH-H5 for qemu-devel@nongnu.org; Sat, 09 Jun 2012 21:09:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SdWei-0001Zz-AT for qemu-devel@nongnu.org; Sat, 09 Jun 2012 21:09:26 -0400 Received: from cantor2.suse.de ([195.135.220.15]:52228 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SdWeh-0001Zf-P6 for qemu-devel@nongnu.org; Sat, 09 Jun 2012 21:09:24 -0400 Received: from relay2.suse.de (unknown [195.135.220.254]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx2.suse.de (Postfix) with ESMTP id B954D9A40E; Sun, 10 Jun 2012 03:09:22 +0200 (CEST) From: =?UTF-8?q?Andreas=20F=C3=A4rber?= To: qemu-devel@nongnu.org Date: Sun, 10 Jun 2012 03:09:05 +0200 Message-Id: <1339290549-3789-4-git-send-email-afaerber@suse.de> X-Mailer: git-send-email 1.7.7 In-Reply-To: <1339290549-3789-1-git-send-email-afaerber@suse.de> References: <1339290549-3789-1-git-send-email-afaerber@suse.de> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4-2.6 X-Received-From: 195.135.220.15 Cc: Paolo Bonzini , =?UTF-8?q?Andreas=20F=C3=A4rber?= , Anthony Liguori Subject: [Qemu-devel] [PATCH qom-next v2 3/7] qom: Add "realized" property 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 Since we had to move the state field from DeviceState to Object, we cannot delay the implementation of the "realized" property. The property is a trigger for two actions that propagate through the composition tree. "Realize" is called when the property becomes true, and propagates in pre-order; realize can fail if the values of the properties are not valid. "Unrealize" is called when the property becomes false, and propagates in post-order; unrealize cannot fail. Realize/unrealize is separate from reset. Reset propagation is a thorny issue of its own. We expect classes that care to implement a reset method and call it from realize or realize_children, depending on whether pre-order or post-order is more appropriate. This patch adds four methods (realize, realize_children, unrealize, unrealize_children) to ObjectClass, together with a default implementation of realize_children and unrealize_children. Signed-off-by: Paolo Bonzini [PB: Added unrealize and (un)realize_children] Signed-off-by: Andreas Färber --- include/qemu/object.h | 22 ++++++++++++ qom/object.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 0 deletions(-) diff --git a/include/qemu/object.h b/include/qemu/object.h index 0a3828d..274c839 100644 --- a/include/qemu/object.h +++ b/include/qemu/object.h @@ -239,6 +239,12 @@ struct ObjectClass { /*< private >*/ Type type; + + /*< public >*/ + void (*realize)(Object *obj, struct Error **errp); + void (*realize_children)(Object *obj, struct Error **errp); + void (*unrealize)(Object *obj); + void (*unrealize_children)(Object *obj); }; /** @@ -452,6 +458,22 @@ Object *object_new_with_type(Type type); void object_delete(Object *obj); /** + * object_realize_children: + * @obj: The object whose children should be realized. + * + * The default implementation of realize_children. + */ +void object_realize_children(Object *obj, struct Error **errp); + +/** + * object_unrealize_children: + * @obj: The object whose children should be unrealized. + * + * The default implementation of unrealize_children. + */ +void object_unrealize_children(Object *obj); + +/** * object_initialize_with_type: * @obj: A pointer to the memory to be used for the object. * @type: The type of the object to instantiate. diff --git a/qom/object.c b/qom/object.c index 8d10a27..c1f1bb4 100644 --- a/qom/object.c +++ b/qom/object.c @@ -265,6 +265,85 @@ static void object_interface_init(Object *obj, InterfaceImpl *iface) obj->interfaces = g_slist_prepend(obj->interfaces, iface_obj); } +static void object_get_realized(Object *obj, Visitor *v, void *opaque, + const char *name, Error **errp) +{ + visit_type_bool(v, &obj->realized, name, errp); +} + +static void object_unrealize(Object *obj) +{ + ObjectClass *klass = object_get_class(obj); + + if (klass->unrealize_children) { + klass->unrealize_children(obj); + } + if (obj->realized && klass->unrealize) { + klass->unrealize(obj); + } + obj->realized = false; +} + +static int object_unrealize_one(Object *obj, void *unused) +{ + object_unrealize(obj); + return 0; +} + +void object_unrealize_children(Object *obj) +{ + object_child_foreach(obj, object_unrealize_one, NULL); +} + +static void object_realize(Object *obj, Error **errp) +{ + ObjectClass *klass = object_get_class(obj); + + if (!obj->realized && klass->realize) { + klass->realize(obj, errp); + } + obj->realized = true; + if (klass->realize_children) { + klass->realize_children(obj, errp); + } +} + +static int object_realize_one(Object *obj, void *errp) +{ + Error *err = NULL; + object_realize(obj, &err); + if (err) { + error_propagate((Error **)errp, err); + return 1; + } + + return 0; +} + +void object_realize_children(Object *obj, Error **errp) +{ + object_child_foreach(obj, object_realize_one, errp); +} + +static void object_set_realized(Object *obj, Visitor *v, void *opaque, + const char *name, Error **errp) +{ + bool value; + Error *err = NULL; + + visit_type_bool(v, &value, name, &err); + if (err) { + error_propagate(errp, err); + return; + } + + if (value) { + object_realize(obj, errp); + } else { + object_unrealize(obj); + } +} + static void object_init_with_type(Object *obj, TypeImpl *ti) { int i; @@ -353,6 +432,8 @@ void object_unparent(Object *obj) static void object_deinit(Object *obj, TypeImpl *type) { + object_property_set_bool(obj, false, "realized", NULL); + if (type->instance_finalize) { type->instance_finalize(obj); } @@ -1245,6 +1326,15 @@ bool object_is_realized(Object *obj) static void object_instance_init(Object *obj) { object_property_add_str(obj, "type", qdev_get_type, NULL, NULL); + + object_property_add(obj, "realized", "bool", object_get_realized, + object_set_realized, NULL, NULL, NULL); +} + +static void object_class_init(ObjectClass *klass, void *class_data) +{ + klass->realize_children = object_realize_children; + klass->unrealize_children = object_unrealize_children; } static void register_types(void) @@ -1258,6 +1348,7 @@ static void register_types(void) static TypeInfo object_info = { .name = TYPE_OBJECT, .instance_size = sizeof(Object), + .class_init = object_class_init, .instance_init = object_instance_init, .abstract = true, };