コード例 #1
0
        public void SharedTest1()
        {
            var commandLock = new CommandLock();

            try
            {
                // should be able to aquire multiple shared locks
                Task t1 = commandLock.AquireShared();
                Assert.True(t1.IsCompleted);

                Task t2 = commandLock.AquireShared();
                Assert.True(t2.IsCompleted);

                Task<ExclusiveLockToken> t3 = commandLock.AquireExclusive();
                Assert.False(t3.IsCompleted);

                commandLock.ReleaseShared();
                Assert.False(t3.IsCompleted);

                commandLock.ReleaseShared();
                Assert.True(t3.IsCompleted);
            }
            finally
            {
                commandLock.Close("Test complete");
            }
        }
コード例 #2
0
        public void ExclusiveThenSharedTest()
        {
            var commandLock = new CommandLock();

            try
            {
                // Part 1 - try and aquire the shared lock while the exclusive lock is held
                Task<ExclusiveLockToken> t1 = commandLock.AquireExclusive();
                Assert.True(t1.IsCompleted);

                Task t2 = commandLock.AquireShared();
                Assert.False(t2.IsCompleted);

                ExclusiveLockToken token = t1.Result;
                token.Close();
                Assert.True(t2.IsCompleted, "Closing t1 should signal t2");
                Assert.True(ExclusiveLockToken.IsNullOrClosed(token), "Closing the token should zero it out");

                commandLock.ReleaseShared();


                // Part 2 - release the exclusive lock before attempting to aquire the shared lock
                t1 = commandLock.AquireExclusive();
                Assert.True(t1.IsCompleted);

                t1.Result.Close();

                t2 = commandLock.AquireShared();
                Assert.True(t2.IsCompleted, "Shared lock should be immediately aquired");
            }
            finally
            {
                commandLock.Close("Test complete");
            }
        }
コード例 #3
0
ファイル: CommandLock.cs プロジェクト: lsgxeva/MIEngine
        public void Close()
        {
            int value = _value;
            if (value != 0)
            {
                var commandLock = _commandLock;
                _value = 0;
                _commandLock = null;

                commandLock.ReleaseExclusive(value);
            }
        }
コード例 #4
0
ファイル: CommandLock.cs プロジェクト: lsgxeva/MIEngine
        internal ExclusiveLockToken(CommandLock commandLock, int value)
        {
            // Either both commandLock and value are non-zero (non-Null case), or both are zero (Null case)
            if (commandLock == null)
                throw new ArgumentNullException("commandLock");

            if (value == 0)
                throw new ArgumentOutOfRangeException("value");

            _commandLock = commandLock;
            _value = value;
        }
コード例 #5
0
        public void Close()
        {
            int value = _value;

            if (value != 0)
            {
                var commandLock = _commandLock;
                _value       = 0;
                _commandLock = null;

                commandLock.ReleaseExclusive(value);
            }
        }
コード例 #6
0
ファイル: CommandLock.cs プロジェクト: lsgxeva/MIEngine
        public void ConvertToSharedLock()
        {
            int value = _value;
            if (value == 0)
            {
                throw new ObjectDisposedException("ExclusiveLockToken");
            }

            var commandLock = _commandLock;
            _value = 0;
            _commandLock = null;

            commandLock.ConvertExclusiveLockToShared(value);
        }
コード例 #7
0
        public void ConvertToSharedLock()
        {
            int value = _value;

            if (value == 0)
            {
                throw new ObjectDisposedException("ExclusiveLockToken");
            }

            var commandLock = _commandLock;

            _value       = 0;
            _commandLock = null;

            commandLock.ConvertExclusiveLockToShared(value);
        }
コード例 #8
0
        internal ExclusiveLockToken(CommandLock commandLock, int value)
        {
            // Either both commandLock and value are non-zero (non-Null case), or both are zero (Null case)
            if (commandLock == null)
            {
                throw new ArgumentNullException("commandLock");
            }

            if (value == 0)
            {
                throw new ArgumentOutOfRangeException("value");
            }

            _commandLock = commandLock;
            _value       = value;
        }
