From 2c322f2a650bc60dcbb254746352af420d515d2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Tue, 21 Jun 2022 05:01:55 +0200 Subject: [PATCH] gmain: Do atomic addition before checking the old value on ref So we avoid working on a value that is not been updated yet. --- glib/gmain.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/glib/gmain.c b/glib/gmain.c index a0ade8acb..6571fa239 100644 --- a/glib/gmain.c +++ b/glib/gmain.c @@ -551,10 +551,12 @@ GSourceFuncs g_idle_funcs = GMainContext * g_main_context_ref (GMainContext *context) { - g_return_val_if_fail (context != NULL, NULL); - g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL); + int old_ref_count; - g_atomic_int_inc (&context->ref_count); + g_return_val_if_fail (context != NULL, NULL); + + old_ref_count = g_atomic_int_add (&context->ref_count, 1); + g_return_val_if_fail (old_ref_count > 0, NULL); return context; }