[glib] GSource: add g_source_set_name, g_source_get_name, g_source_set_name_by_id



commit e5696c282e2c48ac0f822c4e6d33c8507a77e998
Author: Havoc Pennington <hp pobox com>
Date:   Tue Apr 20 17:47:44 2010 -0400

    GSource: add g_source_set_name, g_source_get_name, g_source_set_name_by_id
    
    These allow applications to give meaningful names to their sources.
    Source names can then be used for debugging and profiling, for
    example with systemtap or gdb.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=606044

 glib/glib.symbols |    3 ++
 glib/gmain.c      |   83 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 glib/gmain.h      |    9 +++++-
 3 files changed, 93 insertions(+), 2 deletions(-)
---
diff --git a/glib/glib.symbols b/glib/glib.symbols
index 62abfa8..f197a6d 100644
--- a/glib/glib.symbols
+++ b/glib/glib.symbols
@@ -659,6 +659,7 @@ g_source_get_can_recurse
 g_source_get_context
 g_source_get_current_time
 g_source_get_id
+g_source_get_name
 g_source_get_priority
 g_source_new
 g_source_ref
@@ -670,6 +671,8 @@ g_source_set_callback
 g_source_set_callback_indirect
 g_source_set_can_recurse
 g_source_set_funcs
+g_source_set_name
+g_source_set_name_by_id
 g_source_is_destroyed
 g_source_set_priority
 g_source_unref
diff --git a/glib/gmain.c b/glib/gmain.c
index e04c7b5..048d834 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -1271,6 +1271,88 @@ g_source_get_can_recurse (GSource  *source)
   return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
 }
 
+
+/**
+ * g_source_set_name:
+ * @source: a #GSource
+ * @name: debug name for the source
+ *
+ * Sets a name for the source, used in debugging and profiling.
+ * The name defaults to #NULL.
+ *
+ * The source name should describe in a human-readable way
+ * what the source does. For example, "X11 event queue"
+ * or "GTK+ repaint idle handler" or whatever it is.
+ *
+ * It is permitted to call this function multiple times, but is not
+ * recommended due to the potential performance impact.  For example,
+ * one could change the name in the "check" function of a #GSourceFuncs 
+ * to include details like the event type in the source name.
+ *
+ * Since: 2.26
+ **/
+void
+g_source_set_name (GSource    *source,
+                   const char *name)
+{
+  g_return_if_fail (source != NULL);
+
+  /* setting back to NULL is allowed, just because it's
+   * weird if get_name can return NULL but you can't
+   * set that.
+   */
+
+  g_free (source->name);
+  source->name = g_strdup (name);
+}
+
+/**
+ * g_source_get_name:
+ * @source: a #GSource
+ *
+ * Gets a name for the source, used in debugging and profiling.
+ * The name may be #NULL if it has never been set with
+ * g_source_set_name().
+ *
+ * Return value: the name of the source
+ * Since: 2.26
+ **/
+G_CONST_RETURN char*
+g_source_get_name (GSource *source)
+{
+  g_return_val_if_fail (source != NULL, NULL);
+
+  return source->name;
+}
+
+/**
+ * g_source_set_name_by_id:
+ * @tag: a #GSource ID
+ * @name: debug name for the source
+ *
+ * Sets the name of a source using its ID.
+ *
+ * This is a convenience utility to set source names from the return
+ * value of g_idle_add(), g_timeout_add(), etc.
+ *
+ * Since: 2.26
+ **/
+void
+g_source_set_name_by_id (guint           tag,
+                         const char     *name)
+{
+  GSource *source;
+
+  g_return_if_fail (tag > 0);
+
+  source = g_main_context_find_source_by_id (NULL, tag);
+  if (source == NULL)
+    return;
+
+  g_source_set_name (source, name);
+}
+
+
 /**
  * g_source_ref:
  * @source: a #GSource
@@ -1858,7 +1940,6 @@ g_source_is_destroyed (GSource *source)
   return SOURCE_DESTROYED (source);
 }
 
-
 /* Temporarily remove all this source's file descriptors from the
  * poll(), so that if data comes available for one of the file descriptors
  * we don't continually spin in the poll()
diff --git a/glib/gmain.h b/glib/gmain.h
index 15fe54e..3012cd1 100644
--- a/glib/gmain.h
+++ b/glib/gmain.h
@@ -60,7 +60,7 @@ struct _GSource
   GSource *prev;
   GSource *next;
 
-  gpointer reserved1;
+  char    *name;
   gpointer reserved2;
 };
 
@@ -205,6 +205,13 @@ void     g_source_set_funcs       (GSource        *source,
                                    GSourceFuncs   *funcs);
 gboolean g_source_is_destroyed    (GSource        *source);
 
+void                 g_source_set_name       (GSource        *source,
+                                              const char     *name);
+G_CONST_RETURN char* g_source_get_name       (GSource        *source);
+void                 g_source_set_name_by_id (guint           tag,
+                                              const char     *name);
+
+
 /* Used to implement g_source_connect_closure and internally*/
 void g_source_set_callback_indirect (GSource              *source,
 				     gpointer              callback_data,



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