[glib: 1/4] gmain: Factor out common GTimeoutSource code




commit af02a614a240606f1cb9fe35d06107bce3d3170d
Author: Philip Withnall <pwithnall endlessos org>
Date:   Fri May 27 13:19:54 2022 +0100

    gmain: Factor out common GTimeoutSource code
    
    This allows it to be reused and extended (internally) a little more.
    This commit introduces no functional changes, but allows for more easy
    additions in a following commit.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 glib/gmain.c | 75 ++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 43 insertions(+), 32 deletions(-)
---
diff --git a/glib/gmain.c b/glib/gmain.c
index cd92ed580c..53af23f515 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -4980,6 +4980,21 @@ g_timeout_dispatch (GSource     *source,
   return again;
 }
 
+static GSource *
+timeout_source_new (guint    interval,
+                    gboolean seconds)
+{
+  GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
+  GTimeoutSource *timeout_source = (GTimeoutSource *)source;
+
+  timeout_source->interval = interval;
+  timeout_source->seconds = seconds;
+
+  g_timeout_set_expiration (timeout_source, g_get_monotonic_time ());
+
+  return source;
+}
+
 /**
  * g_timeout_source_new:
  * @interval: the timeout interval in milliseconds.
@@ -4998,13 +5013,7 @@ g_timeout_dispatch (GSource     *source,
 GSource *
 g_timeout_source_new (guint interval)
 {
-  GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
-  GTimeoutSource *timeout_source = (GTimeoutSource *)source;
-
-  timeout_source->interval = interval;
-  g_timeout_set_expiration (timeout_source, g_get_monotonic_time ());
-
-  return source;
+  return timeout_source_new (interval, FALSE);
 }
 
 /**
@@ -5030,17 +5039,36 @@ g_timeout_source_new (guint interval)
 GSource *
 g_timeout_source_new_seconds (guint interval)
 {
-  GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
-  GTimeoutSource *timeout_source = (GTimeoutSource *)source;
+  return timeout_source_new (interval, TRUE);
+}
 
-  timeout_source->interval = interval;
-  timeout_source->seconds = TRUE;
+static guint
+timeout_add_full (gint           priority,
+                  guint          interval,
+                  gboolean       seconds,
+                  GSourceFunc    function,
+                  gpointer       data,
+                  GDestroyNotify notify)
+{
+  GSource *source;
+  guint id;
 
-  g_timeout_set_expiration (timeout_source, g_get_monotonic_time ());
+  g_return_val_if_fail (function != NULL, 0);
 
-  return source;
-}
+  source = timeout_source_new (interval, seconds);
 
+  if (priority != G_PRIORITY_DEFAULT)
+    g_source_set_priority (source, priority);
+
+  g_source_set_callback (source, function, data, notify);
+  id = g_source_attach (source, NULL);
+
+  TRACE (GLIB_TIMEOUT_ADD (source, g_main_context_default (), id, priority, interval, function, data));
+
+  g_source_unref (source);
+
+  return id;
+}
 
 /**
  * g_timeout_add_full: (rename-to g_timeout_add)
@@ -5086,24 +5114,7 @@ g_timeout_add_full (gint           priority,
                    gpointer       data,
                    GDestroyNotify notify)
 {
-  GSource *source;
-  guint id;
-  
-  g_return_val_if_fail (function != NULL, 0);
-
-  source = g_timeout_source_new (interval);
-
-  if (priority != G_PRIORITY_DEFAULT)
-    g_source_set_priority (source, priority);
-
-  g_source_set_callback (source, function, data, notify);
-  id = g_source_attach (source, NULL);
-
-  TRACE (GLIB_TIMEOUT_ADD (source, g_main_context_default (), id, priority, interval, function, data));
-
-  g_source_unref (source);
-
-  return id;
+  return timeout_add_full (priority, interval, FALSE, function, data, notify);
 }
 
 /**


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