[glib: 4/10] gatomicarray: Use atomic exchange for data value




commit 920f54e795b1577a3270c6322f547e0963b2d32a
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Mon Jun 20 18:37:25 2022 +0200

    gatomicarray: Use atomic exchange for data value
    
    We can use pointer exchange now to avoid doing two operations to switch
    to the new data pointer.
    
    Since we're asserting in case of invalid data, we can just do this check
    at later point, without involving any different behavior.
    
    This changes in the unlikely case that G_DISABLE_ASSERT is defined, as in such
    case we should undo the operation.

 gobject/gatomicarray.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
---
diff --git a/gobject/gatomicarray.c b/gobject/gatomicarray.c
index 43111e8c7c..88a07f929a 100644
--- a/gobject/gatomicarray.c
+++ b/gobject/gatomicarray.c
@@ -161,11 +161,18 @@ _g_atomic_array_update (GAtomicArray *array,
   guint8 *old;
 
   G_LOCK (array);
-  old = g_atomic_pointer_get (&array->data);
+  old = g_atomic_pointer_exchange (&array->data, new_data);
 
+#ifdef G_DISABLE_ASSERT
+  if (old && G_ATOMIC_ARRAY_DATA_SIZE (new_data) < G_ATOMIC_ARRAY_DATA_SIZE (old))
+    {
+      g_atomic_pointer_set (&array->data, old);
+      g_return_if_reached ();
+    }
+#else
   g_assert (old == NULL || G_ATOMIC_ARRAY_DATA_SIZE (old) <= G_ATOMIC_ARRAY_DATA_SIZE (new_data));
+#endif
 
-  g_atomic_pointer_set (&array->data, new_data);
   if (old)
     freelist_free (old);
   G_UNLOCK (array);


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