qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Liu Ping Fan <qemulist@gmail.com>
To: qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>, Richard Henderson <rth@twiddle.net>
Subject: [Qemu-devel] [PATCH 1/2] object: introduce Qref to abstract the refcnt interface
Date: Mon, 15 Jul 2013 10:49:15 +0800	[thread overview]
Message-ID: <1373856556-22272-1-git-send-email-pingfank@linux.vnet.ibm.com> (raw)

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 <pingfank@linux.vnet.ibm.com>
---
 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 <stdint.h>
 #include <stdbool.h>
 #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
-- 
1.8.1.4

             reply	other threads:[~2013-07-15  2:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-15  2:49 Liu Ping Fan [this message]
2013-07-15  2:49 ` [Qemu-devel] [PATCH 2/2] object: Object apply the refcnt interface by Qref Liu Ping Fan
2013-07-15 11:38 ` [Qemu-devel] [PATCH 1/2] object: introduce Qref to abstract the refcnt interface Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1373856556-22272-1-git-send-email-pingfank@linux.vnet.ibm.com \
    --to=qemulist@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).