[glib] gapplication: Acquire the main context before running



commit a379a0ad59df0b377bd7b047d2e5a56417252992
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Tue Jul 28 00:14:08 2015 -0700

    gapplication: Acquire the main context before running
    
    Otherwise, we'll acquire it on every loop iteration, which can leave us
    vulnerable to racing another thread for the acquisition of the main
    context.
    
    This can break methods like g_main_context_invoke, which try to acquire
    a context to figure out if it can invoke the method synchronously or
    need to defer to an idle. In these cases, it isn't guaranteed that the
    invocation function will be invoked in the default main context,
    e.g. the one that GApplication is holding.
    
    This also matches what GMainLoop is doing.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=752983

 gio/gapplication.c |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)
---
diff --git a/gio/gapplication.c b/gio/gapplication.c
index ef3074b..05178e2 100644
--- a/gio/gapplication.c
+++ b/gio/gapplication.c
@@ -2238,6 +2238,9 @@ g_application_open (GApplication  *application,
  * This function sets the prgname (g_set_prgname()), if not already set,
  * to the basename of argv[0].
  *
+ * Much like g_main_loop_run(), this function will acquire the main context
+ * for the duration that the application is running.
+ *
  * Since 2.40, applications that are not explicitly flagged as services
  * or launchers (ie: neither %G_APPLICATION_IS_SERVICE or
  * %G_APPLICATION_IS_LAUNCHER are given as flags) will check (from the
@@ -2266,6 +2269,7 @@ g_application_run (GApplication  *application,
 {
   gchar **arguments;
   int status;
+  gboolean acquired_context;
 
   g_return_val_if_fail (G_IS_APPLICATION (application), 1);
   g_return_val_if_fail (argc == 0 || argv != NULL, 1);
@@ -2293,6 +2297,9 @@ g_application_run (GApplication  *application,
       g_free (prgname);
     }
 
+  acquired_context = g_main_context_acquire (NULL);
+  g_return_val_if_fail (acquired_context, 0);
+
   if (!G_APPLICATION_GET_CLASS (application)
         ->local_command_line (application, &arguments, &status))
     {
@@ -2351,6 +2358,8 @@ g_application_run (GApplication  *application,
     while (g_main_context_iteration (NULL, FALSE))
       ;
 
+  g_main_context_release (NULL);
+
   return status;
 }
 


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