diff options
author | Thomas Haller <thaller@redhat.com> | 2019-10-07 13:22:30 +0000 |
---|---|---|
committer | Philip Withnall <philip@tecnocode.co.uk> | 2019-10-07 13:22:30 +0000 |
commit | 39dd2be5386e8e5fa9492cc7f42e658ef18a6960 (patch) | |
tree | 572b651f39fc969abe9535b6bd4963d43a471ac4 | |
parent | 038ec3de31b0aa78aaed002dbd12a51cd059b932 (diff) | |
download | glib-39dd2be5386e8e5fa9492cc7f42e658ef18a6960.tar.gz |
gmain: use atomic operation instead of GMutex to access g_main_context_default()
I think it is wasteful to use a mutex every time the default main context
is accessed. Especially, as the default main context is used all the
time.
-rw-r--r-- | glib/gmain.c | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/glib/gmain.c b/glib/gmain.c index ae86d5a2c..27a30f202 100644 --- a/glib/gmain.c +++ b/glib/gmain.c @@ -439,9 +439,6 @@ static void block_source (GSource *source); static GMainContext *glib_worker_context; -G_LOCK_DEFINE_STATIC (main_loop); -static GMainContext *default_main_context; - #ifndef G_OS_WIN32 @@ -667,34 +664,35 @@ g_main_context_new (void) /** * g_main_context_default: - * + * * Returns the global default main context. This is the main context * used for main loop functions when a main loop is not explicitly * specified, and corresponds to the "main" main loop. See also * g_main_context_get_thread_default(). - * + * * Returns: (transfer none): the global default main context. **/ GMainContext * g_main_context_default (void) { - /* Slow, but safe */ - - G_LOCK (main_loop); + static GMainContext *default_main_context; - if (!default_main_context) + if (g_once_init_enter (&default_main_context)) { - default_main_context = g_main_context_new (); + GMainContext *context; + + context = g_main_context_new (); - TRACE (GLIB_MAIN_CONTEXT_DEFAULT (default_main_context)); + TRACE (GLIB_MAIN_CONTEXT_DEFAULT (context)); #ifdef G_MAIN_POLL_DEBUG if (_g_main_poll_debug) - g_print ("default context=%p\n", default_main_context); + g_print ("default context=%p\n", context); #endif - } - G_UNLOCK (main_loop); + g_once_init_leave ((gsize *) &default_main_context, (gsize) context); + + } return default_main_context; } |