public void GetWaitingThreadsIncludesWaitingThreads(bool isFair)
        {
            PublicReentrantLock myLock = new PublicReentrantLock(isFair);

            ICondition c = myLock.NewCondition();

            myLock.Lock();
            Assert.That(myLock.GetWaitingThreadsPublic(c).Count, Is.EqualTo(0));
            myLock.Unlock();
            Thread t1 = ThreadManager.StartAndAssertRegistered(
                "T1",
                delegate
            {
                myLock.Lock();
                Assert.That(myLock.GetWaitingThreadsPublic(c).Count, Is.EqualTo(0));
                c.Await();
                myLock.Unlock();
            });

            Thread.Sleep(Delays.Short);
            Thread t2 = ThreadManager.StartAndAssertRegistered(
                "T2",
                delegate
            {
                myLock.Lock();
                Assert.That(myLock.GetWaitingThreadsPublic(c).Count, Is.Not.EqualTo(0));
                c.Await();
                myLock.Unlock();
            });

            Thread.Sleep(Delays.Short);
            myLock.Lock();
            Assert.IsTrue(myLock.HasWaiters(c));
            Assert.IsTrue(myLock.GetWaitingThreadsPublic(c).Contains(t1));
            Assert.IsTrue(myLock.GetWaitingThreadsPublic(c).Contains(t2));
            c.SignalAll();
            myLock.Unlock();

            Thread.Sleep(Delays.Short);
            myLock.Lock();
            Assert.IsFalse(myLock.HasWaiters(c));
            Assert.IsTrue((myLock.GetWaitingThreadsPublic(c).Count == 0));
            myLock.Unlock();

            ThreadManager.JoinAndVerify();
        }
Beispiel #2
0
        public void TestGetWaitingThreads()
        {
            PublicReentrantLock locker = new PublicReentrantLock();
            Condition           c      = locker.NewCondition();
            Pair data = new Pair(locker, c);

            Thread t1 = new Thread(TestGetWaitingThreadsRunnable1);
            Thread t2 = new Thread(TestGetWaitingThreadsRunnable2);

            try
            {
                locker.Lock();
                Assert.IsTrue(locker.GetWaitingThreads(c).IsEmpty());
                locker.UnLock();
                t1.Start(data);
                Thread.Sleep(SHORT_DELAY_MS);
                t2.Start(data);
                Thread.Sleep(SHORT_DELAY_MS);
                locker.Lock();
                Assert.IsTrue(locker.HasWaiters(c));
                Assert.IsTrue(locker.GetWaitingThreads(c).Contains(t1));
                Assert.IsTrue(locker.GetWaitingThreads(c).Contains(t2));
                c.SignalAll();
                locker.UnLock();
                Thread.Sleep(SHORT_DELAY_MS);
                locker.Lock();
                Assert.IsFalse(locker.HasWaiters(c));
                Assert.IsTrue(locker.GetWaitingThreads(c).IsEmpty());
                locker.UnLock();
                t1.Join(SHORT_DELAY_MS);
                t2.Join(SHORT_DELAY_MS);
                Assert.IsFalse(t1.IsAlive);
                Assert.IsFalse(t2.IsAlive);
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
        public void TestGetWaitingThreads()
        {
            PublicReentrantLock locker = new PublicReentrantLock();
            Condition c = locker.NewCondition();
            Pair data = new Pair(locker, c);

            Thread t1 = new Thread(TestGetWaitingThreadsRunnable1);
            Thread t2 = new Thread(TestGetWaitingThreadsRunnable2);
    
            try
            {
                locker.Lock();
                Assert.IsTrue(locker.GetWaitingThreads(c).IsEmpty());
                locker.UnLock();
                t1.Start(data);
                Thread.Sleep(SHORT_DELAY_MS);
                t2.Start(data);
                Thread.Sleep(SHORT_DELAY_MS);
                locker.Lock();
                Assert.IsTrue(locker.HasWaiters(c));
                Assert.IsTrue(locker.GetWaitingThreads(c).Contains(t1));
                Assert.IsTrue(locker.GetWaitingThreads(c).Contains(t2));
                c.SignalAll();
                locker.UnLock();
                Thread.Sleep(SHORT_DELAY_MS);
                locker.Lock();
                Assert.IsFalse(locker.HasWaiters(c));
                Assert.IsTrue(locker.GetWaitingThreads(c).IsEmpty());
                locker.UnLock();
                t1.Join(SHORT_DELAY_MS);
                t2.Join(SHORT_DELAY_MS);
                Assert.IsFalse(t1.IsAlive);
                Assert.IsFalse(t2.IsAlive);
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }