public void SignalOnWaitingThread()
        {
            WaitNode    node           = new WaitNode();
            IQueuedSync mockQueuedSync = MockRepository.GenerateMock <IQueuedSync>();

            node.Signal(mockQueuedSync);
            mockQueuedSync.AssertWasCalled(m => m.TakeOver(node));
        }
예제 #2
0
		public bool IsWaiting(Thread thread)
		{
			if (thread == null) throw new ArgumentNullException("thread");
			for (WaitNode node = _head; node != null; node = node.NextWaitNode)
			{
				if (node.IsWaiting && node.Owner == thread) return true;
			}
			return false;
		}
예제 #3
0
		public WaitNode Dequeue()
		{
			if (_head == null) return null;

		    WaitNode w = _head;
		    _head = w.NextWaitNode;
		    if (_head == null) _tail = null;
		    w.NextWaitNode = null;
		    return w;
		}
예제 #4
0
		public void Enqueue(WaitNode waitNode)
		{
			if (_tail == null)
			    _head = _tail = waitNode;
			else
			{
			    _tail.NextWaitNode = waitNode;
			    _tail = waitNode;
			}
		}
 public void WaitingThreads()
 {
     WaitNode node = new WaitNode();
     FIFOWaitQueue queue = new FIFOWaitQueue();
     Assert.AreEqual(0, queue.WaitingThreads.Count);
     Assert.IsFalse(queue.IsWaiting(Thread.CurrentThread));
     queue.Enqueue(node);
     Assert.AreEqual(1, queue.WaitingThreads.Count);
     Assert.IsTrue(queue.IsWaiting(Thread.CurrentThread));
 }
예제 #6
0
 public void Enqueue(WaitNode waitNode)
 {
     if (_tail == null)
         _head = _tail = waitNode;
     else
     {
         _tail.NextWaitNode = waitNode;
         _tail = waitNode;
     }
 }
예제 #7
0
        public WaitNode Dequeue()
        {
            if (_head == null) return null;

            WaitNode w = _head;
            _head = w.NextWaitNode;
            if (_head == null) _tail = null;
            w.NextWaitNode = null;
            return w;
        }
예제 #8
0
        public void WaitingThreads()
        {
            WaitNode      node  = new WaitNode();
            FIFOWaitQueue queue = new FIFOWaitQueue();

            Assert.AreEqual(0, queue.WaitingThreads.Count);
            Assert.IsFalse(queue.IsWaiting(Thread.CurrentThread));
            queue.Enqueue(node);
            Assert.AreEqual(1, queue.WaitingThreads.Count);
            Assert.IsTrue(queue.IsWaiting(Thread.CurrentThread));
        }
예제 #9
0
 public void Enqueue(WaitNode w)
 {
     if (_tail == null)
     {
         _head = _tail = w;
     }
     else
     {
         _tail.NextWaitNode = w;
         _tail = w;
     }
 }
 public void EnqueueAndDequeueElements()
 {
     WaitNode node = new WaitNode();
     WaitNode node1 = new WaitNode();
     FIFOWaitQueue queue = new FIFOWaitQueue();
     Assert.AreEqual(null, queue.Dequeue());
     queue.Enqueue(node1);
     queue.Enqueue(node);
     Assert.AreEqual(2, queue.Length);
     Assert.IsTrue(queue.HasNodes);
     Assert.AreEqual(node1, queue.Dequeue());
     Assert.AreEqual(node, queue.Dequeue());
     Assert.IsFalse(queue.HasNodes);
 }
예제 #11
0
        public void EnqueueAndDequeueElements()
        {
            WaitNode      node  = new WaitNode();
            WaitNode      node1 = new WaitNode();
            FIFOWaitQueue queue = new FIFOWaitQueue();

            Assert.AreEqual(null, queue.Dequeue());
            queue.Enqueue(node1);
            queue.Enqueue(node);
            Assert.AreEqual(2, queue.Length);
            Assert.IsTrue(queue.HasNodes);
            Assert.AreEqual(node1, queue.Dequeue());
            Assert.AreEqual(node, queue.Dequeue());
            Assert.IsFalse(queue.HasNodes);
        }
예제 #12
0
        public WaitNode Dequeue()
        {
            if (_head == null)
            {
                return(null);
            }

            WaitNode w = _head;

            _head = w.NextWaitNode;
            if (_head == null)
            {
                _tail = null;
            }
            w.NextWaitNode = null;
            return(w);
        }
예제 #13
0
 public void TakeOver(WaitNode node)
 {
 }
예제 #14
0
 public bool Recheck(WaitNode node)
 {
     return false;
 }
예제 #15
0
 private void DoWait(Action<WaitNode> action)
 {
     int holdCount = Lock.HoldCount;
     if (holdCount == 0)
     {
         throw new SynchronizationLockException();
     }
     WaitNode n = new WaitNode();
     _wq.Enqueue(n);
     for (int i = holdCount; i > 0; i--) Lock.Unlock();
     try
     {
         action(n);
     }
     finally
     {
         for (int i = holdCount; i > 0; i--) Lock.Lock();
     }
 }
예제 #16
0
 public override void LockInterruptibly()
 {
     Thread caller = Thread.CurrentThread;
     lock (this)
     {
         if (GetHold(caller)) return;
     }
     WaitNode n = new WaitNode();
     n.DoWait(this);
 }
예제 #17
0
 public override bool TryLock(TimeSpan timespan)
 {
     Thread caller = Thread.CurrentThread;
     lock (this)
     {
         if (_owner == null)
         {
             _owner = caller;
             _holds = 1;
             return true;
         }
         else if (caller == _owner)
         {
             ++_holds;
             return true;
         }
     }
     WaitNode n = new WaitNode();
     return n.DoTimedWait(this, timespan);
 }
예제 #18
0
 public void TakeOver(WaitNode node)
 {
     lock (this)
     {
         Debug.Assert(_holds == 1 && _owner == Thread.CurrentThread);
         _owner = node.Owner;
     }
 }
예제 #19
0
 public bool Recheck(WaitNode node)
 {
     lock (this)
     {
         Thread caller = Thread.CurrentThread;
         if (_owner == null)
         {
             _owner = caller;
             _holds = 1;
             return true;
         }
         else if (caller == _owner)
         {
             ++_holds;
             return true;
         }
         _wq.Enqueue(node);
         return false;
     }
 }
예제 #20
0
 public override void LockInterruptibly()
 {
     Thread caller = Thread.CurrentThread;
     lock (this)
     {
         if (_owner == null)
         {
             _owner = caller;
             _holds = 1;
             return;
         }
         else if (caller == _owner)
         {
             ++_holds;
             return;
         }
     }
     WaitNode n = new WaitNode();
     n.DoWait(this);
 }
예제 #21
0
 public bool Recheck(WaitNode node)
 {
     Thread caller = Thread.CurrentThread;
     lock (this)
     {
         if(GetHold(caller)) return true;
         _wq.Enqueue(node);
         return false;
     }
 }
예제 #22
0
 public override bool TryLock(TimeSpan timespan)
 {
     Thread caller = Thread.CurrentThread;
     lock (this)
     {
         if (GetHold(caller)) return true;
     }
     WaitNode n = new WaitNode();
     return n.DoTimedWait(this, timespan);
 }