summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarco Trevisan (Treviño) <mail@3v1n0.net>2023-01-25 20:09:14 +0100
committerMarco Trevisan (Treviño) <mail@3v1n0.net>2023-04-19 22:04:13 +0200
commitf8e440335ce2eb4e9d5474b4c50b06b184611baa (patch)
tree6ca63f9335eb81b4053e2a688649c95817182832
parente7c2ead41b9b5827dfb6c52f69f85be090109bc7 (diff)
downloadglib-f8e440335ce2eb4e9d5474b4c50b06b184611baa.tar.gz
gmain: Avoid locking dance in g_main_loop_run()
We do it during initialization, this may be if necessary handled by GCond in case we need to acquire the lock.
-rw-r--r--glib/gmain.c17
1 files changed, 8 insertions, 9 deletions
diff --git a/glib/gmain.c b/glib/gmain.c
index 542f0e2f5..69de75184 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -4495,13 +4495,13 @@ g_main_loop_run (GMainLoop *loop)
/* Hold a reference in case the loop is unreffed from a callback function */
g_atomic_int_inc (&loop->ref_count);
- if (!g_main_context_acquire (loop->context))
+ LOCK_CONTEXT (loop->context);
+
+ if (!g_main_context_acquire_unlocked (loop->context))
{
gboolean got_ownership = FALSE;
/* Another thread owns this context */
- LOCK_CONTEXT (loop->context);
-
g_atomic_int_set (&loop->is_running, TRUE);
while (g_atomic_int_get (&loop->is_running) && !got_ownership)
@@ -4511,17 +4511,16 @@ g_main_loop_run (GMainLoop *loop)
if (!g_atomic_int_get (&loop->is_running))
{
- UNLOCK_CONTEXT (loop->context);
if (got_ownership)
- g_main_context_release (loop->context);
+ g_main_context_release_unlocked (loop->context);
+
+ UNLOCK_CONTEXT (loop->context);
g_main_loop_unref (loop);
return;
}
g_assert (got_ownership);
}
- else
- LOCK_CONTEXT (loop->context);
if (loop->context->in_check_or_prepare)
{
@@ -4535,10 +4534,10 @@ g_main_loop_run (GMainLoop *loop)
while (g_atomic_int_get (&loop->is_running))
g_main_context_iterate_unlocked (loop->context, TRUE, TRUE, self);
+ g_main_context_release_unlocked (loop->context);
+
UNLOCK_CONTEXT (loop->context);
- g_main_context_release (loop->context);
-
g_main_loop_unref (loop);
}