public override void TestWriteCounter()
        {
            using (ILockStrategy l = LockFactory.Create())
            {
                Assert.AreEqual(0, l.WriteVersion);

                Assert.IsTrue(l.TryRead(0));
                l.ReleaseRead();
                Assert.AreEqual(0, l.WriteVersion);

                Assert.IsTrue(l.TryWrite(0));
                Assert.AreEqual(0, l.WriteVersion);
                l.ReleaseWrite();
                Assert.AreEqual(0, l.WriteVersion);

                using (l.Write())
                {
                    Assert.AreEqual(0, l.WriteVersion);
                    Assert.IsTrue(l.TryWrite(0));
                    // Once a nested write lock is acquired the real lock is obtained.
                    Assert.AreEqual(1, l.WriteVersion);
                    l.ReleaseWrite();
                    Assert.AreEqual(1, l.WriteVersion);
                }

                Assert.IsTrue(l.TryWrite(0));
                l.ReleaseWrite();
                Assert.AreEqual(1, l.WriteVersion);
            }
        }
 public void TestThreadedReadTimeout()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         using (new ThreadedWriter(l))
             using (l.Read(0))
             { }
     }
 }
Пример #3
0
        public void TestWriteRecursion()
        {
            ILockStrategy l = LockFactory.Create();

            using (l.Write())
                using (l.Write())
                    using (l.Write())
                    { }
        }
 public virtual void TestWriteToReadRecursion()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         using (l.Write())
             using (l.Read())
                 using (l.Read())
                 { }
     }
 }
        public virtual void TestReaderAllowsReader()
        {
            using (ILockStrategy l = LockFactory.Create())
            {
                using (new ThreadedReader(l))
                    Assert.IsTrue(l.TryRead(0));

                l.ReleaseRead();
            }
        }
Пример #6
0
 public void NoWriteIncrement()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         Assert.AreEqual(0, l.WriteVersion);
         using (l.Write())
             Assert.AreEqual(0, l.WriteVersion);
         Assert.AreEqual(0, l.WriteVersion);
     }
 }
Пример #7
0
            public override ILockStrategy CreateLock(NodeHandle handle, out object refobj)
            {
                NodeWithLock nlck = new NodeWithLock(null, LockFactory.Create());

                handle.SetCacheEntry(nlck);
                refobj = nlck;
                bool acquired = nlck.Lock.TryWrite(base.Options.LockTimeout);

                DeadlockException.Assert(acquired);
                return(nlck.Lock);
            }
Пример #8
0
        public void TestThreadedTryWrite()
        {
            using (ILockStrategy l = LockFactory.Create())
            {
                Assert.IsTrue(l.TryWrite(0));
                l.ReleaseWrite();

                using (new ThreadedWriter(l))
                    Assert.IsFalse(l.TryWrite(0));

                Assert.IsTrue(l.TryWrite(0));
                l.ReleaseWrite();
            }
        }
            protected override NodePin Lock(NodePin parent, LockType ltype, NodeHandle child)
            {
                ILockStrategy lck;

                if (!child.TryGetCache(out lck))
                {
                    using (_lock.Write(base.Options.LockTimeout))
                    {
                        if (!_list.TryGetValue(child.StoreHandle, out lck))
                        {
                            _list.Add(child.StoreHandle, lck = LockFactory.Create());
                            child.SetCacheEntry(lck);
                        }
                    }
                }

                bool success;

                if (ltype == LockType.Read)
                {
                    success = lck.TryRead(base.Options.LockTimeout);
                }
                else
                {
                    success = lck.TryWrite(base.Options.LockTimeout);
                }
                DeadlockException.Assert(success);
                try
                {
                    Node node;
                    success = Storage.TryGetNode(child.StoreHandle, out node, NodeSerializer);
                    Assert(success && node != null);

                    return(new NodePin(child, lck, ltype, ltype, lck, node, null));
                }
                catch
                {
                    if (ltype == LockType.Read)
                    {
                        lck.ReleaseRead();
                    }
                    else
                    {
                        lck.ReleaseWrite();
                    }
                    throw;
                }
            }
