[glib: 5/10] cancellable: Use more atomic exchanges operations




commit 576e5f2f875125c4ee06505790da22a2878c9198
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Mon Jun 20 18:57:36 2022 +0200

    cancellable: Use more atomic exchanges operations
    
    We used to do get and set atomic operations pair, but these may be
    unsafe in some cases as threads may rely on data that is changed in
    in between them, however this is not a problem if we do exchange the
    pointers.
    
    So just use exchange ops, in this way we can avoid lock/unlock mutex
    dances

 gio/gcancellable.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
---
diff --git a/gio/gcancellable.c b/gio/gcancellable.c
index 64755206be..13a891a696 100644
--- a/gio/gcancellable.c
+++ b/gio/gcancellable.c
@@ -273,12 +273,10 @@ g_cancellable_reset (GCancellable *cancellable)
       g_cond_wait (&cancellable_cond, &cancellable_mutex);
     }
 
-  if (g_atomic_int_get (&priv->cancelled))
+  if (g_atomic_int_exchange (&priv->cancelled, FALSE))
     {
       if (priv->wakeup)
         GLIB_PRIVATE_CALL (g_wakeup_acknowledge) (priv->wakeup);
-
-      g_atomic_int_set (&priv->cancelled, FALSE);
     }
 
   g_mutex_unlock (&cancellable_mutex);
@@ -497,13 +495,12 @@ g_cancellable_cancel (GCancellable *cancellable)
 
   g_mutex_lock (&cancellable_mutex);
 
-  if (g_atomic_int_get (&priv->cancelled))
+  if (g_atomic_int_exchange (&priv->cancelled, TRUE))
     {
       g_mutex_unlock (&cancellable_mutex);
       return;
     }
 
-  g_atomic_int_set (&priv->cancelled, TRUE);
   priv->cancelled_running = TRUE;
 
   if (priv->wakeup)


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