From ee5396c45153b672c82c32b51026eb2cb76e4550 Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Thu, 8 Jul 2010 15:01:26 +0000 Subject: [PATCH] Refactored locking logic inn Registry, DataObject, SqlObject --- Builder/IISMainHandler/build.txt | 2 +- Builder/IISUploadHandler/build.txt | 2 +- Common/SqlObject.cs | 12 +++++----- Core/DataObject.cs | 9 ++++++-- Core/Registry.cs | 36 ++++++++++-------------------- 5 files changed, 27 insertions(+), 34 deletions(-) diff --git a/Builder/IISMainHandler/build.txt b/Builder/IISMainHandler/build.txt index 0905ea7..26f252b 100644 --- a/Builder/IISMainHandler/build.txt +++ b/Builder/IISMainHandler/build.txt @@ -1 +1 @@ -642 \ No newline at end of file +649 \ No newline at end of file diff --git a/Builder/IISUploadHandler/build.txt b/Builder/IISUploadHandler/build.txt index 9361cf7..be6c136 100644 --- a/Builder/IISUploadHandler/build.txt +++ b/Builder/IISUploadHandler/build.txt @@ -1 +1 @@ -376 \ No newline at end of file +383 \ No newline at end of file diff --git a/Common/SqlObject.cs b/Common/SqlObject.cs index a0dac00..cbf2746 100644 --- a/Common/SqlObject.cs +++ b/Common/SqlObject.cs @@ -17,7 +17,7 @@ namespace FLocal.Common { get; } - private bool _isLoaded = false; + private volatile bool _isLoaded = false; protected bool isLoaded { get { return this._isLoaded; @@ -27,8 +27,8 @@ namespace FLocal.Common { } } - private object lockFiller = new object(); - private object lockInitializer = new object(); + private volatile object lockFiller = new object(); + private volatile object lockInitializer = new object(); abstract protected void doFromHash(Dictionary data); @@ -87,10 +87,10 @@ namespace FLocal.Common { } } - protected override void AfterCreate(bool forLoadingFromHash) { + /*protected override void AfterCreate(bool forLoadingFromHash) { base.AfterCreate(forLoadingFromHash); - if(!forLoadingFromHash) this.Load(); - } + if(!forLoadingFromHash) this.LoadIfNotLoaded(); + }*/ protected static void Refresh(TKey id) { Dictionary objects = LoadByIdsForLoadingFromHash(new List() { id }); diff --git a/Core/DataObject.cs b/Core/DataObject.cs index e182c42..5914dd9 100644 --- a/Core/DataObject.cs +++ b/Core/DataObject.cs @@ -47,13 +47,18 @@ namespace FLocal.Core { return res; } - protected virtual void AfterCreate(bool forLoadingFromHash) { } + /// + /// This method should consume no time + /// + /// + protected virtual void AfterCreate(bool forLoadingFromHash) { } internal override void CreateByIdFromRegistry(TKey id, bool forLoadingFromHash) { if(!this.isJustCreated) throw new CriticalException("Object already has an id"); this._id = id; this.isJustCreated = false; - this.AfterCreate(forLoadingFromHash); + //System.Threading.ThreadPool.QueueUserWorkItem(state => this.AfterCreate(forLoadingFromHash)); + this.AfterCreate(forLoadingFromHash); } internal override void markAsDeletedFromRegistry() { diff --git a/Core/Registry.cs b/Core/Registry.cs index 4a401a7..16ae28c 100644 --- a/Core/Registry.cs +++ b/Core/Registry.cs @@ -10,9 +10,9 @@ namespace FLocal.Core { internal static readonly Registry instance = new Registry(); - private Dictionary storage; + private volatile Dictionary storage; - private Dictionary locks; + private volatile Dictionary locks; private Registry() { this.storage = new Dictionary(); @@ -20,23 +20,15 @@ namespace FLocal.Core { } internal TData Get(TKey id, bool forLoadingFromHash) { - if(!this.locks.ContainsKey(id)) { + if(!this.storage.ContainsKey(id)) { lock(this.locks) { - if(!this.locks.ContainsKey(id)) { - this.locks.Add(id, new object()); + if(!this.storage.ContainsKey(id)) { + TData obj = new TData(); + obj.CreateByIdFromRegistry(id, forLoadingFromHash); + this.storage[id] = obj; } } } - lock(this.locks[id]) { - if(!this.storage.ContainsKey(id)) { - - //this.storage[id] = IDataObject.CreateByIdFromRegistry(id); - //this.storage[id] = TData.CreateByIdFromRegistry(id); - TData obj = new TData(); - obj.CreateByIdFromRegistry(id, forLoadingFromHash); - this.storage[id] = obj; - } - } lock(this.locks) { if(this.storage.ContainsKey(id)) { @@ -58,16 +50,12 @@ namespace FLocal.Core { } internal void Delete(TKey[] idsToDelete) { - foreach(TKey id in idsToDelete) { - lock(this.locks[id]) { + lock(this.locks) { + foreach(TKey id in idsToDelete) { IDataObject obj = null; - lock(this.locks) { - if(this.storage.ContainsKey(id)) { - obj = this.storage[id]; - this.storage.Remove(id); - } - } - if(obj != null) { + if(this.storage.ContainsKey(id)) { + obj = this.storage[id]; + this.storage.Remove(id); obj.markAsDeletedFromRegistry(); } }