[gnome-software: 20/29] appstream: Make plugin setup asynchronous




commit eddcd9e5fda4bfab373d761cb92e25fda106dd35
Author: Philip Withnall <pwithnall endlessos org>
Date:   Fri Oct 15 14:17:13 2021 +0100

    appstream: Make plugin setup asynchronous
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Helps: #1472

 plugins/core/gs-plugin-appstream.c | 110 +++++++++++++++++++++++++++++++++++--
 1 file changed, 106 insertions(+), 4 deletions(-)
---
diff --git a/plugins/core/gs-plugin-appstream.c b/plugins/core/gs-plugin-appstream.c
index 042ebbb92..2998cd84a 100644
--- a/plugins/core/gs-plugin-appstream.c
+++ b/plugins/core/gs-plugin-appstream.c
@@ -31,6 +31,8 @@ struct _GsPluginAppstream
 {
        GsPlugin                 parent;
 
+       GsWorkerThread          *worker;  /* (owned) */
+
        XbSilo                  *silo;
        GRWLock                  silo_lock;
        GSettings               *settings;
@@ -38,6 +40,9 @@ struct _GsPluginAppstream
 
 G_DEFINE_TYPE (GsPluginAppstream, gs_plugin_appstream, GS_TYPE_PLUGIN)
 
+#define assert_in_worker(self) \
+       g_assert (gs_worker_thread_is_in_worker_context (self->worker))
+
 static void
 gs_plugin_appstream_dispose (GObject *object)
 {
@@ -46,6 +51,7 @@ gs_plugin_appstream_dispose (GObject *object)
        g_clear_object (&self->silo);
        g_clear_object (&self->settings);
        g_rw_lock_clear (&self->silo_lock);
+       g_clear_object (&self->worker);
 
        G_OBJECT_CLASS (gs_plugin_appstream_parent_class)->dispose (object);
 }
@@ -713,13 +719,103 @@ gs_plugin_appstream_check_silo (GsPluginAppstream  *self,
        return TRUE;
 }
 
-gboolean
-gs_plugin_setup (GsPlugin *plugin, GCancellable *cancellable, GError **error)
+static void setup_thread_cb (GTask        *task,
+                             gpointer      source_object,
+                             gpointer      task_data,
+                             GCancellable *cancellable);
+
+static void
+gs_plugin_appstream_setup_async (GsPlugin            *plugin,
+                                 GCancellable        *cancellable,
+                                 GAsyncReadyCallback  callback,
+                                 gpointer             user_data)
 {
        GsPluginAppstream *self = GS_PLUGIN_APPSTREAM (plugin);
+       g_autoptr(GTask) task = NULL;
 
-       /* set up silo, compiling if required */
-       return gs_plugin_appstream_check_silo (self, cancellable, error);
+       task = g_task_new (plugin, cancellable, callback, user_data);
+       g_task_set_source_tag (task, gs_plugin_appstream_setup_async);
+
+       /* Start up a worker thread to process all the plugin’s function calls. */
+       self->worker = gs_worker_thread_new ("gs-plugin-appstream");
+
+       /* Queue a job to check the silo, which will cause it to be loaded. */
+       gs_worker_thread_queue (self->worker, G_PRIORITY_DEFAULT,
+                               setup_thread_cb, g_steal_pointer (&task));
+}
+
+/* Run in @worker. */
+static void
+setup_thread_cb (GTask        *task,
+                 gpointer      source_object,
+                 gpointer      task_data,
+                 GCancellable *cancellable)
+{
+       GsPluginAppstream *self = GS_PLUGIN_APPSTREAM (source_object);
+       g_autoptr(GError) local_error = NULL;
+
+       assert_in_worker (self);
+
+       if (!gs_plugin_appstream_check_silo (self, cancellable, &local_error))
+               g_task_return_error (task, g_steal_pointer (&local_error));
+       else
+               g_task_return_boolean (task, TRUE);
+}
+
+static gboolean
+gs_plugin_appstream_setup_finish (GsPlugin      *plugin,
+                                  GAsyncResult  *result,
+                                  GError       **error)
+{
+       return g_task_propagate_boolean (G_TASK (result), error);
+}
+
+static void shutdown_cb (GObject      *source_object,
+                         GAsyncResult *result,
+                         gpointer      user_data);
+
+static void
+gs_plugin_appstream_shutdown_async (GsPlugin            *plugin,
+                                    GCancellable        *cancellable,
+                                    GAsyncReadyCallback  callback,
+                                    gpointer             user_data)
+{
+       GsPluginAppstream *self = GS_PLUGIN_APPSTREAM (plugin);
+       g_autoptr(GTask) task = NULL;
+
+       task = g_task_new (self, cancellable, callback, user_data);
+       g_task_set_source_tag (task, gs_plugin_appstream_shutdown_async);
+
+       /* Stop the worker thread. */
+       gs_worker_thread_shutdown_async (self->worker, cancellable, shutdown_cb, g_steal_pointer (&task));
+}
+
+static void
+shutdown_cb (GObject      *source_object,
+             GAsyncResult *result,
+             gpointer      user_data)
+{
+       g_autoptr(GTask) task = G_TASK (user_data);
+       GsPluginAppstream *self = g_task_get_source_object (task);
+       g_autoptr(GsWorkerThread) worker = NULL;
+       g_autoptr(GError) local_error = NULL;
+
+       worker = g_steal_pointer (&self->worker);
+
+       if (!gs_worker_thread_shutdown_finish (worker, result, &local_error)) {
+               g_task_return_error (task, g_steal_pointer (&local_error));
+               return;
+       }
+
+       g_task_return_boolean (task, TRUE);
+}
+
+static gboolean
+gs_plugin_appstream_shutdown_finish (GsPlugin      *plugin,
+                                     GAsyncResult  *result,
+                                     GError       **error)
+{
+       return g_task_propagate_boolean (G_TASK (result), error);
 }
 
 gboolean
@@ -1185,8 +1281,14 @@ static void
 gs_plugin_appstream_class_init (GsPluginAppstreamClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       GsPluginClass *plugin_class = GS_PLUGIN_CLASS (klass);
 
        object_class->dispose = gs_plugin_appstream_dispose;
+
+       plugin_class->setup_async = gs_plugin_appstream_setup_async;
+       plugin_class->setup_finish = gs_plugin_appstream_setup_finish;
+       plugin_class->shutdown_async = gs_plugin_appstream_shutdown_async;
+       plugin_class->shutdown_finish = gs_plugin_appstream_shutdown_finish;
 }
 
 GType


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