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
This commit is contained in:
Jasper St. Pierre 2015-07-28 00:14:08 -07:00 committed by Matthias Clasen
parent 5e73ca974d
commit a379a0ad59

View File

@ -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;
}