Example #1
0
        public void Locksmith_WhenWriteLockIsDowngraded_AllowsNewReadersToEnter()
        {
            const int        successValue        = 25;
            const int        initialValue        = 10;
            int              readResult          = initialValue;
            ManualResetEvent readerFinishedEvent = new ManualResetEvent(false);

            Locksmith.EnterWriteLock(ref lockState, writeLockTestObject);
            Locksmith.Downgrade(ref lockState, readLockTestObject);
            {
                StartNewThreadThatRuns(SetReadResultToFailureValue);
                readerFinishedEvent.WaitOne();
                Assert.That(readResult, Is.EqualTo(successValue));
            }
            Locksmith.ExitReadLock(ref lockState, readLockTestObject, writeLockTestObject);

            return;


            void SetReadResultToFailureValue()
            {
                Locksmith.EnterReadLock(ref lockState, readLockTestObject);
                {
                    readResult = successValue;
                }
                Locksmith.ExitReadLock(ref lockState, readLockTestObject, writeLockTestObject);

                readerFinishedEvent.Set();
            }
        }
Example #2
0
        public void Locksmith_WhenWriteLockIsDowngraded_ThrowsIfExitWriteLockIsCalled()
        {
            Locksmith.EnterWriteLock(ref lockState, writeLockTestObject);
            Locksmith.Downgrade(ref lockState, readLockTestObject);
            Assert.Throws <SynchronizationLockException>(TryToCallExitWriteLock);
            Locksmith.ExitReadLock(ref lockState, readLockTestObject, writeLockTestObject);

            return;


            void TryToCallExitWriteLock() =>
            Locksmith.ExitWriteLock(ref lockState, readLockTestObject, writeLockTestObject);
        }