Patchwork [58/66] qom: Use atomics for object refcounting

login
register
mail settings
Submitter Paolo Bonzini
Date July 4, 2013, 3:13 p.m.
Message ID <1372950842-32422-59-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/256947/
State New
Headers show

Comments

Paolo Bonzini - July 4, 2013, 3:13 p.m.
From: Jan Kiszka <jan.kiszka@siemens.com>

Object reference counts will soon be changed outside the BQL. So we need
to use atomics in object_ref/unref.

Based on a patch by Liu Ping Fan.

Signed-off-by: Liu Ping Fan <qemulist@gmail.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 qom/object.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Patch

diff --git a/qom/object.c b/qom/object.c
index 803b94b..cbd7e86 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++;
+     atomic_inc(&obj->ref);
 }
 
 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 (atomic_fetch_dec(&obj->ref) == 1) {
         object_finalize(obj);
     }
 }