gmain: Do atomic addition before checking the old value on ref

So we avoid working on a value that is not been updated yet.
This commit is contained in:
Marco Trevisan (Treviño) 2022-06-21 05:01:55 +02:00
parent bfd77693ce
commit 2c322f2a65

View File

@ -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;
}