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);
            }
        }
Ejemplo n.º 2
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();
            }
        }
Ejemplo n.º 3
0
 public void TestTryWrite()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         Assert.IsTrue(l.TryWrite(0));
         l.ReleaseWrite();
     }
 }
 /// <summary> Acquires the lock within the timeout or throws TimeoutException </summary>
 /// <exception cref="System.TimeoutException"/>
 public static WriteLock Acquire(ILockStrategy lck, int timeout)
 {
     if (!lck.TryWrite(timeout))
     {
         throw new TimeoutException();
     }
     return(new WriteLock(lck, true));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Closes the storage and clears memory used by the instance
 /// </summary>
 public void Dispose()
 {
     if (_disposed)
     {
         return;
     }
     try
     {
         bool locked = false;
         try
         {
             if (!IsReadOnly)
             {
                 locked = _selfLock.TryWrite(Math.Max(1000, LockTimeout));
                 if (locked)
                 {
                     CommitChanges(false);
                 }
             }
             _storage.Dispose();
         }
         finally
         {
             try
             {
                 if (_options.LogFile != null)
                 {
                     _options.LogFile.Dispose();
                 }
             }
             finally
             {
                 if (locked)
                 {
                     _selfLock.ReleaseWrite();
                 }
                 //Do not dispose, this may be a shared lock:
                 //_selfLock.Dispose();
             }
         }
     }
     finally
     {
         _disposed = true;
     }
 }
Ejemplo n.º 6
0
 public bool TryWrite(int timeout)
 {
     if (!_lock.TryWrite(timeout))
     {
         return(false);
     }
     AddThreadCount(0, 1);
     return(true);
 }
Ejemplo n.º 7
0
 public bool TryWrite(int timeout)
 {
     if (!_lock.TryWrite(timeout))
     {
         return(false);
     }
     AddCount(ref _factory.MaxWriterCount, ref _factory.CurrentWriterCount, ref _factory.TotalWriterCount);
     return(true);
 }
Ejemplo n.º 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);
        }
 public void ReadToWriteFails()
 {
     using (ILockStrategy l = LockFactory.Create())
         using (l.Read())
             Assert.IsFalse(l.TryWrite(10));
 }
 /// <summary> Acquires a read lock on the resource </summary>
 public WriteLock(ILockStrategy lck, int timeout)
 {
     _lock    = lck;
     _hasLock = lck.TryWrite(timeout);
 }
 /// <summary> Acquires a read lock on the resource </summary>
 public WriteLock(ILockStrategy lck, int timeout)
 {
     _lock = lck;
     _hasLock = lck.TryWrite(timeout);
 }
 /// <summary> Acquires the lock within the timeout or throws TimeoutException </summary>
 /// <exception cref="System.TimeoutException"/>
 public static WriteLock Acquire(ILockStrategy lck, int timeout)
 {
     if(!lck.TryWrite(timeout)) throw new TimeoutException();
     return new WriteLock(lck, true);
 }