From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48938) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uyh7B-00078z-TR for qemu-devel@nongnu.org; Mon, 15 Jul 2013 07:38:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Uyh7A-0001lG-NT for qemu-devel@nongnu.org; Mon, 15 Jul 2013 07:38:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:3711) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uyh7A-0001l8-Er for qemu-devel@nongnu.org; Mon, 15 Jul 2013 07:38:48 -0400 Message-ID: <51E3DF3B.5040409@redhat.com> Date: Mon, 15 Jul 2013 13:38:35 +0200 From: Paolo Bonzini MIME-Version: 1.0 References: <1373856556-22272-1-git-send-email-pingfank@linux.vnet.ibm.com> In-Reply-To: <1373856556-22272-1-git-send-email-pingfank@linux.vnet.ibm.com> Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH 1/2] object: introduce Qref to abstract the refcnt interface List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Liu Ping Fan Cc: qemu-devel@nongnu.org, Richard Henderson Il 15/07/2013 04:49, Liu Ping Fan ha scritto: > Qref is similar to kref. It hides the refcnt detail and provides > a common interface. And this patch is based on the idiom of refcnt, > and adopts some optimization about memory model, which finally > falls back on gcc implementation. > > Signed-off-by: Liu Ping Fan > --- > include/qom/object.h | 34 ++++++++++++++++++++++++++++++++++ > 1 file changed, 34 insertions(+) > > diff --git a/include/qom/object.h b/include/qom/object.h > index 23fc048..2e165fb 100644 > --- a/include/qom/object.h > +++ b/include/qom/object.h > @@ -18,6 +18,7 @@ > #include > #include > #include "qemu/queue.h" > +#include "qemu/osdep.h" > > struct Visitor; > struct Error; > @@ -363,6 +364,10 @@ struct ObjectClass > ObjectUnparent *unparent; > }; > > +typedef struct Qref { > + int32_t count; > +} Qref; > + > /** > * Object: > * > @@ -1133,5 +1138,34 @@ int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), > */ > Object *container_get(Object *root, const char *path); > > +static inline void qref_get(Qref *qref) > +{ > +#ifdef __ATOMIC_RELAXED > + __atomic_fetch_add(&qref->count, 1, __ATOMIC_RELAXED); > +#else > + __sync_fetch_and_add(&qref->count, 1); > +#endif > +} > + > +typedef void (*ReleaseFn)(Qref *); > > +static inline void qref_put(Qref *qref, ReleaseFn release) > +{ > + int32_t i; > + > +#ifdef __ATOMIC_RELAXED > + i = __atomic_fetch_add(&qref->count, -1, __ATOMIC_RELAXED); > + g_assert(i > 0); > + if (unlikely(i == 1)) { > + __atomic_thread_fence(__ATOMIC_SEQ_CST); > + release(qref); > + } > +#else > + i = __sync_fetch_and_add(&qref->count, -1); > + g_assert(i > 0); > + if (unlikely(i == 1)) { > + release(qref); > + } > +#endif > +} > #endif > I don't think this should depend on QOM, so include/qom/object.h is not the right place to put it. I suggested a different way in the other thread. Also, get/put is not used (except in a couple places for historical reasons) in QEMU. Paolo