Add g_autoptr helper for pushing a thread default main context
authorAlexander Larsson <alexl@redhat.com>
Thu, 10 Sep 2020 09:00:35 +0000 (11:00 +0200)
committerAlexander Larsson <alexl@redhat.com>
Fri, 11 Sep 2020 10:03:28 +0000 (12:03 +0200)
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

index cd3123651287e4f763eb8b91f03ff03a038dabb1..7db7270da0e16204a22d748f7a23dcba6b6ad24f 100644 (file)
 #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 <ot-keyfile-utils.h>
 #include <ot-gio-utils.h>
 #include <ot-fs-utils.h>