From 5d117b021a8817950b5acca707ce015aac922cee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 19 Feb 2025 05:09:34 +0100 Subject: [PATCH] gmain: Avoid unneeded atomic read on GSource ref In g_source_ref() we can just ensure that the value we've referenced was valid, instead of checking it before, since if get to such state the GSource is broken anyways, but at least we can avoid doing an unneeded atomic read. Reason why we don't bother resetting the reference in case of failure. --- glib/gmain.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/glib/gmain.c b/glib/gmain.c index fbeeb8c41..7258eade6 100644 --- a/glib/gmain.c +++ b/glib/gmain.c @@ -2191,12 +2191,13 @@ g_source_set_name_by_id (guint tag, GSource * g_source_ref (GSource *source) { + int old_ref G_GNUC_UNUSED; g_return_val_if_fail (source != NULL, NULL); + + old_ref = g_atomic_int_add (&source->ref_count, 1); /* We allow ref_count == 0 here to allow the dispose function to resurrect * the GSource if needed */ - g_return_val_if_fail (g_atomic_int_get (&source->ref_count) >= 0, NULL); - - g_atomic_int_inc (&source->ref_count); + g_return_val_if_fail (old_ref >= 0, NULL); return source; }