From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37754) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uueeq-0000PF-Ri for qemu-devel@nongnu.org; Thu, 04 Jul 2013 04:12:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UueX6-0000mn-Of for qemu-devel@nongnu.org; Thu, 04 Jul 2013 04:04:55 -0400 Received: from mail-wg0-x22a.google.com ([2a00:1450:400c:c00::22a]:55735) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UueRb-0006zU-Ka for qemu-devel@nongnu.org; Thu, 04 Jul 2013 03:59:11 -0400 Received: by mail-wg0-f42.google.com with SMTP id z11so6185422wgg.1 for ; Thu, 04 Jul 2013 00:59:10 -0700 (PDT) Sender: Paolo Bonzini Message-ID: <51D52B45.7060905@redhat.com> Date: Thu, 04 Jul 2013 09:59:01 +0200 From: Paolo Bonzini MIME-Version: 1.0 References: <51D29F27.9040706@siemens.com> <87bo6lqarq.fsf@codemonkey.ws> <51D2F2D5.90801@redhat.com> <8761wsc426.fsf@codemonkey.ws> In-Reply-To: <8761wsc426.fsf@codemonkey.ws> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH] qom: Use atomics for object refcounting List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Anthony Liguori Cc: Jan Kiszka , Liu Ping Fan , qemu-devel Il 02/07/2013 18:36, Anthony Liguori ha scritto: > Paolo Bonzini writes: > >> Il 02/07/2013 16:47, Anthony Liguori ha scritto: >>> Jan Kiszka writes: >>> >>>> Objects can soon be referenced/dereference outside the BQL. So we need >>>> to use atomics in object_ref/unref. >>>> >>>> Based on patch by Liu Ping Fan. >>>> >>>> Signed-off-by: Jan Kiszka >>>> --- >>>> qom/object.c | 5 ++--- >>>> 1 files changed, 2 insertions(+), 3 deletions(-) >>>> >>>> diff --git a/qom/object.c b/qom/object.c >>>> index 803b94b..a76a30b 100644 >>>> --- a/qom/object.c >>>> +++ b/qom/object.c >>>> @@ -683,16 +683,15 @@ GSList *object_class_get_list(const char *implements_type, >>>> >>>> void object_ref(Object *obj) >>>> { >>>> - obj->ref++; >>>> + __sync_fetch_and_add(&obj->ref, 1); >>>> } >>>> >>>> void object_unref(Object *obj) >>>> { >>>> g_assert(obj->ref > 0); >>>> - obj->ref--; >>>> >>>> /* parent always holds a reference to its children */ >>>> - if (obj->ref == 0) { >>>> + if (__sync_sub_and_fetch(&obj->ref, 1) == 0) { >>>> object_finalize(obj); >>>> } >>>> } >>> >>> Should we introduce something akin to kref now that referencing counting >>> has gotten fancy? >> >> I'm not a big fan of kref (it seems _too_ thin a wrapper to me, i.e. it >> doesn't really wrap enough to be useful), but I wouldn't oppose it if >> someone else does it. > > I had honestly hoped Object was light enough to be used for this > purpose. What do you think? We should make it more robust against objects that are not in the QOM composition tree (adding/removing the "child" property is relatively slow). As things stand, QOM is definitely too slow for something like SCSIRequest. In the long term, it is definitely nice to use Object more. But if we really had to abstract things, for now I'd just do #define atomic_ref(x) atomic_inc(x) #define atomic_unref_test_zero(x) (atomic_fetch_dec(x) == 1) or something like that. Paolo