/// <summary> /// This method borrows a lock from the lowest layer (lock store). /// </summary> /// <param name="key">Generic key against which the lock is maintained</param> /// <returns></returns> private SlimLockWrapper BorrowLockObject(TKey key, bool createNew) { SlimLockWrapper retInstance = null; lock (_keyLockTable){ if (_keyLockTable.ContainsKey(key)) { retInstance = _keyLockTable[key]; } if (retInstance == null && !createNew) { return(null); } if (retInstance == null) { retInstance = _keyLockTable[key] = new SlimLockWrapper(_policy); } else if (retInstance.MarkedDeleted) { retInstance = _keyLockTable[key] = new SlimLockWrapper(_policy); } retInstance.IncrementRef(); return(retInstance); } }
private void GetLock(TKey key) //, LockMode lockMode) { SlimLockWrapper slimlock = BorrowLockObject(key, true); slimlock.GetLock(); }
private SlimLockWrapper GetLockObject(TKey key) { SlimLockWrapper retInstance = null; lock (_keyLockTable) { if (_keyLockTable.ContainsKey(key)) { retInstance = _keyLockTable[key]; } return(retInstance); } }
private void ReleaseLock(TKey key)//, LockMode lockMode) { SlimLockWrapper lockObject = GetLockObject(key); if(lockObject == null) return; lockObject.ReleaseLock(); ReturnLockObject(key, lockObject); }
private void GetLock(TKey key, LockMode lockMode) { SlimLockWrapper slimlock = BorrowLockObject(key, true); switch (lockMode) { case LockMode.Reader: slimlock.GetReaderLock(); return; case LockMode.Write: slimlock.GetWriterLock(); return; } }
/// <summary> /// This method returns a lock object to the lowest layer (lock store). /// </summary> /// <param name="key">Generic key against which the lock is maintained.</param> /// <param name="lockObject">The lock value stored.</param> private void ReturnLockObject(TKey key, SlimLockWrapper lockObject) { lock (_keyLockTable){ lockObject.DecrementRef(); if (lockObject.MarkedDeleted) { SlimLockWrapper outValue; if (_keyLockTable.TryGetValue(key, out outValue) && lockObject == outValue) { _keyLockTable.Remove(key); } } } }
private void ReleaseLock(TKey key, LockMode lockMode) { SlimLockWrapper lockObject = GetLockObject(key); if (lockObject == null) { return; } switch (lockMode) { case LockMode.Reader: lockObject.ReleaseReaderLock(); break; case LockMode.Write: lockObject.ReleaseWriterLock(); break; } ReturnLockObject(key, lockObject); }
/// <summary> /// This method borrows a lock from the lowest layer (lock store). /// </summary> /// <param name="key">Generic key against which the lock is maintained</param> /// <returns></returns> private SlimLockWrapper BorrowLockObject(TKey key, bool createNew) { SlimLockWrapper retInstance; lock (_keyLockTable) { _keyLockTable.TryGetValue(key, out retInstance); if (retInstance == null && !createNew) { return null; } if (retInstance == null) { retInstance = _keyLockTable[key] = new SlimLockWrapper();//_policy); } else if (retInstance.MarkedDeleted){ retInstance = _keyLockTable[key] = new SlimLockWrapper();// _policy); } retInstance.IncrementRef(); return retInstance; } }