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

            node.Signal(mockQueuedSync);
            mockQueuedSync.AssertWasCalled(m => m.TakeOver(node));
        }
Exemplo n.º 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;
		}
Exemplo n.º 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;
		}
Exemplo n.º 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));
 }
Exemplo n.º 6
0
 public void Enqueue(WaitNode waitNode)
 {
     if (_tail == null)
         _head = _tail = waitNode;
     else
     {
         _tail.NextWaitNode = waitNode;
         _tail = waitNode;
     }
 }
Exemplo n.º 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;
        }
Exemplo n.º 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));
        }
Exemplo n.º 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);
 }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 13
0
 public void TakeOver(WaitNode node)
 {
 }
Exemplo n.º 14
0
 public bool Recheck(WaitNode node)
 {
     return false;
 }
Exemplo n.º 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();
     }
 }
Exemplo n.º 16
0
 public override void LockInterruptibly()
 {
     Thread caller = Thread.CurrentThread;
     lock (this)
     {
         if (GetHold(caller)) return;
     }
     WaitNode n = new WaitNode();
     n.DoWait(this);
 }
Exemplo n.º 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);
 }
Exemplo n.º 18
0
 public void TakeOver(WaitNode node)
 {
     lock (this)
     {
         Debug.Assert(_holds == 1 && _owner == Thread.CurrentThread);
         _owner = node.Owner;
     }
 }
Exemplo n.º 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;
     }
 }
Exemplo n.º 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);
 }
Exemplo n.º 21
0
 public bool Recheck(WaitNode node)
 {
     Thread caller = Thread.CurrentThread;
     lock (this)
     {
         if(GetHold(caller)) return true;
         _wq.Enqueue(node);
         return false;
     }
 }
Exemplo n.º 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);
 }