* [Qemu-devel] [PATCH v2 1/2] refcnt: introduce Qref to abstract the refcnt interface
@ 2013-07-16 4:36 Liu Ping Fan
2013-07-16 4:36 ` [Qemu-devel] [PATCH v2 2/2] object: Object apply the refcnt interface by Qref Liu Ping Fan
0 siblings, 1 reply; 2+ messages in thread
From: Liu Ping Fan @ 2013-07-16 4:36 UTC (permalink / raw)
To: qemu-devel; +Cc: Paolo Bonzini, Richard Henderson
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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [Qemu-devel] [PATCH v2 2/2] object: Object apply the refcnt interface by Qref
2013-07-16 4:36 [Qemu-devel] [PATCH v2 1/2] refcnt: introduce Qref to abstract the refcnt interface Liu Ping Fan
@ 2013-07-16 4:36 ` Liu Ping Fan
0 siblings, 0 replies; 2+ messages in thread
From: Liu Ping Fan @ 2013-07-16 4:36 UTC (permalink / raw)
To: qemu-devel; +Cc: Paolo Bonzini, Richard Henderson
Signed-off-by: Liu Ping Fan <pingfank@linux.vnet.ibm.com>
---
include/qom/object.h | 3 ++-
qom/object.c | 15 +++++----------
2 files changed, 7 insertions(+), 11 deletions(-)
diff --git a/include/qom/object.h b/include/qom/object.h
index 23fc048..6331972 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/refcnt.h"
struct Visitor;
struct Error;
@@ -384,7 +385,7 @@ struct Object
ObjectClass *class;
ObjectFree *free;
QTAILQ_HEAD(, ObjectProperty) properties;
- uint32_t ref;
+ Qref ref;
Object *parent;
};
diff --git a/qom/object.c b/qom/object.c
index b2479d1..e7e5c26 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -388,15 +388,15 @@ static void object_deinit(Object *obj, TypeImpl *type)
}
}
-static void object_finalize(void *data)
+static void object_finalize(Qref *qref)
{
- Object *obj = data;
+ Object *obj = container_of(qref, Object, ref);
TypeImpl *ti = obj->class->type;
object_deinit(obj, ti);
object_property_del_all(obj);
- g_assert(obj->ref == 0);
+ g_assert(qref->count == 0);
if (obj->free) {
obj->free(obj);
}
@@ -683,17 +683,12 @@ GSList *object_class_get_list(const char *implements_type,
void object_ref(Object *obj)
{
- atomic_inc(&obj->ref);
+ qref_inc(&obj->ref);
}
void object_unref(Object *obj)
{
- g_assert(obj->ref > 0);
-
- /* parent always holds a reference to its children */
- if (atomic_fetch_dec(&obj->ref) == 1) {
- object_finalize(obj);
- }
+ qref_dec(&obj->ref, object_finalize);
}
void object_property_add(Object *obj, const char *name, const char *type,
--
1.8.1.4
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2013-07-16 4:39 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-07-16 4:36 [Qemu-devel] [PATCH v2 1/2] refcnt: introduce Qref to abstract the refcnt interface Liu Ping Fan
2013-07-16 4:36 ` [Qemu-devel] [PATCH v2 2/2] object: Object apply the refcnt interface by Qref Liu Ping Fan
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).