[glib/wip/new-gsource: 6/8] gsource: add g_source_set_ready_time()



commit 095a2494984971d5e10344ccc35adb8a15c740bd
Author: Ryan Lortie <desrt desrt ca>
Date:   Tue Oct 30 09:48:10 2012 +0100

    gsource: add g_source_set_ready_time()
    
    This API provides the ability to set a monotonic time at which the
    source automatically becomes ready without having to implement a check
    or prepare function.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=657729

 docs/reference/glib/glib-sections.txt |    1 +
 glib/glib.symbols                     |    1 +
 glib/gmain.c                          |   79 +++++++++++++++++++++++++++++++++
 glib/gmain.h                          |    3 +
 4 files changed, 84 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 42ec0f9..9115363 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -529,6 +529,7 @@ g_source_get_context
 g_source_set_callback
 GSourceFunc
 g_source_set_callback_indirect
+g_source_set_ready_time
 g_source_add_handle
 g_source_remove_handle
 g_source_modify_handle
diff --git a/glib/glib.symbols b/glib/glib.symbols
index 92df304..51cb8f7 100644
--- a/glib/glib.symbols
+++ b/glib/glib.symbols
@@ -656,6 +656,7 @@ g_source_set_name
 g_source_set_name_by_id
 g_source_is_destroyed
 g_source_set_priority
+g_source_set_ready_time
 g_source_unref
 g_idle_add
 g_idle_add_full
diff --git a/glib/gmain.c b/glib/gmain.c
index 4d66e89..ed3de3d 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -318,6 +318,8 @@ struct _GSourcePrivate
 
   GPollFD **handles;
   gint      n_handles;
+
+  gint64    ready_time;
 };
 
 typedef struct _GSourceIter
@@ -839,6 +841,8 @@ g_source_new (GSourceFuncs *source_funcs,
 
   source->flags = G_HOOK_FLAG_ACTIVE;
 
+  source->priv->ready_time = -1;
+
   /* NULL/0 initialization for all other fields */
   
   return source;
@@ -1664,6 +1668,44 @@ g_source_get_priority (GSource *source)
 }
 
 /**
+ * g_source_set_ready_time:
+ * @source: a #GSource
+ * @ready_time: the monotonic time at which the source will be ready,
+ *               or -1
+ *
+ * Sets a #GSource to be dispatched when the given monotonic time is
+ * reached (or passed).
+ *
+ * If @ready_time is -1 then the wakeup is unset.
+ *
+ * Since: 2.36
+ **/
+void
+g_source_set_ready_time (GSource *source,
+                         gint64   ready_time)
+{
+  GMainContext *context;
+
+  g_return_if_fail (source != NULL);
+  g_return_if_fail (source->ref_count > 0);
+
+  context = source->context;
+
+  if (context)
+    LOCK_CONTEXT (context);
+
+  source->priv->ready_time = ready_time;
+
+  if (context)
+    {
+      /* Quite likely that we need to change the timeout on the poll */
+      if (!SOURCE_BLOCKED (source))
+        g_wakeup_signal (context->wakeup);
+      UNLOCK_CONTEXT (context);
+    }
+}
+
+/**
  * g_source_set_can_recurse:
  * @source: a #GSource
  * @can_recurse: whether recursion is allowed for this source
@@ -3225,6 +3267,31 @@ g_main_context_prepare (GMainContext *context,
               context->in_check_or_prepare--;
             }
 
+          if (result == FALSE && source->priv->ready_time != -1)
+            {
+              if (!context->time_is_fresh)
+                {
+                  context->time = g_get_monotonic_time ();
+                  context->time_is_fresh = TRUE;
+                }
+
+              if (source->priv->ready_time <= context->time)
+                {
+                  source_timeout = 0;
+                  result = TRUE;
+                }
+              else
+                {
+                  gint timeout;
+
+                  /* rounding down will lead to spinning, so always round up */
+                  timeout = (source->priv->ready_time - context->time + 999) / 1000;
+
+                  if (source_timeout < 0 || timeout < source_timeout)
+                    source_timeout = timeout;
+                }
+            }
+
 	  if (result)
 	    {
 	      GSource *ready_source = source;
@@ -3430,6 +3497,18 @@ g_main_context_check (GMainContext *context,
                   }
             }
 
+          if (result == FALSE && source->priv->ready_time != -1)
+            {
+              if (!context->time_is_fresh)
+                {
+                  context->time = g_get_monotonic_time ();
+                  context->time_is_fresh = TRUE;
+                }
+
+              if (source->priv->ready_time <= context->time)
+                result = TRUE;
+            }
+
 	  if (result)
 	    {
 	      GSource *ready_source = source;
diff --git a/glib/gmain.h b/glib/gmain.h
index eaa6bb6..70ae009 100644
--- a/glib/gmain.h
+++ b/glib/gmain.h
@@ -410,6 +410,9 @@ const char *         g_source_get_name       (GSource        *source);
 void                 g_source_set_name_by_id (guint           tag,
                                               const char     *name);
 
+void                 g_source_set_ready_time (GSource        *source,
+                                              gint64          ready_time);
+
 guint                g_source_add_handle     (GSource        *source,
                                               GHandle         handle,
                                               GIOCondition    events);



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