Пример #10
0
        public void DisposedWithWriters()
        {
            ILockStrategy  l      = LockFactory.Create();
            ThreadedWriter thread = new ThreadedWriter(l);

            try
            {
                l.Dispose();
            }
            finally
            {
                try { thread.Dispose(); }
                catch (ObjectDisposedException)
                { }
            }
        }
Пример #11
0
            protected override NodePin Lock(NodePin parent, LockType ltype, NodeHandle child)
            {
                NodeWithLock nlck;

                if (!child.TryGetCache(out nlck))
                {
                    child.SetCacheEntry(nlck = new NodeWithLock(null, LockFactory.Create()));
                }

                bool acquired;

                if (ltype == LockType.Read)
                {
                    acquired = nlck.Lock.TryRead(base.Options.LockTimeout);
                }
                else
                {
                    acquired = nlck.Lock.TryWrite(base.Options.LockTimeout);
                }
                DeadlockException.Assert(acquired);
                try
                {
                    if (nlck.Node == null)
                    {
                        using (new SafeLock <DeadlockException>(nlck, base.Options.LockTimeout))
                            Storage.TryGetNode(child.StoreHandle, out nlck.Node, NodeSerializer);
                    }

                    Assert(nlck.Node != null);
                    return(new NodePin(child, nlck.Lock, ltype, ltype, nlck, nlck.Node, null));
                }
                catch
                {
                    if (ltype == LockType.Read)
                    {
                        nlck.Lock.ReleaseRead();
                    }
                    else
                    {
                        nlck.Lock.ReleaseWrite();
                    }
                    throw;
                }
            }
            public override ILockStrategy CreateLock(NodeHandle handle, out object refobj)
            {
                ILockStrategy lck;

                using (_lock.Write(base.Options.LockTimeout))
                {
                    if (!_list.TryGetValue(handle.StoreHandle, out lck))
                    {
                        _list.Add(handle.StoreHandle, lck = LockFactory.Create());
                        handle.SetCacheEntry(lck);
                    }
                }

                refobj = null;
                bool acquired = lck.TryWrite(base.Options.LockTimeout);

                DeadlockException.Assert(acquired);
                return(lck);
            }
Пример #13
0
        public virtual void TestWriteCounter()
        {
            ILockStrategy l = LockFactory.Create();

            int count = l.WriteVersion;

            Assert.IsTrue(l.TryRead(0));
            l.ReleaseRead();
            Assert.AreEqual(count, l.WriteVersion);

            Assert.IsTrue(l.TryWrite(0));
            l.ReleaseWrite();
            Assert.AreNotEqual(count, l.WriteVersion);
            count = l.WriteVersion;

            Assert.IsTrue(l.TryWrite(0));
            l.ReleaseWrite();
            Assert.AreNotEqual(count, l.WriteVersion);
        }
            protected override void LoadStorage()
            {
                bool isNew;

                _root = new NodeHandle(Storage.OpenRoot(out isNew));
                _root.SetCacheEntry(LockFactory.Create());
                if (isNew)
                {
                    CreateRoot(_root);
                }

                Node rootNode;

                if (Storage.TryGetNode(_root.StoreHandle, out rootNode, NodeSerializer))
                {
                    _root.SetCacheEntry(LockFactory.Create());
                }

                Assert(rootNode != null, "Unable to load storage root.");
            }
 public void ReadToWriteFails()
 {
     using (ILockStrategy l = LockFactory.Create())
         using (l.Read())
             Assert.IsFalse(l.TryWrite(10));
 }
 public void TestExcessiveReleaseRead()
 {
     using (ILockStrategy l = LockFactory.Create())
         l.ReleaseRead();
 }
Пример #17
0
 private CacheManager(LockType lockType)
 {
     _lock = LockFactory.Create(lockType);
 }
Пример #18
0
 private PubSub(LockType lockType)
 {
     _lock = LockFactory.Create(lockType);
 }
Пример #19
0
 private DagStorage(LockType lockType)
 {
     _lock = LockFactory.Create(lockType);
 }