[glib: 1/2] gmain: Add names to various GSources constructed in GLib



commit 208a6e815affead7ef8ed86439fcc2bb6a86d56a
Author: Philip Withnall <philip withnall collabora co uk>
Date:   Thu Jun 16 18:31:42 2016 -0400

    gmain: Add names to various GSources constructed in GLib
    
    For the purposes of debugging, it is quite useful for every GSource to
    have a name set. Ensure that any GSource we construct inside GLib has a
    name set. For GSources which are then returned to the caller, this name
    can then be overridden with something even more useful by the caller.
    
    Since this data is only used for debugging, avoid doing any allocations
    for it; just use static strings.
    
    https://gitlab.gnome.org/GNOME/glib/issues/1175

 gio/glocalfilemonitor.c |  2 ++
 gio/gtask.c             |  1 +
 glib/gmain.c            | 71 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+)
---
diff --git a/gio/glocalfilemonitor.c b/gio/glocalfilemonitor.c
index c19da3ba8..7b0a82439 100644
--- a/gio/glocalfilemonitor.c
+++ b/gio/glocalfilemonitor.c
@@ -648,6 +648,8 @@ g_file_monitor_source_new (gpointer           instance,
   source = g_source_new (&source_funcs, sizeof (GFileMonitorSource));
   fms = (GFileMonitorSource *) source;
 
+  g_source_set_name (source, "GFileMonitorSource");
+
   g_mutex_init (&fms->lock);
   fms->instance = instance;
   fms->pending_changes = g_sequence_new (pending_change_free);
diff --git a/gio/gtask.c b/gio/gtask.c
index 814ba9433..df40357dd 100644
--- a/gio/gtask.c
+++ b/gio/gtask.c
@@ -1975,6 +1975,7 @@ g_task_thread_pool_init (void)
   g_thread_pool_set_sort_function (task_pool, g_task_compare_priority, NULL);
 
   task_pool_manager = g_source_new (&trivial_source_funcs, sizeof (GSource));
+  g_source_set_name (task_pool_manager, "GTask thread pool manager");
   g_source_set_callback (task_pool_manager, task_pool_manager_timeout, NULL, NULL);
   g_source_set_ready_time (task_pool_manager, -1);
   g_source_attach (task_pool_manager,
diff --git a/glib/gmain.c b/glib/gmain.c
index 5f0b05198..1f8bdb91b 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -5239,6 +5239,68 @@ unref_unix_signal_handler_unlocked (int signum)
     }
 }
 
+/* Return a const string to avoid allocations. We lose precision in the case the
+ * @signum is unrecognised, but that’ll do. */
+static const gchar *
+signum_to_string (int signum)
+{
+  /* See `man 0P signal.h` */
+#define SIGNAL(s) \
+    case (s): \
+      return ("GUnixSignalSource: " #s);
+  switch (signum)
+    {
+    /* These signals are guaranteed to exist by POSIX. */
+    SIGNAL (SIGABRT)
+    SIGNAL (SIGFPE)
+    SIGNAL (SIGILL)
+    SIGNAL (SIGINT)
+    SIGNAL (SIGSEGV)
+    SIGNAL (SIGTERM)
+    /* Frustratingly, these are not, and hence for brevity the list is
+     * incomplete. */
+#ifdef SIGALRM
+    SIGNAL (SIGALRM)
+#endif
+#ifdef SIGCHLD
+    SIGNAL (SIGCHLD)
+#endif
+#ifdef SIGHUP
+    SIGNAL (SIGHUP)
+#endif
+#ifdef SIGKILL
+    SIGNAL (SIGKILL)
+#endif
+#ifdef SIGPIPE
+    SIGNAL (SIGPIPE)
+#endif
+#ifdef SIGQUIT
+    SIGNAL (SIGQUIT)
+#endif
+#ifdef SIGSTOP
+    SIGNAL (SIGSTOP)
+#endif
+#ifdef SIGUSR1
+    SIGNAL (SIGUSR1)
+#endif
+#ifdef SIGUSR2
+    SIGNAL (SIGUSR2)
+#endif
+#ifdef SIGPOLL
+    SIGNAL (SIGPOLL)
+#endif
+#ifdef SIGPROF
+    SIGNAL (SIGPROF)
+#endif
+#ifdef SIGTRAP
+    SIGNAL (SIGTRAP)
+#endif
+    default:
+      return "GUnixSignalSource: Unrecognized signal";
+    }
+#undef SIGNAL
+}
+
 GSource *
 _g_main_create_unix_signal_watch (int signum)
 {
@@ -5251,6 +5313,9 @@ _g_main_create_unix_signal_watch (int signum)
   unix_signal_source->signum = signum;
   unix_signal_source->pending = FALSE;
 
+  /* Set a default name on the source, just in case the caller does not. */
+  g_source_set_name (source, signum_to_string (signum));
+
   G_LOCK (unix_signal_lock);
   ref_unix_signal_handler_unlocked (signum);
   unix_signal_watches = g_slist_prepend (unix_signal_watches, unix_signal_source);
@@ -5379,6 +5444,9 @@ g_child_watch_source_new (GPid pid)
   source = g_source_new (&g_child_watch_funcs, sizeof (GChildWatchSource));
   child_watch_source = (GChildWatchSource *)source;
 
+  /* Set a default name on the source, just in case the caller does not. */
+  g_source_set_name (source, "GChildWatchSource");
+
   child_watch_source->pid = pid;
 
 #ifdef G_OS_WIN32
@@ -5565,6 +5633,9 @@ g_idle_source_new (void)
   source = g_source_new (&g_idle_funcs, sizeof (GSource));
   g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
 
+  /* Set a default name on the source, just in case the caller does not. */
+  g_source_set_name (source, "GIdleSource");
+
   return source;
 }
 


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