예제 #1
0
            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);
                }
            }
예제 #2
0
        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));
        }
예제 #3
0
        public void TestLockedGet()
        {
            var ld = new LockingDictionary <int, string>
            {
                [1] = "one",
                [2] = "two"
            };

            ld.Add(10, "ten");
            _TestDictionary(ld.Locked);
        }
예제 #4
0
        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]);
        }