public void TestWriterBlocksReader()
        {
            using (ILockStrategy l = LockFactory.Create())
            {
                Assert.IsTrue(l.TryRead(0));
                l.ReleaseRead();

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

                Assert.IsTrue(l.TryRead(0));
                l.ReleaseRead();
            }
        }
        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);
            }
        }
示例#3
0
 public void TestTryRead()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         Assert.IsTrue(l.TryRead(0));
         l.ReleaseRead();
     }
 }
示例#4
0
 /// <summary> Acquires the lock within the timeout or throws TimeoutException </summary>
 /// <exception cref="System.TimeoutException"/>
 public static ReadLock Acquire(ILockStrategy lck, int timeout)
 {
     if (!lck.TryRead(timeout))
     {
         throw new TimeoutException();
     }
     return(new ReadLock(lck, true));
 }
 public bool TryRead(int timeout)
 {
     if (!_lock.TryRead(timeout))
     {
         return(false);
     }
     AddThreadCount(1, 0);
     return(true);
 }
示例#6
0
 public bool TryRead(int timeout)
 {
     if (!_lock.TryRead(timeout))
     {
         return(false);
     }
     AddCount(ref _factory.MaxReaderCount, ref _factory.CurrentReaderCount, ref _factory.TotalReaderCount);
     return(true);
 }
        public virtual void TestReaderAllowsReader()
        {
            using (ILockStrategy l = LockFactory.Create())
            {
                using (new ThreadedReader(l))
                    Assert.IsTrue(l.TryRead(0));

                l.ReleaseRead();
            }
        }
示例#8
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);
        }
示例#9
0
 /// <summary> Acquires a read lock on the resource </summary>
 public ReadLock(ILockStrategy lck, int timeout)
 {
     _lock    = lck;
     _hasLock = lck.TryRead(timeout);
 }
 /// <summary> Acquires a read lock on the resource </summary>
 public ReadLock(ILockStrategy lck, int timeout)
 {
     _lock = lck;
     _hasLock = lck.TryRead(timeout);
 }
 /// <summary> Acquires the lock within the timeout or throws TimeoutException </summary>
 /// <exception cref="System.TimeoutException"/>
 public static ReadLock Acquire(ILockStrategy lck, int timeout)
 {
     if (!lck.TryRead(timeout)) throw new TimeoutException();
     return new ReadLock(lck, true);
 }