gtkmain: Add gtk_get_main_thread()
authorPhilippe Normand <philn@igalia.com>
Fri, 8 Jun 2018 17:10:59 +0000 (18:10 +0100)
committerPhilippe Normand <philn@igalia.com>
Fri, 8 Jun 2018 17:41:54 +0000 (18:41 +0100)
This utility function can be useful to know which thread was initialized for
GTK+.

gtk/gtkmain.c
gtk/gtkmain.h
testsuite/gtk/main.c

index f0bbb899e8af6c2d21aae3fc99a6fc34398a0e1d..fd26bdef739a59efdf5bb631b8c798a7a4f58ce3 100644 (file)
@@ -140,6 +140,7 @@ static guint gtk_main_loop_level = 0;
 static gint pre_initialized = FALSE;
 static gint gtk_initialized = FALSE;
 static GList *current_events = NULL;
+static GThread *initialized_thread = NULL;
 
 static GSList *main_loops = NULL;      /* stack of currently executing main loops */
 
@@ -774,6 +775,8 @@ gtk_init_check (void)
   do_pre_parse_initialization ();
   do_post_parse_initialization ();
 
+  initialized_thread = g_thread_self ();
+
   ret = gdk_display_open_default () != NULL;
 
   if (ret && (gtk_get_debug_flags () & GTK_DEBUG_INTERACTIVE))
@@ -898,6 +901,20 @@ gtk_is_initialized (void)
   return gtk_initialized;
 }
 
+/**
+ * gtk_get_main_thread:
+ *
+ * Get the thread from which GTK+ was initialized.
+ *
+ * Returns: (transfer none): The #GThread initialized for GTK+, must not be freed
+ */
+GThread *
+gtk_get_main_thread (void)
+{
+  return initialized_thread;
+}
+
+
 /**
  * gtk_get_locale_direction:
  *
index 438e5985e0334966b3e75c08b52979577e0b0f60..b80da107d54923f11244226f3095169783149448 100644 (file)
@@ -81,6 +81,9 @@ gboolean gtk_init_check           (void);
 GDK_AVAILABLE_IN_ALL
 gboolean gtk_is_initialized       (void);
 
+GDK_AVAILABLE_IN_ALL
+GThread * gtk_get_main_thread     (void);
+
 #ifdef G_OS_WIN32
 
 /* Variants that are used to check for correct struct packing
index dc5e2490693113291fbc4f0420cbc45cd3dd5a1c..727ae032bbd0e15328ba15db488a724c8cde763e 100644 (file)
@@ -4,9 +4,15 @@
 static void
 test_init (void)
 {
+  GThread *self = g_thread_self ();
+
   g_assert (gtk_is_initialized () == FALSE);
+  g_assert (gtk_get_main_thread () == NULL);
+
   g_assert (gtk_init_check ());
   g_assert (gtk_is_initialized () == TRUE);
+
+  g_assert (gtk_get_main_thread () == self);
 }
 
 int