[gnome-software] Make the hardcoded ratings plugin threadsafe



commit 8cf53129268a5c319938d393d269400bd6bed3e4
Author: Richard Hughes <richard hughsie com>
Date:   Sun Sep 1 15:50:13 2013 +0100

    Make the hardcoded ratings plugin threadsafe

 src/plugins/gs-plugin-hardcoded-ratings.c |   25 ++++++++++++++++++-------
 1 files changed, 18 insertions(+), 7 deletions(-)
---
diff --git a/src/plugins/gs-plugin-hardcoded-ratings.c b/src/plugins/gs-plugin-hardcoded-ratings.c
index 91db78f..92e1759 100644
--- a/src/plugins/gs-plugin-hardcoded-ratings.c
+++ b/src/plugins/gs-plugin-hardcoded-ratings.c
@@ -26,6 +26,7 @@
 struct GsPluginPrivate {
        GHashTable              *cache;
        gboolean                 loaded;
+       GMutex                   mutex;
 };
 
 /**
@@ -50,6 +51,9 @@ gs_plugin_initialize (GsPlugin *plugin)
                                                     g_str_equal,
                                                     NULL,
                                                     NULL);
+
+       /* can only load from one thread */
+       g_mutex_init (&plugin->priv->mutex);
 }
 
 /**
@@ -68,15 +72,16 @@ void
 gs_plugin_destroy (GsPlugin *plugin)
 {
        g_hash_table_unref (plugin->priv->cache);
+       g_mutex_clear (&plugin->priv->mutex);
 }
 
 /**
- * gs_plugin_hardcoded_ratings_add:
+ * gs_plugin_hardcoded_ratings_setup:
  *
  * The data was generated by contrib/get-ratings-for-fas.py on 2013-03-07
  */
 static gboolean
-gs_plugin_hardcoded_ratings_add (GsPlugin *plugin, GError **error)
+gs_plugin_hardcoded_ratings_setup (GsPlugin *plugin, GError **error)
 {
        guint i;
        static struct {
@@ -688,6 +693,12 @@ gs_plugin_hardcoded_ratings_add (GsPlugin *plugin, GError **error)
                { -1    ,NULL}
        };
 
+       /* protect */
+       g_mutex_lock (&plugin->priv->mutex);
+
+       if (plugin->priv->loaded)
+               goto out;
+
        /* add each one to a hash table */
        for (i = 0; ratings[i].id != NULL; i++) {
                g_hash_table_insert (plugin->priv->cache,
@@ -696,6 +707,8 @@ gs_plugin_hardcoded_ratings_add (GsPlugin *plugin, GError **error)
        }
 
        plugin->priv->loaded = TRUE;
+out:
+       g_mutex_unlock (&plugin->priv->mutex);
        return TRUE;
 }
 
@@ -714,11 +727,9 @@ gs_plugin_refine (GsPlugin *plugin,
        GsApp *app;
 
        /* already loaded */
-       if (!plugin->priv->loaded) {
-               ret = gs_plugin_hardcoded_ratings_add (plugin, error);
-               if (!ret)
-                       goto out;
-       }
+       ret = gs_plugin_hardcoded_ratings_setup (plugin, error);
+       if (!ret)
+               goto out;
 
        /* add any missing ratings data */
        for (l = list; l != NULL; l = l->next) {


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