From a99ac7ccd84c22c1a30c29bb4f1ce50d211d6aaa Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 22 Jul 2021 18:31:35 +0200 Subject: [PATCH] glib-aux: add nm_g_idle_add() g_idle_add() is discouraged, and the checkpatch.pl script warns about it. Sometimes there is a legitimate use of it, when you want to always schedule an idle action (without intent to cancel or track it). That makes more sense for g_idle_add() than it does for g_timeout_add(), because a timeout really should be tracked and cancelled if necessary. Add a wrapper to rename the legitimate uses. This way, we can avoid the checkpatch.pl warnings, and can grep for the remaining illegitimate uses. --- contrib/scripts/checkpatch.pl | 2 +- src/libnm-glib-aux/nm-shared-utils.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/contrib/scripts/checkpatch.pl b/contrib/scripts/checkpatch.pl index c535fd0a93..d1bc6c69c2 100755 --- a/contrib/scripts/checkpatch.pl +++ b/contrib/scripts/checkpatch.pl @@ -191,7 +191,7 @@ complain ("This gtk-doc annotation looks wrong") if $line =~ /\*.*\( *(transfer- complain ("Prefer nm_assert() or g_return*() to g_assert*()") if $line =~ /g_assert/ and (not $filename =~ /\/tests\//) and (not $filename =~ /\/nm-test-/); complain ("Use gs_free_error with GError variables") if $line =~ /\bgs_free\b +GError *\*/; complain ("Don't use strcmp/g_strcmp0 unless you need to sort. Consider nm_streq()/nm_streq0(),NM_IN_STRSET() for testing equality") if $line =~ /\b(strcmp|g_strcmp0)\b/; -complain ("Don't use API that uses the numeric source id. Instead, use GSource and API like nm_g_idle_add_source(), nm_clear_g_source_inst(), etc.") if $line =~ /\b(g_idle_add|g_idle_add_full|g_timeout_add|g_timeout_add_seconds|g_source_remove|nm_clear_g_source)\b/; +complain ("Don't use API that uses the numeric source id. Instead, use GSource and API like nm_g_idle_add(), nm_g_idle_add_source(), nm_clear_g_source_inst(), etc.") if $line =~ /\b(g_idle_add|g_idle_add_full|g_timeout_add|g_timeout_add_seconds|g_source_remove|nm_clear_g_source)\b/; #complain ("Use spaces instead of tabs") if $line =~ /\t/; # Further on we process stuff without comments. diff --git a/src/libnm-glib-aux/nm-shared-utils.h b/src/libnm-glib-aux/nm-shared-utils.h index e8c282d8a2..8d12f05ef5 100644 --- a/src/libnm-glib-aux/nm-shared-utils.h +++ b/src/libnm-glib-aux/nm-shared-utils.h @@ -1725,6 +1725,20 @@ nm_g_source_attach(GSource *source, GMainContext *context) return source; } +static inline void +nm_g_idle_add(GSourceFunc func, gpointer user_data) +{ + /* g_idle_add() is discouraged because it relies on the guint source ids. + * + * Usually, you would want to use nm_g_idle_add_source() which returns a GSource* + * instance. + * + * However, if you don't care to ever call g_source_remove() on the source id, then + * g_idle_add() is fine. But our checkpatch script would complain about it. nm_g_idle_add(), + * is the solution because it intentionally ignores the guint source id. */ + g_idle_add(func, user_data); +} + static inline GSource * nm_g_idle_add_source(GSourceFunc func, gpointer user_data) {