private LockingDictionary <StorageKey, ObjectDecorator> GetSingleKeySpace(DataBuffer keySpace) { var hash = keySpace.GetHashCode(); LockingDictionary <StorageKey, ObjectDecorator> keySpaceDct; // upgradeable blocks other upgradeable, so try read first using (_keySpaces.Lock(LockType.Read)) { if (_keySpaces.TryGetValue(hash, out keySpaceDct)) { return(keySpaceDct); } } // didn't find it, so now try upgradeable to write if need be using (_keySpaces.Lock(LockType.ReadUpgradable)) { if (!_keySpaces.TryGetValue(hash, out keySpaceDct)) { using (_keySpaces.Lock(LockType.Write)) { keySpaceDct = new LockingDictionary <StorageKey, ObjectDecorator>(); _keySpaces.Add(hash, keySpaceDct); } } return(keySpaceDct); } }
public void TestLockedSet() { var ld = new LockingDictionary <int, string>().Locked; Assert.ThrowsException <NotSupportedException>(() => ld[0] = "zero"); Assert.ThrowsException <NotSupportedException>(() => ld.Add(0, "zero")); Assert.ThrowsException <NotSupportedException>(() => ld.Clear()); Assert.ThrowsException <NotSupportedException>(() => ld.Remove(1)); }
public void TestLockedGet() { var ld = new LockingDictionary <int, string> { [1] = "one", [2] = "two" }; ld.Add(10, "ten"); _TestDictionary(ld.Locked); }
public void TestParentReferenceStability() { var ld = new LockingDictionary <int, string> { [1] = "one", [2] = "two" }; var locked = ld.Locked; ld[2] = "Two!"; ld.Add(4, "Four?"); Assert.IsTrue(locked.ContainsKey(4)); Assert.AreEqual("Two!", ld[2]); }