[glib/wip/new-gsource: 3/8] gsource: allow NULL check and prepare functions



commit 38d6acaf783f5bffcdd93160c79c6bc02f08d2f9
Author: Ryan Lortie <desrt desrt ca>
Date:   Mon Oct 29 18:26:05 2012 +0100

    gsource: allow NULL check and prepare functions
    
    Allow for NULL GSourceFuncs.check() and .prepare().
    
    For prepare() the source will be taken not to be ready and having an
    infinite timeout.  For check() the source will be taken not to be ready.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=686853

 glib/gmain.c |   57 ++++++++++++++++++++++++++++++++++++---------------------
 glib/gmain.h |    8 ++++++--
 2 files changed, 42 insertions(+), 23 deletions(-)
---
diff --git a/glib/gmain.c b/glib/gmain.c
index 133b0f1..6721f09 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -2975,18 +2975,26 @@ g_main_context_prepare (GMainContext *context,
 
       if (!(source->flags & G_SOURCE_READY))
 	{
-	  gboolean result;
-	  gboolean (*prepare)  (GSource  *source, 
-				gint     *timeout);
+	  gboolean result = FALSE;
+	  gboolean (* prepare) (GSource  *source,
+                                gint     *timeout);
 
-	  prepare = source->source_funcs->prepare;
-	  context->in_check_or_prepare++;
-	  UNLOCK_CONTEXT (context);
+          prepare = source->source_funcs->prepare;
 
-	  result = (*prepare) (source, &source_timeout);
+          /* If the prepare function is set, call it.
+           *
+           * Otherwise, assume it would return FALSE with timeout of -1.
+           */
+          if (prepare)
+            {
+              context->in_check_or_prepare++;
+              UNLOCK_CONTEXT (context);
 
-	  LOCK_CONTEXT (context);
-	  context->in_check_or_prepare--;
+              result = (* prepare) (source, &source_timeout);
+
+              LOCK_CONTEXT (context);
+              context->in_check_or_prepare--;
+            }
 
 	  if (result)
 	    {
@@ -3158,19 +3166,26 @@ g_main_context_check (GMainContext *context,
 
       if (!(source->flags & G_SOURCE_READY))
 	{
-	  gboolean result;
-	  gboolean (*check) (GSource  *source);
+          gboolean result = FALSE;
+          gboolean (* check) (GSource *source);
+
+          check = source->source_funcs ? source->source_funcs->check : NULL;
+
+          /* If the check function is set, call it.
+           *
+           * Otherwise assume it would return FALSE.
+           */
+          if (check)
+            {
+              context->in_check_or_prepare++;
+              UNLOCK_CONTEXT (context);
+
+              result = (* check) (source);
+
+              LOCK_CONTEXT (context);
+              context->in_check_or_prepare--;
+            }
 
-	  check = source->source_funcs->check;
-	  
-	  context->in_check_or_prepare++;
-	  UNLOCK_CONTEXT (context);
-	  
-	  result = (*check) (source);
-	  
-	  LOCK_CONTEXT (context);
-	  context->in_check_or_prepare--;
-	  
 	  if (result)
 	    {
 	      GSource *ready_source = source;
diff --git a/glib/gmain.h b/glib/gmain.h
index de035ca..a40cb93 100644
--- a/glib/gmain.h
+++ b/glib/gmain.h
@@ -86,11 +86,15 @@ typedef struct _GSourceCallbackFuncs    GSourceCallbackFuncs;
  *     a @timeout_ value which should be the maximum timeout (in milliseconds)
  *     which should be passed to the poll() call. The actual timeout used will
  *     be -1 if all sources returned -1, or it will be the minimum of all the
- *     @timeout_ values returned which were >= 0.
+ *     @timeout_ values returned which were >= 0.  Since 2.36 this may
+ *     be %NULL, in which case the effect is as if the function always
+ *     returns %FALSE with a timeout of -1.
  * @check: Called after all the file descriptors are polled. The source
  *     should return %TRUE if it is ready to be dispatched. Note that some
  *     time may have passed since the previous prepare function was called,
- *     so the source should be checked again here.
+ *     so the source should be checked again here.  Since 2.36 this may
+ *     be %NULL, in which case the effect is as if the function always
+ *     returns %FALSE.
  * @dispatch: Called to dispatch the event source, after it has returned
  *     %TRUE in either its @prepare or its @check function. The @dispatch
  *     function is passed in a callback function and data. The callback



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