[Xfce4-commits] <garcon:master> Use a GStaticMutex in the item cache.

Nick Schermer nick at xfce.org
Mon Aug 17 18:16:09 CEST 2009


Updating branch refs/heads/master
         to f9dd6b9bc5b02b34c2f5bada07870d882b1fafff (commit)
       from b449c28f93c1e9c13601a6c8f0a3a0a41160df34 (commit)

commit f9dd6b9bc5b02b34c2f5bada07870d882b1fafff
Author: Nick Schermer <nick at xfce.org>
Date:   Mon Aug 17 17:54:53 2009 +0200

    Use a GStaticMutex in the item cache.
    
    Because the object is implemented as a singleton we can
    use a global lock in the file. The advantage is that
    we can drop our g_thread_init() call in garcon_init(), so
    the user can initialize garcon from every place in the
    code.

 garcon/garcon-menu-item-cache.c |   30 ++++++++++++++++--------------
 1 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/garcon/garcon-menu-item-cache.c b/garcon/garcon-menu-item-cache.c
index 56a15b0..219bf23 100644
--- a/garcon/garcon-menu-item-cache.c
+++ b/garcon/garcon-menu-item-cache.c
@@ -51,13 +51,15 @@ static void garcon_menu_item_cache_finalize   (GObject                  *object)
 
 
 
+/* Mutex lock */
+static GStaticMutex lock = G_STATIC_MUTEX_INIT;
+
+
+
 struct _GarconMenuItemCachePrivate
 {
   /* Hash table for mapping absolute filenames to GarconMenuItem's */
   GHashTable *items;
-
-  /* Mutex lock */
-  GMutex     *lock;
 };
 
 
@@ -84,9 +86,6 @@ garcon_menu_item_cache_init (GarconMenuItemCache *cache)
 {
   cache->priv = GARCON_MENU_ITEM_CACHE_GET_PRIVATE (cache);
 
-  /* Initialize the mutex lock */
-  cache->priv->lock = g_mutex_new ();
-
   /* Create empty hash table */
   cache->priv->items = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
                                               (GDestroyNotify) garcon_menu_item_unref);
@@ -132,9 +131,6 @@ garcon_menu_item_cache_finalize (GObject *object)
   /* Free hash table */
   g_hash_table_unref (cache->priv->items);
 
-  /* Destroy the mutex */
-  g_mutex_free (cache->priv->lock);
-
   (*G_OBJECT_CLASS (garcon_menu_item_cache_parent_class)->finalize) (object);
 }
 
@@ -154,7 +150,7 @@ garcon_menu_item_cache_lookup (GarconMenuItemCache *cache,
   /* Acquire lock on the item cache as it's likely that we need to load
    * items from the hard drive and store it in the hash table of the
    * item cache */
-  g_mutex_lock (cache->priv->lock);
+  g_static_mutex_lock (&lock);
 
   /* Search uri in the hash table */
   item = g_hash_table_lookup (cache->priv->items, uri);
@@ -166,7 +162,7 @@ garcon_menu_item_cache_lookup (GarconMenuItemCache *cache,
       garcon_menu_item_set_desktop_id (item, desktop_id);
 
       /* Release item cache lock */
-      g_mutex_unlock (cache->priv->lock);
+      g_static_mutex_unlock (&lock);
 
       return item;
     }
@@ -184,7 +180,7 @@ garcon_menu_item_cache_lookup (GarconMenuItemCache *cache,
     }
 
   /* Release item cache lock */
-  g_mutex_unlock (cache->priv->lock);
+  g_static_mutex_unlock (&lock);
 
   return item;
 }
@@ -199,12 +195,12 @@ garcon_menu_item_cache_foreach (GarconMenuItemCache *cache,
   g_return_if_fail (GARCON_IS_MENU_ITEM_CACHE (cache));
 
   /* Acquire lock on the item cache */
-  g_mutex_lock (cache->priv->lock);
+  g_static_mutex_lock (&lock);
 
   g_hash_table_foreach (cache->priv->items, func, user_data);
 
   /* Release item cache lock */
-  g_mutex_unlock (cache->priv->lock);
+  g_static_mutex_unlock (&lock);
 }
 
 
@@ -214,6 +210,12 @@ garcon_menu_item_cache_invalidate (GarconMenuItemCache *cache)
 {
   g_return_if_fail (GARCON_IS_MENU_ITEM_CACHE (cache));
 
+  /* Acquire lock on the item cache */
+  g_static_mutex_lock (&lock);
+
   /* Remove all items from the hash table */
   g_hash_table_remove_all (cache->priv->items);
+
+  /* Release item cache lock */
+  g_static_mutex_unlock (&lock);
 }



More information about the Xfce4-commits mailing list