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 v2 1/2] refcnt: introduce Qref to abstract the refcnt interface
Date: Tue, 16 Jul 2013 12:36:29 +0800	[thread overview]
Message-ID: <1373949390-3642-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>
---
v2:
    put Qref to a dedicated file.
    rename qref_get/_put as qref_inc/_dec
---
 include/qemu/refcnt.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
 create mode 100644 include/qemu/refcnt.h

diff --git a/include/qemu/refcnt.h b/include/qemu/refcnt.h
new file mode 100644
index 0000000..815534a
--- /dev/null
+++ b/include/qemu/refcnt.h
@@ -0,0 +1,57 @@
+/*
+ * refcount management routine
+ *
+ * Copyright IBM, Corp. 2013
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef QEMU_REFCNT_H
+#define QEMU_REFCNT_H
+
+#include "qemu/osdep.h"
+
+/*
+ * Qref defines the interface for thread-safe refcount management.
+ * As to atomic ops, it falls back on gcc implementation, and do some
+ * optimization if gcc supports C11 relaxed memory model, otherwise
+ * just using seq_cst memory model.
+ */
+typedef struct Qref {
+    int32_t count;
+} Qref;
+
+static inline void qref_inc(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 *);
+
+/* When refcnt comes to zero, @release will be called */
+static inline void qref_dec(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-16  4:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-16  4:36 Liu Ping Fan [this message]
2013-07-16  4:36 ` [Qemu-devel] [PATCH v2 2/2] object: Object apply the refcnt interface by Qref Liu Ping Fan

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=1373949390-3642-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).