public void TestHasWaiters()
        {
            Mutex sync = new Mutex();

            AbstractQueuedSynchronizer.ConditionObject c = sync.NewCondition();
            Pair   data = new Pair(sync, c);
            Thread t    = new Thread(TestHasWaitersRunnable);

            try
            {
                t.Start(data);
                Thread.Sleep(SHORT_DELAY_MS);
                sync.Acquire(1);
                Assert.IsTrue(sync.HasWaiters(c));
                Assert.AreEqual(1, sync.GetWaitQueueLength(c));
                c.Signal();
                sync.Release(1);
                Thread.Sleep(SHORT_DELAY_MS);
                sync.Acquire(1);
                Assert.IsFalse(sync.HasWaiters(c));
                Assert.AreEqual(0, sync.GetWaitQueueLength(c));
                sync.Release(1);
                t.Join(SHORT_DELAY_MS);
                Assert.IsFalse(t.IsAlive);
            }
            catch (Exception)
            {
                UnexpectedException();
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Acquire read permission.
 /// </summary>
 public void AcquireReader()
 {
     // Only allow a single thread through at a time by using a turnstile system
     _turnStile.Acquire();
     _turnStile.Release();
     // Acquire the Switch object, which governs controls over the write permission Mutex
     _readPermission.Acquire();
 }
Exemplo n.º 3
0
    /// <summary>
    /// Arrive at the Barrier.
    /// </summary>
    /// <returns>
    /// Returns Boolean, representing whether or not all threads have arrived at the Barrier and are now synchronized.
    /// </returns>
    public Boolean Arrive()
    {
        Boolean result = false;

        // Thread arrives
        _turnStile.Acquire();
        lock (_lock)
        {
            _threadCount++;
            // If all threads have arrived, open the Barrier and let threads commence with activity
            if (_threadCount == _barrierLimit)
            {
                _actPermission.Release(_barrierLimit);
                result = true;
            }
            else
            {
                _turnStile.Release();
            }
        }
        // Waiting threads acquire from Semaphore and wait for final thread to trip Barrier
        _actPermission.Acquire();
        lock (_lock)
        {
            // Let the threads leave
            _threadCount--;
            // If the last thread to leave, free the turnstile
            if (_threadCount == 0)
            {
                _turnStile.Release();
            }
        }
        return(result);
    }
        public void TestGetFirstQueuedThread()
        {
            Mutex  sync = new Mutex();
            Thread t1   = new Thread(InterruptedSyncRunnable);
            Thread t2   = new Thread(InterruptibleSyncRunnable);

            try
            {
                Assert.IsNull(sync.FirstQueuedThread);
                sync.Acquire(1);
                t1.Start(sync);
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.AreEqual(t1, sync.FirstQueuedThread);
                t2.Start(sync);
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.AreEqual(t1, sync.FirstQueuedThread);
                t1.Interrupt();
                Thread.Sleep(SHORT_DELAY_MS);
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.AreEqual(t2, sync.FirstQueuedThread);
                sync.Release(1);
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.IsNull(sync.FirstQueuedThread);
                t1.Join();
                t2.Join();
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
        public void TestHasContended()
        {
            Mutex  sync = new Mutex();
            Thread t1   = new Thread(InterruptedSyncRunnable);
            Thread t2   = new Thread(InterruptibleSyncRunnable);

            try
            {
                Assert.IsFalse(sync.HasContended);
                sync.Acquire(1);
                t1.Start(sync);
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.IsTrue(sync.HasContended);
                t2.Start(sync);
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.IsTrue(sync.HasContended);
                t1.Interrupt();
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.IsTrue(sync.HasContended);
                sync.Release(1);
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.IsTrue(sync.HasContended);
                t1.Join();
                t2.Join();
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
        public void TestGetSharedQueuedThreads()
        {
            Mutex  sync = new Mutex();
            Thread t1   = new Thread(InterruptedSyncRunnable);
            Thread t2   = new Thread(InterruptibleSyncRunnable);

            try {
                Assert.IsTrue(sync.SharedQueuedThreads.IsEmpty());
                sync.Acquire(1);
                Assert.IsTrue(sync.SharedQueuedThreads.IsEmpty());
                t1.Start(sync);
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.IsTrue(sync.SharedQueuedThreads.IsEmpty());
                t2.Start(sync);
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.IsTrue(sync.SharedQueuedThreads.IsEmpty());
                t1.Interrupt();
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.IsTrue(sync.SharedQueuedThreads.IsEmpty());
                sync.Release(1);
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.IsTrue(sync.SharedQueuedThreads.IsEmpty());
                t1.Join();
                t2.Join();
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
        public void TestAwaitUnInterruptibly()
        {
            Mutex sync = new Mutex();

            AbstractQueuedSynchronizer.ConditionObject c = sync.NewCondition();
            Pair data = new Pair(sync, c);

            Thread t = new Thread(TestAwaitUnInterruptiblyRunnable);

            try
            {
                t.Start(data);
                Thread.Sleep(SHORT_DELAY_MS);
                t.Interrupt();
                sync.Acquire(1);
                c.Signal();
                sync.Release(1);
                t.Join(SHORT_DELAY_MS);
                Assert.IsFalse(t.IsAlive);
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
        public void TestSignalAll()
        {
            Mutex sync = new Mutex();

            AbstractQueuedSynchronizer.ConditionObject c = sync.NewCondition();
            Pair data = new Pair(sync, c);

            Thread t1 = new Thread(TestSignalAllRunnable1);
            Thread t2 = new Thread(TestSignalAllRunnable2);

            try
            {
                t1.Start(data);
                t2.Start(data);
                Thread.Sleep(SHORT_DELAY_MS);
                sync.Acquire(1);
                c.SignalAll();
                sync.Release(1);
                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 TestAcquire()
        {
            Mutex rl = new Mutex();

            rl.Acquire(1);
            Assert.IsTrue(rl.AccessIsHeldExclusively());
            rl.Release(1);
            Assert.IsFalse(rl.AccessIsHeldExclusively());
        }
Exemplo n.º 10
0
 /// <summary>
 /// Acquire write permission.
 /// </summary>
 public void AcquireWriter()
 {
     // Use a seperate turnstile for threads that seek write permission. Writing threads will queue here
     _writeTurnStile.Acquire();
     // Enter the standard turnstile, acquire the write permission Mutex and release the standard turnstile to allow potential readers to queue
     _turnStile.Acquire();
     _writePermission.Acquire();
     _turnStile.Release();
 }
Exemplo n.º 11
0
 public void SetSail()
 {
     _permission.Acquire();
     //Thread.Sleep(new Random().Next(500, 1500));
     Console.WriteLine("\t\t" + Thread.CurrentThread.Name + ": Boat full! Let's go for a boat ride!");
     //Thread.Sleep(new Random().Next(1500, 3000));
     Console.WriteLine("\t\t\t" + Thread.CurrentThread.Name + ": Boat ride has finished! Let new patrons board.");
     _boardPermission.Release();
 }
        public static void OtherThread()
        {
            writeMtx.Acquire();
            {
                Console.WriteLine("Inside OtherThread");
                Thread.Sleep(1000);

                Console.WriteLine("1 - Other");
                Thread.Sleep(1000);

                Console.WriteLine("2 - Other");
                Thread.Sleep(1000);

                Console.WriteLine("3 - Other");
                Thread.Sleep(1000);
            }
            Console.WriteLine("Attempting to release OtherThread writeMtx");
            writeMtx.Release();
        }
Exemplo n.º 13
0
        // The logic
        protected override void Run()
        {
            if (PhilosophersProfiler.debug)
            {
                Console.WriteLine("Inside run()!");
            }

            while (true)
            {
                // Some thinking
                Thread.Sleep((int)PhilosophersProfiler.timeThinking);

                if (PhilosophersProfiler.debug)
                {
                    Console.WriteLine(base.activeThread.Name + " Has finished thinking and wants to eat!");
                }

                // Attempting to start eating
                if (_startLeft)
                {
                    _leftChopstick.Acquire();
                    _rightChopstick.Acquire();
                }
                else
                {
                    _rightChopstick.Acquire();
                    _leftChopstick.Acquire();
                }

                // Eating
                if (PhilosophersProfiler.debug)
                {
                    Console.WriteLine(base.activeThread.Name + " Is now eating food!");
                }

                ++timesEating;

                Thread.Sleep((int)PhilosophersProfiler.timeEating);

                _rightChopstick.Release();
                _leftChopstick.Release();
            }
        }
        public void TestToString()
        {
            Mutex  sync = new Mutex();
            String us   = sync.ToString();

            Assert.IsTrue(us.IndexOf("State = 0") >= 0);
            sync.Acquire(1);
            String ls = sync.ToString();

            Assert.IsTrue(ls.IndexOf("State = 1") >= 0);
        }
        private void TestAwaitUnInterruptiblyRunnable(object state)
        {
            Pair  data = state as Pair;
            Mutex sync = data.first as Mutex;

            AbstractQueuedSynchronizer.ConditionObject c =
                data.second as AbstractQueuedSynchronizer.ConditionObject;

            sync.Acquire(1);
            c.AwaitUnInterruptibly();
            sync.Release(1);
        }
        private void TestGetStateRunnable(object state)
        {
            Mutex sync = state as Mutex;

            sync.Acquire(1);
            try
            {
                Thread.Sleep(SMALL_DELAY_MS);
            }
            catch (Exception e)
            {
                ThreadUnexpectedException(e);
            }
            sync.Release(1);
        }
        public void TestAwaitTimeout()
        {
            Mutex sync = new Mutex();

            AbstractQueuedSynchronizer.ConditionObject c = sync.NewCondition();
            try
            {
                sync.Acquire(1);
                Assert.IsFalse(c.Await(SHORT_DELAY_MS) > 0);
                sync.Release(1);
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
        public void TestAwaitUntilTimeout()
        {
            Mutex sync = new Mutex();

            AbstractQueuedSynchronizer.ConditionObject c = sync.NewCondition();

            try
            {
                sync.Acquire(1);
                DateTime deadline = DateTime.Now;
                Assert.IsFalse(c.AwaitUntil(deadline.AddSeconds(5)));
                sync.Release(1);
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
        public void TestAcquireTimedTimeout()
        {
            Mutex sync = new Mutex();

            sync.Acquire(1);
            Thread t = new Thread(TestAcquireTimedTimeoutRunnable);

            try
            {
                t.Start(sync);
                t.Join();
                sync.Release(1);
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
        public void TestTryAcquireWhenSynced()
        {
            Mutex sync = new Mutex();

            sync.Acquire(1);
            Thread t = new Thread(TestTryAcquireWhenSyncedRunnable);

            try
            {
                t.Start(sync);
                t.Join();
                sync.Release(1);
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
        public void TestThreadInterruptedException2()
        {
            Mutex sync = new Mutex();

            sync.Acquire(1);
            Thread t = new Thread(TestThreadInterruptedException2Rannable);

            try
            {
                t.Start(sync);
                t.Interrupt();
                t.Join();
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
        private void TestAwaitRunnable(object state)
        {
            Pair  data = state as Pair;
            Mutex sync = data.first as Mutex;

            AbstractQueuedSynchronizer.ConditionObject c =
                data.second as AbstractQueuedSynchronizer.ConditionObject;

            try
            {
                sync.Acquire(1);
                c.Await();
                sync.Release(1);
            }
            catch (ThreadInterruptedException e)
            {
                ThreadUnexpectedException(e);
            }
        }
        private void TestAwaitUntilInterruptRunnable(object state)
        {
            Pair  data = state as Pair;
            Mutex sync = data.first as Mutex;

            AbstractQueuedSynchronizer.ConditionObject c =
                data.second as AbstractQueuedSynchronizer.ConditionObject;

            try
            {
                sync.Acquire(1);
                c.AwaitUntil(DateTime.Now.AddMilliseconds(10000));
                sync.Release(1);
                ThreadShouldThrow();
            }
            catch (ThreadInterruptedException)
            {
            }
        }
        private void TestGetWaitQueueLengthRunnable2(object state)
        {
            Pair  data = state as Pair;
            Mutex sync = data.first as Mutex;

            AbstractQueuedSynchronizer.ConditionObject c = data.second as AbstractQueuedSynchronizer.ConditionObject;

            try
            {
                sync.Acquire(1);
                ThreadAssertTrue(sync.HasWaiters(c));
                ThreadAssertEquals(1, sync.GetWaitQueueLength(c));
                c.Await();
                sync.Release(1);
            }
            catch (ThreadInterruptedException e)
            {
                ThreadUnexpectedException(e);
            }
        }
        private void TestGetWaitingThreadsRunnable2(object state)
        {
            Pair  data = state as Pair;
            Mutex sync = data.first as Mutex;

            AbstractQueuedSynchronizer.ConditionObject c =
                data.second as AbstractQueuedSynchronizer.ConditionObject;

            try
            {
                sync.Acquire(1);
                ThreadAssertFalse(sync.GetWaitingThreads(c).IsEmpty());
                c.Await();
                sync.Release(1);
            }
            catch (ThreadInterruptedException e)
            {
                ThreadUnexpectedException(e);
            }
        }
        public void TestAcquireInterruptibly1()
        {
            Mutex sync = new Mutex();

            sync.Acquire(1);
            Thread t = new Thread(InterruptedSyncRunnable);

            try
            {
                t.Start(sync);
                Thread.Sleep(SHORT_DELAY_MS);
                t.Interrupt();
                Thread.Sleep(SHORT_DELAY_MS);
                sync.Release(1);
                t.Join();
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
        public void TestGetState()
        {
            Mutex sync = new Mutex();

            sync.Acquire(1);
            Assert.IsTrue(sync.AccessIsHeldExclusively());
            sync.Release(1);
            Assert.IsFalse(sync.AccessIsHeldExclusively());
            Thread t = new Thread(TestGetStateRunnable);

            try
            {
                t.Start(sync);
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.IsTrue(sync.AccessIsHeldExclusively());
                t.Join();
                Assert.IsFalse(sync.AccessIsHeldExclusively());
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
Exemplo n.º 28
0
 /// <summary>
 /// Acquire the Latch.
 /// </summary>
 public void Acquire()
 {
     //Acquire and immediately release the Mutex.
     _mutex.Acquire();
     _mutex.Release();
 }
Exemplo n.º 29
0
 public HeldMutex(Mutex instance)
 {
     this.instance = instance;
     instance.Acquire();
 }
Exemplo n.º 30
0
        public override void Visit(Acquire node)
        {
            Mutex mutex = thread.Pop().Read().GetAsMutex();

            mutex.Acquire(thread);
        }