From c7df4317bd965006f7d98186b239b6c0618c6217 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Thu, 10 Sep 2020 11:00:35 +0200 Subject: [PATCH] Add g_autoptr helper for pushing a thread default main context This happens in a bunch of places, and currently each time it does we have to use "goto out" style cleanups, which just isn't looking very nice. --- src/libotutil/otutil.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/libotutil/otutil.h b/src/libotutil/otutil.h index cd312365..7db7270d 100644 --- a/src/libotutil/otutil.h +++ b/src/libotutil/otutil.h @@ -52,6 +52,31 @@ #define ot_journal_print(...) {} #endif +typedef GMainContext GMainContextPopDefault; +static inline void +_ostree_main_context_pop_default_destroy (void *p) +{ + GMainContext *main_context = p; + + if (main_context) + { + g_main_context_pop_thread_default (main_context); + g_main_context_unref (main_context); + } +} + +static inline GMainContextPopDefault * +_ostree_main_context_new_default (void) +{ + GMainContext *main_context = g_main_context_new (); + + g_main_context_push_thread_default (main_context); + return main_context; +} + +G_DEFINE_AUTOPTR_CLEANUP_FUNC (GMainContextPopDefault, _ostree_main_context_pop_default_destroy) + + #include #include #include