[glib: 8/10] gmain: Do atomic addition before checking the old value on ref




commit 2c322f2a650bc60dcbb254746352af420d515d2c
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Tue Jun 21 05:01:55 2022 +0200

    gmain: Do atomic addition before checking the old value on ref
    
    So we avoid working on a value that is not been updated yet.

 glib/gmain.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
---
diff --git a/glib/gmain.c b/glib/gmain.c
index a0ade8acbb..6571fa239b 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -551,10 +551,12 @@ GSourceFuncs g_idle_funcs =
 GMainContext *
 g_main_context_ref (GMainContext *context)
 {
+  int old_ref_count;
+
   g_return_val_if_fail (context != NULL, NULL);
-  g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL); 
 
-  g_atomic_int_inc (&context->ref_count);
+  old_ref_count = g_atomic_int_add (&context->ref_count, 1);
+  g_return_val_if_fail (old_ref_count > 0, NULL);
 
   return context;
 }


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