[gnome-builder] app: add helper to get application main thread



commit 0922ebafb0c0aa1d8da7654901c065a4fd791253
Author: Christian Hergert <chergert redhat com>
Date:   Fri Aug 26 13:05:00 2016 -0700

    app: add helper to get application main thread
    
    This can be useful for assertions where we want to make sure we are being
    called from the main thread. Helpful for tracking down weird errors.

 libide/application/ide-application.c |   19 +++++++++++++++++++
 libide/application/ide-application.h |    2 ++
 2 files changed, 21 insertions(+), 0 deletions(-)
---
diff --git a/libide/application/ide-application.c b/libide/application/ide-application.c
index 39b503e..2bb6178 100644
--- a/libide/application/ide-application.c
+++ b/libide/application/ide-application.c
@@ -49,6 +49,8 @@
 
 G_DEFINE_TYPE (IdeApplication, ide_application, GTK_TYPE_APPLICATION)
 
+static GThread *main_thread;
+
 static void
 ide_application_make_skeleton_dirs (IdeApplication *self)
 {
@@ -443,6 +445,8 @@ ide_application_class_init (IdeApplicationClass *klass)
   g_app_class->open = ide_application_open;
   g_app_class->startup = ide_application_startup;
   g_app_class->shutdown = ide_application_shutdown;
+
+  main_thread = g_thread_self ();
 }
 
 static void
@@ -757,3 +761,18 @@ ide_application_get_disable_theme_tracking (IdeApplication *self)
 
   return self->disable_theme_tracking;
 }
+
+/**
+ * ide_application_get_main_thread:
+ *
+ * This function returns the thread-id of the main thread for the applicaiton.
+ * This is only really useful to determine if you are in the main UI thread.
+ * This is used by IDE_IS_MAIN_THREAD for assertion checks.
+ *
+ * Returns: (transfer none): A #GThread
+ */
+GThread *
+ide_application_get_main_thread (void)
+{
+  return main_thread;
+}
diff --git a/libide/application/ide-application.h b/libide/application/ide-application.h
index d13e0b2..607ee0a 100644
--- a/libide/application/ide-application.h
+++ b/libide/application/ide-application.h
@@ -27,6 +27,7 @@ G_BEGIN_DECLS
 
 #define IDE_TYPE_APPLICATION    (ide_application_get_type())
 #define IDE_APPLICATION_DEFAULT (IDE_APPLICATION (g_application_get_default()))
+#define IDE_IS_MAIN_THREAD()    (g_thread_self() == ide_application_get_main_thread())
 
 G_DECLARE_FINAL_TYPE (IdeApplication, ide_application, IDE, APPLICATION, GtkApplication)
 
@@ -38,6 +39,7 @@ typedef enum
   IDE_APPLICATION_MODE_TESTS,
 } IdeApplicationMode;
 
+GThread            *ide_application_get_main_thread      (void);
 IdeApplicationMode  ide_application_get_mode             (IdeApplication       *self);
 IdeApplication     *ide_application_new                  (void);
 GDateTime          *ide_application_get_started_at       (IdeApplication       *self);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]