summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-11-06 09:26:33 +0100
committerThomas Haller <thaller@redhat.com>2019-11-07 11:34:36 +0100
commitafa54fdfd530392c1c50d578d5366b999a60eef1 (patch)
tree2cea62441e2b39a8c97fa9a0eece38f7a0188f08
parent3b95905ae325db5927ac06ca5eae0fc47afb829f (diff)
downloadNetworkManager-afa54fdfd530392c1c50d578d5366b999a60eef1.tar.gz
shared: add nm_g_main_context_push_thread_default*() and nm_auto helper
-rw-r--r--shared/nm-glib-aux/nm-shared-utils.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h
index 2ffb972746..5f1edcb219 100644
--- a/shared/nm-glib-aux/nm-shared-utils.h
+++ b/shared/nm-glib-aux/nm-shared-utils.h
@@ -928,6 +928,42 @@ nm_g_source_destroy_and_unref (GSource *source)
NM_AUTO_DEFINE_FCN0 (GSource *, _nm_auto_destroy_and_unref_gsource, nm_g_source_destroy_and_unref);
#define nm_auto_destroy_and_unref_gsource nm_auto(_nm_auto_destroy_and_unref_gsource)
+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)
+
+static inline GMainContext *
+nm_g_main_context_push_thread_default (GMainContext *context)
+{
+ /* This function is to work together with nm_auto_pop_gmaincontext. */
+ if (G_UNLIKELY (!context))
+ context = g_main_context_default ();
+ g_main_context_push_thread_default (context);
+ return context;
+}
+
+static inline GMainContext *
+nm_g_main_context_push_thread_default_if_necessary (GMainContext *context)
+{
+ GMainContext *cur_context;
+
+ cur_context = g_main_context_get_thread_default ();
+ if (cur_context == context)
+ return NULL;
+
+ if (G_UNLIKELY (!cur_context)) {
+ cur_context = g_main_context_default ();
+ if (cur_context == context)
+ return NULL;
+ } else if (G_UNLIKELY (!context)) {
+ context = g_main_context_default ();
+ if (cur_context == context)
+ return NULL;
+ }
+
+ g_main_context_push_thread_default (context);
+ return context;
+}
+
/*****************************************************************************/
static inline int