Refactored locking logic inn Registry, DataObject, SqlObject

main
Inga 🏳‍🌈 15 years ago
parent 56ccdd0290
commit ee5396c451
  1. 2
      Builder/IISMainHandler/build.txt
  2. 2
      Builder/IISUploadHandler/build.txt
  3. 12
      Common/SqlObject.cs
  4. 9
      Core/DataObject.cs
  5. 36
      Core/Registry.cs

@ -17,7 +17,7 @@ namespace FLocal.Common {
get; get;
} }
private bool _isLoaded = false; private volatile bool _isLoaded = false;
protected bool isLoaded { protected bool isLoaded {
get { get {
return this._isLoaded; return this._isLoaded;
@ -27,8 +27,8 @@ namespace FLocal.Common {
} }
} }
private object lockFiller = new object(); private volatile object lockFiller = new object();
private object lockInitializer = new object(); private volatile object lockInitializer = new object();
abstract protected void doFromHash(Dictionary<string, string> data); abstract protected void doFromHash(Dictionary<string, string> data);
@ -87,10 +87,10 @@ namespace FLocal.Common {
} }
} }
protected override void AfterCreate(bool forLoadingFromHash) { /*protected override void AfterCreate(bool forLoadingFromHash) {
base.AfterCreate(forLoadingFromHash); base.AfterCreate(forLoadingFromHash);
if(!forLoadingFromHash) this.Load(); if(!forLoadingFromHash) this.LoadIfNotLoaded();
} }*/
protected static void Refresh(TKey id) { protected static void Refresh(TKey id) {
Dictionary<TKey, T> objects = LoadByIdsForLoadingFromHash(new List<TKey>() { id }); Dictionary<TKey, T> objects = LoadByIdsForLoadingFromHash(new List<TKey>() { id });

@ -47,13 +47,18 @@ namespace FLocal.Core {
return res; return res;
} }
protected virtual void AfterCreate(bool forLoadingFromHash) { } /// <summary>
/// This method should consume no time
/// </summary>
/// <param name="forLoadingFromHash"></param>
protected virtual void AfterCreate(bool forLoadingFromHash) { }
internal override void CreateByIdFromRegistry(TKey id, bool forLoadingFromHash) { internal override void CreateByIdFromRegistry(TKey id, bool forLoadingFromHash) {
if(!this.isJustCreated) throw new CriticalException("Object already has an id"); if(!this.isJustCreated) throw new CriticalException("Object already has an id");
this._id = id; this._id = id;
this.isJustCreated = false; this.isJustCreated = false;
this.AfterCreate(forLoadingFromHash); //System.Threading.ThreadPool.QueueUserWorkItem(state => this.AfterCreate(forLoadingFromHash));
this.AfterCreate(forLoadingFromHash);
} }
internal override void markAsDeletedFromRegistry() { internal override void markAsDeletedFromRegistry() {

@ -10,9 +10,9 @@ namespace FLocal.Core {
internal static readonly Registry<TKey, TData> instance = new Registry<TKey,TData>(); internal static readonly Registry<TKey, TData> instance = new Registry<TKey,TData>();
private Dictionary<TKey, TData> storage; private volatile Dictionary<TKey, TData> storage;
private Dictionary<TKey, object> locks; private volatile Dictionary<TKey, object> locks;
private Registry() { private Registry() {
this.storage = new Dictionary<TKey,TData>(); this.storage = new Dictionary<TKey,TData>();
@ -20,23 +20,15 @@ namespace FLocal.Core {
} }
internal TData Get(TKey id, bool forLoadingFromHash) { internal TData Get(TKey id, bool forLoadingFromHash) {
if(!this.locks.ContainsKey(id)) { if(!this.storage.ContainsKey(id)) {
lock(this.locks) { lock(this.locks) {
if(!this.locks.ContainsKey(id)) { if(!this.storage.ContainsKey(id)) {
this.locks.Add(id, new object()); 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<TKey, TData>.CreateByIdFromRegistry(id);
//this.storage[id] = TData.CreateByIdFromRegistry(id);
TData obj = new TData();
obj.CreateByIdFromRegistry(id, forLoadingFromHash);
this.storage[id] = obj;
}
}
lock(this.locks) { lock(this.locks) {
if(this.storage.ContainsKey(id)) { if(this.storage.ContainsKey(id)) {
@ -58,16 +50,12 @@ namespace FLocal.Core {
} }
internal void Delete(TKey[] idsToDelete) { internal void Delete(TKey[] idsToDelete) {
foreach(TKey id in idsToDelete) { lock(this.locks) {
lock(this.locks[id]) { foreach(TKey id in idsToDelete) {
IDataObject<TKey, TData> obj = null; IDataObject<TKey, TData> obj = null;
lock(this.locks) { if(this.storage.ContainsKey(id)) {
if(this.storage.ContainsKey(id)) { obj = this.storage[id];
obj = this.storage[id]; this.storage.Remove(id);
this.storage.Remove(id);
}
}
if(obj != null) {
obj.markAsDeletedFromRegistry(); obj.markAsDeletedFromRegistry();
} }
} }

Loading…
Cancel
Save