[blam] ItemStore: Use a lock around the hash table access



commit a6fb2b53c45e8f851b08d4db51e0088127f1ea9b
Author: Carlos Martín Nieto <carlos cmartin tk>
Date:   Sun Mar 27 14:43:40 2011 +0200

    ItemStore: Use a lock around the hash table access
    
    Signed-off-by: Carlos Martín Nieto <carlos cmartin tk>

 src/ItemStore.cs |   51 ++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 36 insertions(+), 15 deletions(-)
---
diff --git a/src/ItemStore.cs b/src/ItemStore.cs
index bf904e3..ec6b80f 100644
--- a/src/ItemStore.cs
+++ b/src/ItemStore.cs
@@ -12,26 +12,39 @@ namespace Imendio.Blam
         private Hashtable items;
         static ItemStore instance = null;
         static string itemfile = Defines.APP_HOMEDIR + "/" + Defines.APP_ITEMSTORE_FILE;
-        private object db_lock;
+        private object db_lock = new object();
 
         public Item Get(string id)
         {
+            Item item;
+
             if(id == null){
                 Console.Error.WriteLine("Tried to access item with null key");
                 return null;
             }
 
-            return items[id] as Item;
+            lock(db_lock){
+                item = items[id] as Item;
+            }
+
+            return item;
         }
 
         public void Add(Item item)
         {
-            if(items.ContainsKey(item.Id)){
-                (items[item.Id] as Item).Grab();
+            if(item.Id == null){
+                Console.Error.WriteLine("Tried to add item with null key");
                 return;
-            } else {
-                item.Grab();
-                items.Add(item.Id, item);
+            }
+
+            lock(db_lock){
+                if(items.ContainsKey(item.Id)){
+                    (items[item.Id] as Item).Grab();
+                    return;
+                } else {
+                    item.Grab();
+                    items.Add(item.Id, item);
+                }
             }
         }
 
@@ -41,15 +54,19 @@ namespace Imendio.Blam
                 return;
             }
 
-            item.Release();
-            if(item.RefCount == 0){
-                items.Remove(item.Id);
+            lock(db_lock){
+                item.Release();
+                if(item.RefCount == 0){
+                    items.Remove(item.Id);
+                }
             }
         }
 
         public void Remove(string id)
         {
-            Remove(items[id] as Item);
+            lock(db_lock){
+                Remove(items[id] as Item);
+            }
         }
 
         public static ItemStore GetInstance()
@@ -86,12 +103,16 @@ namespace Imendio.Blam
                 return;
             }
 
-            foreach(Item item in instance.items.Values){
-                item.WriteExtensions();
+            Item[] items;
+
+            lock(instance.db_lock){
+                foreach(Item item in instance.items.Values){
+                    item.WriteExtensions();
+                }
+                items = new Item[instance.items.Count];
+                instance.items.Values.CopyTo(items, 0);
             }
             XmlWriter writer = XmlWriter.Create(itemfile);
-            Item[] items = new Item[instance.items.Count];
-            instance.items.Values.CopyTo(items, 0);
             SyndicationFeed sf = new SyndicationFeed(items);
             Atom10FeedFormatter fmtr = sf.GetAtom10Formatter();
             fmtr.WriteTo(writer);



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