Patchwork [v7,1/7] qom: apply atomic on object's refcount

login
register
mail settings
Submitter pingfan liu
Date Nov. 25, 2012, 2:02 a.m.
Message ID <1353808984-22368-2-git-send-email-qemulist@gmail.com>
Download mbox | patch
Permalink /patch/201504/
State New
Headers show

Comments

pingfan liu - Nov. 25, 2012, 2:02 a.m.
From: Liu Ping Fan <pingfank@linux.vnet.ibm.com>

Signed-off-by: Liu Ping Fan <pingfank@linux.vnet.ibm.com>
---
 qom/object.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
Richard Henderson - Nov. 28, 2012, 5:16 p.m.
On 11/24/2012 06:02 PM, Liu Ping Fan wrote:
> -    obj->ref--;
>  
>      /* parent always holds a reference to its children */
> -    if (obj->ref == 0) {
> +    if (__sync_fetch_and_sub(&obj->ref, 1) == 1) {

  if (__sync_sub_and_fetch(&obj->ref, 1) == 0)


r~
pingfan liu - Nov. 29, 2012, 8:35 a.m.
On Thu, Nov 29, 2012 at 1:16 AM, Richard Henderson <rth@twiddle.net> wrote:
> On 11/24/2012 06:02 PM, Liu Ping Fan wrote:
>> -    obj->ref--;
>>
>>      /* parent always holds a reference to its children */
>> -    if (obj->ref == 0) {
>> +    if (__sync_fetch_and_sub(&obj->ref, 1) == 1) {
>
>   if (__sync_sub_and_fetch(&obj->ref, 1) == 0)
>
Applied, thanks
>
> r~

Patch

diff --git a/qom/object.c b/qom/object.c
index e3e9242..1a697b3 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -600,16 +600,15 @@  GSList *object_class_get_list(const char *implements_type,
 
 void object_ref(Object *obj)
 {
-    obj->ref++;
+     __sync_fetch_and_add(&obj->ref, 1);
 }
 
 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 (__sync_fetch_and_sub(&obj->ref, 1) == 1) {
         object_finalize(obj);
     }
 }