Exemplo n.º 1
0
        public void TestDebugFactoryRecursiveOptions()
        {
            DebugLockFactory <SimpleReadWriteLocking> factory = new DebugLockFactory <SimpleReadWriteLocking>(
                false, 0, 1, false, 1);

            using (ILockStrategy lck = factory.Create())
            {
                using (lck.Write())
                    using (lck.Write()) //second lock, allow recurse 1 time as per constructor
                    {
                        try { using (lck.Write()) { Assert.Fail(); } }
                        catch (Exception ex)
                        {
                            Assert.IsTrue(ex is DebugAssertionFailedException);//nesting prohibited by debug lock
                        }
                    }

                using (lck.Read())
                    using (lck.Read()) //second lock, allow recurse 1 time as per constructor
                    {
                        try { using (lck.Read()) { Assert.Fail(); } }
                        catch (Exception ex)
                        {
                            Assert.IsTrue(ex is DebugAssertionFailedException);//nesting prohibited by debug lock
                        }
                    }
            }
        }
 public override void UpdateNode(NodePin node)
 {
     if (node.IsDeleted)
     {
         using (_lock.Write(base.Options.LockTimeout))
             _list.Remove(node.Handle.StoreHandle);
     }
 }
Exemplo n.º 3
0
 public void Enqueue(string value)
 {
     using (_writeLock.Write())
     {
         _write.WriteLine(value);
         _write.Flush();
     }
 }
Exemplo n.º 4
0
        public void TestWriteRecursion()
        {
            ILockStrategy l = LockFactory.Create();

            using (l.Write())
                using (l.Write())
                    using (l.Write())
                    { }
        }
        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);
            }
        }
Exemplo n.º 6
0
            protected Node CreateRoot(NodeHandle rootHandle)
            {
                NodeHandle hChild;

                using (NodeTransaction t = BeginTransaction())
                {
                    using (NodePin child = t.Create(LockType.Insert, true))
                    {
                        hChild = child.Handle;
                        t.Commit();
                    }
                }

                object   refobj;
                RootNode rootNode = new RootNode(rootHandle.StoreHandle);

                ILockStrategy lck = CreateLock(rootHandle, out refobj);

                using (lck.Write(Options.LockTimeout))
                {
                    using (NodePin rootPin = new NodePin(rootHandle, lck, LockType.Insert, LockType.Insert, refobj, rootNode, null))
                        using (NodeTransaction t = BeginTransaction())
                        {
                            rootNode = (RootNode)t.BeginUpdate(rootPin);
                            rootNode.ReplaceChild(0, null, hChild);
                            t.Commit();
                        }
                }

                return(rootNode);
            }
Exemplo n.º 7
0
 void OnChanged()
 {
     if (_options.TransactionLogLimit > 0 && _options.LogFile != null)
     {
         if (_options.LogFile.Size > _options.TransactionLogLimit)
         {
             using (_selfLock.Write(LockTimeout))
             {
                 if (_options.LogFile.Size > _options.TransactionLogLimit)
                 {
                     CommitChanges(false);
                 }
             }
         }
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// Locks the collection and replaces the underlying storage dictionary.
 /// </summary>
 public IList <T> ReplaceStorage(IList <T> newStorage)
 {
     using (_lock.Write())
     {
         IList <T> storage = _store;
         _store = Check.NotNull(newStorage);
         return(storage);
     }
 }
Exemplo n.º 9
0
            CacheEntry GetCache(NodeHandle handle, bool isNew)
            {
                Utils.WeakReference <CacheEntry> weakRef;
                CacheEntry entry = null;

                if (!isNew)
                {
                    if (handle.TryGetCache(out weakRef) && weakRef != null && weakRef.TryGetTarget(out entry))
                    {
                        return(entry);
                    }

                    using (_cacheLock.Read(base.Options.LockTimeout))
                    {
                        if (_cache.TryGetValue(handle, out weakRef))
                        {
                            if (!weakRef.TryGetTarget(out entry))
                            {
                                using (new SafeLock <DeadlockException>(weakRef))
                                {
                                    if (!weakRef.TryGetTarget(out entry))
                                    {
                                        weakRef.Target = entry = new CacheEntry(this, handle);
                                    }
                                    handle.SetCacheEntry(weakRef);
                                }
                            }
                        }
                    }
                }
                if (entry == null)
                {
                    using (_cacheLock.Write(base.Options.LockTimeout))
                    {
                        if (!_cache.TryGetValue(handle, out weakRef))
                        {
                            _cache.Add(handle, weakRef = new Utils.WeakReference <CacheEntry>(entry = new CacheEntry(this, handle)));
                            handle.SetCacheEntry(weakRef);
                        }
                        else
                        {
                            if (!weakRef.TryGetTarget(out entry))
                            {
                                using (new SafeLock <DeadlockException>(weakRef))
                                {
                                    if (!weakRef.TryGetTarget(out entry))
                                    {
                                        weakRef.Target = entry = new CacheEntry(this, handle);
                                    }
                                    handle.SetCacheEntry(weakRef);
                                }
                            }
                        }
                    }
                }
                Assert(entry != null, "Cache entry is null");
                _keepAlive.Add(entry);
                return(entry);
            }
Exemplo n.º 10
0
 public void TestYouCanDisposeWriteLock()
 {
     using (ILockStrategy l = LockFactory.Create())
         using (IDisposable w = l.Write())
         {
             w.Dispose();//since the using statement has the same boxed pointer to w, we are allowed to dispose
         }
 }
Exemplo n.º 11
0
 public bool TryDequeue(out string value)
 {
     using (_writeLock.Read())
         using (_readLock.Write())
         {
             value = _read.ReadLine();
         }
     return(value != null);
 }
 public virtual void TestWriteToReadRecursion()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         using (l.Write())
             using (l.Read())
                 using (l.Read())
                 { }
     }
 }
Exemplo n.º 13
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);
     }
 }
Exemplo n.º 14
0
 public void TestReadThenWrite()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         using (l.Read())
         { }
         using (l.Write())
         { }
     }
 }
Exemplo n.º 15
0
 public void TestWriteThenReadWithTimeout()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         using (l.Write(0))
         { }
         using (l.Read(0))
         { }
     }
 }
            public void Rollback()
            {
                ITransactable tstore = _store as ITransactable;

                if (tstore != null)
                {
                    using (_lock.Write())
                    {
                        _ordered.Clear();
                        _cache.Clear();
                        tstore.Rollback();
                    }
                }
            }
Exemplo n.º 17
0
 public void TestWrite()
 {
     using (ILockStrategy l = LockFactory.Create())
         using (l.Write())
         { }
 }
Exemplo n.º 18
0
 public void TestThreadedWriteTimeout()
 {
     using (ILockStrategy l = LockFactory.Create())
         using (new ThreadedWriter(l))
             using (l.Write(0)) { }
 }
Exemplo n.º 19
0
 protected virtual IDisposable Lock()
 {
     return(_lck.Write(100));
 }
Exemplo n.º 20
0
 /// <summary>
 /// Locks the collection and replaces the underlying storage dictionary.
 /// </summary>
 public IDictionary <TKey, TValue> ReplaceStorage(IDictionary <TKey, TValue> newStorage)
 {
     using (_lock.Write())
     {
         IDictionary <TKey, TValue> storage = _store;
         _store = Check.NotNull(newStorage);
         return(storage);
     }
 }