public SlimUpgradeableReadLockHolder(ReaderWriterLockSlim locker, bool waitForLock, bool wasLockAlreadyHelf) { this.locker = locker; if (wasLockAlreadyHelf) { lockAcquired = true; wasLockAlreadyHeld = true; return; } if(waitForLock) { locker.EnterUpgradeableReadLock(); lockAcquired = true; return; } lockAcquired = locker.TryEnterUpgradeableReadLock(0); }
public static void Dispose() { ReaderWriterLockSlim rwls; rwls = new ReaderWriterLockSlim(); rwls.Dispose(); Assert.Throws<ObjectDisposedException>(() => rwls.TryEnterReadLock(0)); Assert.Throws<ObjectDisposedException>(() => rwls.TryEnterUpgradeableReadLock(0)); Assert.Throws<ObjectDisposedException>(() => rwls.TryEnterWriteLock(0)); rwls.Dispose(); for (int i = 0; i < 3; i++) { rwls = new ReaderWriterLockSlim(); switch (i) { case 0: rwls.EnterReadLock(); break; case 1: rwls.EnterUpgradeableReadLock(); break; case 2: rwls.EnterWriteLock(); break; } Assert.Throws<SynchronizationLockException>(() => rwls.Dispose()); } }
public void RecursiveUpgradeableReadLockTest () { var v = new ReaderWriterLockSlim (LockRecursionPolicy.SupportsRecursion); Assert.IsTrue (v.TryEnterUpgradeableReadLock (100), "#1"); Assert.IsTrue (v.TryEnterUpgradeableReadLock (100), "#2"); Assert.IsTrue (v.TryEnterUpgradeableReadLock (100), "#3"); Assert.AreEqual (3, v.RecursiveUpgradeCount); }
public void TryEnterUpgradeableReadLock_OutOfRange () { var v = new ReaderWriterLockSlim (); try { v.TryEnterUpgradeableReadLock (-2); Assert.Fail ("1"); } catch (ArgumentOutOfRangeException) { } try { v.TryEnterUpgradeableReadLock (TimeSpan.MaxValue); Assert.Fail ("2"); } catch (ArgumentOutOfRangeException) { } try { v.TryEnterUpgradeableReadLock (TimeSpan.MinValue); Assert.Fail ("3"); } catch (ArgumentOutOfRangeException) { } }
public static void EnterExit() { using (ReaderWriterLockSlim rwls = new ReaderWriterLockSlim()) { Assert.False(rwls.IsReadLockHeld); rwls.EnterReadLock(); Assert.True(rwls.IsReadLockHeld); rwls.ExitReadLock(); Assert.False(rwls.IsReadLockHeld); Assert.False(rwls.IsUpgradeableReadLockHeld); rwls.EnterUpgradeableReadLock(); Assert.True(rwls.IsUpgradeableReadLockHeld); rwls.ExitUpgradeableReadLock(); Assert.False(rwls.IsUpgradeableReadLockHeld); Assert.False(rwls.IsWriteLockHeld); rwls.EnterWriteLock(); Assert.True(rwls.IsWriteLockHeld); rwls.ExitWriteLock(); Assert.False(rwls.IsWriteLockHeld); Assert.False(rwls.IsUpgradeableReadLockHeld); rwls.EnterUpgradeableReadLock(); Assert.False(rwls.IsWriteLockHeld); Assert.True(rwls.IsUpgradeableReadLockHeld); rwls.EnterWriteLock(); Assert.True(rwls.IsWriteLockHeld); rwls.ExitWriteLock(); Assert.False(rwls.IsWriteLockHeld); Assert.True(rwls.IsUpgradeableReadLockHeld); rwls.ExitUpgradeableReadLock(); Assert.False(rwls.IsUpgradeableReadLockHeld); Assert.True(rwls.TryEnterReadLock(0)); rwls.ExitReadLock(); Assert.True(rwls.TryEnterReadLock(Timeout.InfiniteTimeSpan)); rwls.ExitReadLock(); Assert.True(rwls.TryEnterUpgradeableReadLock(0)); rwls.ExitUpgradeableReadLock(); Assert.True(rwls.TryEnterUpgradeableReadLock(Timeout.InfiniteTimeSpan)); rwls.ExitUpgradeableReadLock(); Assert.True(rwls.TryEnterWriteLock(0)); rwls.ExitWriteLock(); Assert.True(rwls.TryEnterWriteLock(Timeout.InfiniteTimeSpan)); rwls.ExitWriteLock(); } }
public static void WriterToUpgradeableReaderChain() { using (AutoResetEvent are = new AutoResetEvent(false)) using (ReaderWriterLockSlim rwls = new ReaderWriterLockSlim()) { rwls.EnterWriteLock(); Task t = Task.Factory.StartNew(() => { Assert.False(rwls.TryEnterUpgradeableReadLock(TimeSpan.FromMilliseconds(10))); Task.Run(() => are.Set()); // ideally this won't fire until we've called EnterReadLock, but it's a benign race in that the test will succeed either way rwls.EnterUpgradeableReadLock(); rwls.ExitUpgradeableReadLock(); }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); are.WaitOne(); rwls.ExitWriteLock(); t.GetAwaiter().GetResult(); } }
public static void InvalidTimeouts() { using (ReaderWriterLockSlim rwls = new ReaderWriterLockSlim()) { Assert.Throws<ArgumentOutOfRangeException>(() => rwls.TryEnterReadLock(-2)); Assert.Throws<ArgumentOutOfRangeException>(() => rwls.TryEnterUpgradeableReadLock(-3)); Assert.Throws<ArgumentOutOfRangeException>(() => rwls.TryEnterWriteLock(-4)); Assert.Throws<ArgumentOutOfRangeException>(() => rwls.TryEnterReadLock(TimeSpan.MaxValue)); Assert.Throws<ArgumentOutOfRangeException>(() => rwls.TryEnterUpgradeableReadLock(TimeSpan.MinValue)); Assert.Throws<ArgumentOutOfRangeException>(() => rwls.TryEnterWriteLock(TimeSpan.FromMilliseconds(-2))); } }
/// <summary> /// /// </summary> /// <param name="readerWriterLockSlim"></param> /// <returns></returns> public static bool QuickTryEnterUpgradeableReadLock( ReaderWriterLockSlim readerWriterLockSlim ) { return readerWriterLockSlim.TryEnterUpgradeableReadLock( s_TimeSpan ); }
/// <summary> /// Acquires an upgradeable read lock on a lock object. If the lock object already /// holds a read lock, no new lock is acquired and no lock will be released. /// </summary> /// <param name="rwlock">Lock object to work with. If this is null, no lock will be acquired.</param> public UpgradeableReadLock(ReaderWriterLockSlim rwlock) { this.rwlock = rwlock; if (rwlock == null) wasLocked = true; else wasLocked = rwlock.IsUpgradeableReadLockHeld || rwlock.IsWriteLockHeld; if (!wasLocked && !rwlock.TryEnterUpgradeableReadLock((ReadLockTimeout + WriteLockTimeout) / 2)) { throw new InvalidOperationException("The upgradeable reader lock could not be acquired."); } }
/// <summary> /// The get read lock. /// </summary> /// <param name="locks"> The locks. </param> public static void GetReadLock(ReaderWriterLockSlim locks) { bool lockAcquired = false; while (!lockAcquired) { lockAcquired = locks.TryEnterUpgradeableReadLock(1); } }