public void Dispose() { lock (locks) { LockItem li; if (!locks.TryGetValue(key, out li)) { return; } if (li != this) { // Something is wrong (comparer is not stable?), but we cannot loose the completions sources while (li.Next != null) { li = li.Next; } li.Next = Next; return; } if (Next == null) { locks.Remove(key); return; } locks[key] = Next; } Next.Continue(); }
public Task <IDisposable> GetLock(T key) { LockItem nextLi = new LockItem(locks, key); try { bool continueImmediately = false; lock (locks) { LockItem li; if (!locks.TryGetValue(key, out li)) { locks.Add(key, nextLi); continueImmediately = true; } else { while (li.Next != null) { li = li.Next; } li.Next = nextLi; } } if (continueImmediately) { nextLi.Continue(); } } catch (Exception e) { nextLi.Error(e); } return(nextLi.GetTask()); }
/// <summary> /// Waits for and acquires a lock on the specified key. Dispose the returned value to release the lock. /// </summary> /// <param name="key"></param> public Task <IDisposable> GetLock(T key) { // ReSharper disable once InconsistentlySynchronizedField - by design var nextLi = new LockItem(locks, key); try { var continueImmediately = false; lock (locks) { if (!locks.TryGetValue(key, out var li)) { locks.Add(key, nextLi); continueImmediately = true; } else { while (li.Next != null) { li = li.Next; } li.Next = nextLi; } } if (continueImmediately) { nextLi.Continue(); } } catch (Exception e) { nextLi.Error(e); } return(nextLi.GetTask()); }