コード例 #9
0
ファイル: CommandLockTests.cs プロジェクト: lsgxeva/MIEngine
        public void ExclusiveTest1()
        {
            using (var commandLock = new CommandLock())
            {
                Task<ExclusiveLockToken> t1 = commandLock.AquireExclusive();
                Assert.True(t1.IsCompleted);

                Task<ExclusiveLockToken> t2 = commandLock.AquireExclusive();
                Assert.False(t2.IsCompleted);

                ExclusiveLockToken token = t1.Result;
                token.Close();
                Assert.True(t2.IsCompleted, "Closing t1 should signal t2");
                Assert.True(ExclusiveLockToken.IsNullOrClosed(token), "Closing the token should zero it out");
            }
        }
コード例 #10
0
        public void CloseAbortsOperationsTest()
        {
            Task[] pendingTasks = new Task[2];

            var commandLock = new CommandLock();

            try
            {
                Task<ExclusiveLockToken> t1 = commandLock.AquireExclusive();
                Assert.True(t1.IsCompleted);

                pendingTasks[0] = commandLock.AquireExclusive();
                Assert.False(pendingTasks[0].IsCompleted);

                pendingTasks[1] = commandLock.AquireShared();
                Assert.False(pendingTasks[1].IsCompleted);
            }
            finally
            {
                commandLock.Close("CloseAbortsOperationsTest complete");
            }

            foreach (Task t in pendingTasks)
            {
                Assert.True(t.IsCompleted);
                Assert.IsType(typeof(DebuggerDisposedException), t.Exception.InnerException);
                Assert.Equal("CloseAbortsOperationsTest complete", t.Exception.InnerException.Message);
            }
        }
コード例 #11
0
        public void ConvertToSharedLockTest2()
        {
            // NOTE: This test covers the case that there are NOT pending shared locks
            var commandLock = new CommandLock();

            try
            {
                Task<ExclusiveLockToken> t1 = commandLock.AquireExclusive();
                Assert.True(t1.IsCompleted);

                ExclusiveLockToken token = t1.Result;
                token.ConvertToSharedLock();
                Assert.True(ExclusiveLockToken.IsNullOrClosed(token), "ConvertToSharedLock should have nulled out the token");

                Task t2 = commandLock.AquireShared();
                Assert.True(t2.IsCompleted);
            }
            finally
            {
                commandLock.Close("Test complete");
            }
        }
コード例 #12
0
ファイル: CommandLockTests.cs プロジェクト: lsgxeva/MIEngine
        public void SharedThenExclusiveTest()
        {
            using (var commandLock = new CommandLock())
            {
                // should be able to aquire multiple shared locks
                Task t1 = commandLock.AquireShared();
                Assert.True(t1.IsCompleted);

                Task t2 = commandLock.AquireShared();
                Assert.True(t2.IsCompleted);

                commandLock.ReleaseShared();
                commandLock.ReleaseShared();

                Task<ExclusiveLockToken> t3 = commandLock.AquireExclusive();
                Assert.True(t3.IsCompleted);
            }
        }
コード例 #13
0
ファイル: CommandLockTests.cs プロジェクト: lsgxeva/MIEngine
        public void CloseAbortsOperationsTest()
        {
            Task[] pendingTasks = new Task[2];

            using (var commandLock = new CommandLock())
            {
                Task<ExclusiveLockToken> t1 = commandLock.AquireExclusive();
                Assert.True(t1.IsCompleted);

                pendingTasks[0] = commandLock.AquireExclusive();
                Assert.False(pendingTasks[0].IsCompleted);

                pendingTasks[1] = commandLock.AquireShared();
                Assert.False(pendingTasks[1].IsCompleted);

                commandLock.Close();
            }

            foreach (Task t in pendingTasks)
            {
                Assert.True(t.IsCompleted);
                Assert.IsType(typeof(ObjectDisposedException), t.Exception.InnerException);
            }
        }