[glib/gobject-performance] Make all accesses of Node->ref_count atomic



commit cb9f4c1aa0f5e55a9872b83645dec068ba043a89
Author: Edward Hervey <bilboed bilboed com>
Date:   Thu Sep 24 11:38:49 2009 +0200

    Make all accesses of Node->ref_count atomic
    
    This does not change any locking behavior at all, it just replaces
    simple getters/setters of the variable with atomic versions.
    
    The ref_count variable was kept as unsigned, even though that requires
    casting for all operations, to mirror GObject->refcount.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=585375

 gobject/gtype.c |   11 +++++------
 1 files changed, 5 insertions(+), 6 deletions(-)
---
diff --git a/gobject/gtype.c b/gobject/gtype.c
index e7800fe..e83fa9d 100644
--- a/gobject/gtype.c
+++ b/gobject/gtype.c
@@ -252,7 +252,7 @@ struct _TypeNode
 #define NODE_PARENT_TYPE(node)			(node->supers[1])
 #define NODE_FUNDAMENTAL_TYPE(node)		(node->supers[node->n_supers])
 #define NODE_NAME(node)				(g_quark_to_string (node->qname))
-#define NODE_REFCOUNT(node)                     (node->ref_count)
+#define NODE_REFCOUNT(node)                     ((guint) g_atomic_int_get ((int *) &(node)->ref_count))
 #define	NODE_IS_IFACE(node)			(NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
 #define	CLASSED_NODE_IFACES_ENTRIES(node)	(&(node)->_prot.iface_entries)
 #define	CLASSED_NODE_IFACES_ENTRIES_LOCKED(node)(G_ATOMIC_ARRAY_GET_LOCKED(CLASSED_NODE_IFACES_ENTRIES((node)), IFaceEntries))
@@ -1157,7 +1157,7 @@ type_data_make_W (TypeNode              *node,
   
   g_assert (node->data->common.value_table != NULL); /* paranoid */
 
-  node->ref_count = 1;
+  g_atomic_int_set ((int *) &node->ref_count, 1);
 }
 
 static inline void
@@ -1197,7 +1197,7 @@ type_data_ref_Wm (TypeNode *node)
     {
       g_assert (NODE_REFCOUNT (node) > 0);
       
-      node->ref_count += 1;
+      g_atomic_int_inc ((int *) &node->ref_count);
     }
 }
 
@@ -2285,8 +2285,7 @@ type_data_last_unref_Wm (TypeNode *node,
     }
   
   /* may have been re-referenced meanwhile */
-  node->ref_count -= 1;
-  if (NODE_REFCOUNT (node) == 0)
+  if (g_atomic_int_dec_and_test ((int *) &node->ref_count))
     {
       GType ptype = NODE_PARENT_TYPE (node);
       TypeData *tdata;
@@ -2348,7 +2347,7 @@ type_data_unref_U (TypeNode *node,
   G_WRITE_LOCK (&type_rw_lock);
   g_assert (node->data && NODE_REFCOUNT (node) > 0);
   if (NODE_REFCOUNT (node) > 1)
-    node->ref_count -= 1;
+    g_atomic_int_add ((int *) &node->ref_count, -1);
   else
     {
       if (!node->plugin)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]