From 6b18fc252d1e31399b9aef3049a78d9a9b129e00 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 21 Jan 2021 18:37:38 +0100 Subject: [PATCH] shared: add nm_g_{idle,timeout}_add_source() helpers We have g_idle_add() and g_timeout_add(). But these return those odd guint source ids. That is totally pointless. The only potential benefit is that a guint is only 4 bytes while a pointer is 8 bytes (on 64 bit systems). Otherwise, it seems always preferable to have an actual GSource instance instead of an integer. It also saves the overhead in g_source_remove() which first needs to do a hash lookup to find the GSource. A GSource instance would theoretically work with multiple GMainContext instances, while g_source_remove() only works wit g_main_context_default(). On the other hand we have helper API like nm_g_idle_source_new() and nm_g_timeout_source_new(), which is fully flexible and sensible because it returns a reference to the GSource instance. However, it is a bit verbose to use in the common case. Add helper functions that simplify the use and are conceptionally similar to g_{idle,timeout}_add() (hence the name). --- shared/nm-glib-aux/nm-shared-utils.h | 53 ++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h index 8f0d50c9f5..c432b125b6 100644 --- a/shared/nm-glib-aux/nm-shared-utils.h +++ b/shared/nm-glib-aux/nm-shared-utils.h @@ -1364,6 +1364,59 @@ nm_g_source_attach(GSource *source, GMainContext *context) return source; } +static inline GSource * +nm_g_idle_add_source(GSourceFunc func, gpointer user_data) +{ + /* G convenience function to attach a new timeout source to the default GMainContext. + * In that sense it's very similar to g_idle_add() except that it returns a + * reference to the new source. */ + return nm_g_source_attach(nm_g_idle_source_new(G_PRIORITY_DEFAULT, func, user_data, NULL), + NULL); +} + +static inline GSource * +nm_g_timeout_add_source(guint timeout_msec, GSourceFunc func, gpointer user_data) +{ + /* G convenience function to attach a new timeout source to the default GMainContext. + * In that sense it's very similar to g_timeout_add() except that it returns a + * reference to the new source. */ + return nm_g_source_attach( + nm_g_timeout_source_new(timeout_msec, G_PRIORITY_DEFAULT, func, user_data, NULL), + NULL); +} + +static inline GSource * +nm_g_timeout_add_source_seconds(guint timeout_sec, GSourceFunc func, gpointer user_data) +{ + /* G convenience function to attach a new timeout source to the default GMainContext. + * In that sense it's very similar to g_timeout_add_seconds() except that it returns a + * reference to the new source. */ + return nm_g_source_attach( + nm_g_timeout_source_new_seconds(timeout_sec, G_PRIORITY_DEFAULT, func, user_data, NULL), + NULL); +} + +static inline GSource * +nm_g_timeout_add_source_approx(guint timeout_msec, + guint timeout_sec_threshold, + GSourceFunc func, + gpointer user_data) +{ + GSource *source; + + /* If timeout_msec is larger or equal than a threshold, then we use g_timeout_source_new_seconds() + * instead. */ + if (timeout_msec / 1000u >= timeout_sec_threshold) + source = nm_g_timeout_source_new_seconds(timeout_msec / 1000u, + G_PRIORITY_DEFAULT, + func, + user_data, + NULL); + else + source = nm_g_timeout_source_new(timeout_msec, G_PRIORITY_DEFAULT, func, user_data, NULL); + return nm_g_source_attach(source, NULL); +} + NM_AUTO_DEFINE_FCN0(GMainContext *, _nm_auto_unref_gmaincontext, g_main_context_unref); #define nm_auto_unref_gmaincontext nm_auto(_nm_auto_unref_gmaincontext)