diff options
author | Wim Taymans <wim.taymans@collabora.co.uk> | 2009-09-16 17:39:48 +0200 |
---|---|---|
committer | Edward Hervey <bilboed@bilboed.com> | 2010-10-08 16:54:26 +0200 |
commit | 1542535c068ad162f1dcf269b8f55f91cd818691 (patch) | |
tree | b60e480a6539a6fc0c61b76c97420106b2300b6d | |
parent | 6b8d0a034c6166f22ec64aae39ad2c8873f1aa00 (diff) |
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.
-rw-r--r-- | glib/gthread.c | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/glib/gthread.c b/glib/gthread.c index 638006a04..6ae36110b 100644 --- a/glib/gthread.c +++ b/glib/gthread.c @@ -1271,19 +1271,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: |