[glib/glib-2-28] gthread: avoid locking in _get_mutex_impl



commit 32b57792b1968c2c881649a457e545e91cacbedf
Author: Wim Taymans <wim taymans collabora co uk>
Date:   Wed Sep 16 17:39:48 2009 +0200

    gthread: avoid locking in _get_mutex_impl
    
    When getting the mutex implementation of a static mutex, avoid taking the global
    lock every time but only take the lock when there was no mutex and we need to
    create one.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=599954

 glib/gthread.c |   23 +++++++++++++++++------
 1 files changed, 17 insertions(+), 6 deletions(-)
---
diff --git a/glib/gthread.c b/glib/gthread.c
index 2261f9b..380f4bc 100644
--- a/glib/gthread.c
+++ b/glib/gthread.c
@@ -1268,19 +1268,30 @@ g_static_mutex_init (GStaticMutex *mutex)
 GMutex *
 g_static_mutex_get_mutex_impl (GMutex** mutex)
 {
+  GMutex *result;
+
   if (!g_thread_supported ())
     return NULL;
 
-  g_assert (g_once_mutex);
+  result = g_atomic_pointer_get (mutex);
 
-  g_mutex_lock (g_once_mutex);
+  if (!result)
+    {
+      g_assert (g_once_mutex);
 
-  if (!(*mutex))
-    g_atomic_pointer_set (mutex, g_mutex_new());
+      g_mutex_lock (g_once_mutex);
 
-  g_mutex_unlock (g_once_mutex);
+      result = *mutex;
+      if (!result)
+        {
+          result = g_mutex_new ();
+          g_atomic_pointer_set (mutex, result);
+        }
+
+      g_mutex_unlock (g_once_mutex);
+    }
 
-  return *mutex;
+  return result;
 }
 
 /* IMPLEMENTATION NOTE:



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