From mboxrd@z Thu Jan 1 00:00:00 1970 From: Liu Ping Fan Subject: [PATCH 02/15] qom: using atomic ops to re-implement object_ref Date: Wed, 8 Aug 2012 14:25:43 +0800 Message-ID: <1344407156-25562-3-git-send-email-qemulist@gmail.com> References: <1344407156-25562-1-git-send-email-qemulist@gmail.com> Cc: kvm@vger.kernel.org, Anthony Liguori , Avi Kivity , Jan Kiszka , Marcelo Tosatti , Stefan Hajnoczi , Paolo Bonzini , Blue Swirl , =?UTF-8?q?Andreas=20F=C3=A4rber?= , qemulist@gmail.com To: qemu-devel@nongnu.org Return-path: Received: from mail-pb0-f46.google.com ([209.85.160.46]:61001 "EHLO mail-pb0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932126Ab2HHG0O (ORCPT ); Wed, 8 Aug 2012 02:26:14 -0400 Received: by mail-pb0-f46.google.com with SMTP id rr13so950401pbb.19 for ; Tue, 07 Aug 2012 23:26:13 -0700 (PDT) In-Reply-To: <1344407156-25562-1-git-send-email-qemulist@gmail.com> Sender: kvm-owner@vger.kernel.org List-ID: From: Liu Ping Fan Signed-off-by: Liu Ping Fan --- include/qemu/object.h | 3 ++- qom/object.c | 13 +++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/include/qemu/object.h b/include/qemu/object.h index 8b17776..58db9d0 100644 --- a/include/qemu/object.h +++ b/include/qemu/object.h @@ -18,6 +18,7 @@ #include #include #include "qemu-queue.h" +#include "qemu/atomic.h" struct Visitor; struct Error; @@ -262,7 +263,7 @@ struct Object ObjectClass *class; GSList *interfaces; QTAILQ_HEAD(, ObjectProperty) properties; - uint32_t ref; + Atomic ref; Object *parent; }; diff --git a/qom/object.c b/qom/object.c index 00bb3b0..822bdb7 100644 --- a/qom/object.c +++ b/qom/object.c @@ -378,7 +378,7 @@ void object_finalize(void *data) object_deinit(obj, ti); object_property_del_all(obj); - g_assert(obj->ref == 0); + g_assert(atomic_read(&obj->ref) == 0); } Object *object_new_with_type(Type type) @@ -405,7 +405,7 @@ Object *object_new(const char *typename) void object_delete(Object *obj) { object_unref(obj); - g_assert(obj->ref == 0); + g_assert(atomic_read(&obj->ref) == 0); g_free(obj); } @@ -639,16 +639,13 @@ GSList *object_class_get_list(const char *implements_type, void object_ref(Object *obj) { - obj->ref++; + atomic_inc(&obj->ref); } void object_unref(Object *obj) { - g_assert(obj->ref > 0); - obj->ref--; - - /* parent always holds a reference to its children */ - if (obj->ref == 0) { + g_assert(atomic_read(&obj->ref) > 0); + if (atomic_dec_and_test(&obj->ref)) { object_finalize(obj); } } -- 1.7.4.4 From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:43680) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Syzih-0008C8-NR for qemu-devel@nongnu.org; Wed, 08 Aug 2012 02:26:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Syzig-0001tn-Au for qemu-devel@nongnu.org; Wed, 08 Aug 2012 02:26:15 -0400 Received: from mail-pb0-f45.google.com ([209.85.160.45]:47873) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Syzig-0001rm-4q for qemu-devel@nongnu.org; Wed, 08 Aug 2012 02:26:14 -0400 Received: by mail-pb0-f45.google.com with SMTP id rp12so493650pbb.4 for ; Tue, 07 Aug 2012 23:26:13 -0700 (PDT) From: Liu Ping Fan Date: Wed, 8 Aug 2012 14:25:43 +0800 Message-Id: <1344407156-25562-3-git-send-email-qemulist@gmail.com> In-Reply-To: <1344407156-25562-1-git-send-email-qemulist@gmail.com> References: <1344407156-25562-1-git-send-email-qemulist@gmail.com> Subject: [Qemu-devel] [PATCH 02/15] qom: using atomic ops to re-implement object_ref List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kvm@vger.kernel.org, Stefan Hajnoczi , Marcelo Tosatti , qemulist@gmail.com, Blue Swirl , Avi Kivity , Anthony Liguori , Jan Kiszka , Paolo Bonzini , =?UTF-8?q?Andreas=20F=C3=A4rber?= From: Liu Ping Fan Signed-off-by: Liu Ping Fan --- include/qemu/object.h | 3 ++- qom/object.c | 13 +++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/include/qemu/object.h b/include/qemu/object.h index 8b17776..58db9d0 100644 --- a/include/qemu/object.h +++ b/include/qemu/object.h @@ -18,6 +18,7 @@ #include #include #include "qemu-queue.h" +#include "qemu/atomic.h" struct Visitor; struct Error; @@ -262,7 +263,7 @@ struct Object ObjectClass *class; GSList *interfaces; QTAILQ_HEAD(, ObjectProperty) properties; - uint32_t ref; + Atomic ref; Object *parent; }; diff --git a/qom/object.c b/qom/object.c index 00bb3b0..822bdb7 100644 --- a/qom/object.c +++ b/qom/object.c @@ -378,7 +378,7 @@ void object_finalize(void *data) object_deinit(obj, ti); object_property_del_all(obj); - g_assert(obj->ref == 0); + g_assert(atomic_read(&obj->ref) == 0); } Object *object_new_with_type(Type type) @@ -405,7 +405,7 @@ Object *object_new(const char *typename) void object_delete(Object *obj) { object_unref(obj); - g_assert(obj->ref == 0); + g_assert(atomic_read(&obj->ref) == 0); g_free(obj); } @@ -639,16 +639,13 @@ GSList *object_class_get_list(const char *implements_type, void object_ref(Object *obj) { - obj->ref++; + atomic_inc(&obj->ref); } void object_unref(Object *obj) { - g_assert(obj->ref > 0); - obj->ref--; - - /* parent always holds a reference to its children */ - if (obj->ref == 0) { + g_assert(atomic_read(&obj->ref) > 0); + if (atomic_dec_and_test(&obj->ref)) { object_finalize(obj); } } -- 1.7.4.4