From c40ff42ae6a5282bed5cbe59ff86bf146ae71ffa Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 13 Nov 2019 17:46:14 +0100 Subject: [PATCH] shared: add nm_g_*_source_new() and nm_g_source_attach() helpers Small utilities to make is more convenient to create and attach GSource instances. --- shared/nm-glib-aux/nm-shared-utils.c | 51 ++++++++++++++++++++++++++++ shared/nm-glib-aux/nm-shared-utils.h | 25 ++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c index 60f1bfaf81..6b290b4bc9 100644 --- a/shared/nm-glib-aux/nm-shared-utils.c +++ b/shared/nm-glib-aux/nm-shared-utils.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "nm-errno.h" @@ -3388,3 +3389,53 @@ nm_utils_parse_debug_string (const char *string, return result; } + +/*****************************************************************************/ + +GSource * +nm_g_idle_source_new (int priority, + GSourceFunc func, + gpointer user_data, + GDestroyNotify destroy_notify) +{ + GSource *source; + + source = g_idle_source_new (); + if (priority != G_PRIORITY_DEFAULT) + g_source_set_priority (source, priority); + g_source_set_callback (source, func, user_data, destroy_notify); + return source; +} + +GSource * +nm_g_timeout_source_new (guint timeout_ms, + int priority, + GSourceFunc func, + gpointer user_data, + GDestroyNotify destroy_notify) +{ + GSource *source; + + source = g_timeout_source_new (timeout_ms); + if (priority != G_PRIORITY_DEFAULT) + g_source_set_priority (source, priority); + g_source_set_callback (source, func, user_data, destroy_notify); + return source; +} + +GSource * +nm_g_unix_signal_source_new (int signum, + int priority, + GSourceFunc handler, + gpointer user_data, + GDestroyNotify notify) +{ + GSource *source; + + source = g_unix_signal_source_new (signum); + + if (priority != G_PRIORITY_DEFAULT) + g_source_set_priority (source, priority); + g_source_set_callback (source, handler, user_data, notify); + return source; +} diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h index 5f1edcb219..d04f99f666 100644 --- a/shared/nm-glib-aux/nm-shared-utils.h +++ b/shared/nm-glib-aux/nm-shared-utils.h @@ -931,6 +931,31 @@ NM_AUTO_DEFINE_FCN0 (GSource *, _nm_auto_destroy_and_unref_gsource, nm_g_source_ NM_AUTO_DEFINE_FCN0 (GMainContext *, _nm_auto_pop_gmaincontext, g_main_context_pop_thread_default) #define nm_auto_pop_gmaincontext nm_auto (_nm_auto_pop_gmaincontext) +GSource *nm_g_idle_source_new (int priority, + GSourceFunc func, + gpointer user_data, + GDestroyNotify destroy_notify); + +GSource *nm_g_timeout_source_new (guint timeout_ms, + int priority, + GSourceFunc func, + gpointer user_data, + GDestroyNotify destroy_notify); + +GSource *nm_g_unix_signal_source_new (int signum, + int priority, + GSourceFunc handler, + gpointer user_data, + GDestroyNotify notify); + +static inline GSource * +nm_g_source_attach (GSource *source, + GMainContext *context) +{ + g_source_attach (source, context); + return source; +} + static inline GMainContext * nm_g_main_context_push_thread_default (GMainContext *context) {