示例#1
0
 public virtual void DoWait(IQueuedSync sync)
 {
     lock (this)
     {
         if (!sync.Recheck(this))
         {
             try
             {
                 while (_waiting)
                 {
                     Monitor.Wait(this);
                 }
             }
             catch (ThreadInterruptedException ex)
             {
                 if (_waiting)
                 {
                     // no notification
                     _waiting = false; // invalidate for the signaller
                     throw ex.PreserveStackTrace();
                 }
                 // thread was interrupted after it was notified
                 Thread.CurrentThread.Interrupt();
                 return;
             }
         }
     }
 }
示例#2
0
 public virtual void DoWaitUninterruptibly(IQueuedSync sync)
 {
     lock (this)
     {
         if (!sync.Recheck(this))
         {
             var wasInterrupted = false;
             while (_waiting)
             {
                 try
                 {
                     Monitor.Wait(this);
                 }
                 catch (ThreadInterruptedException)
                 {
                     wasInterrupted = true;
                     // no need to notify; if we were signalled, we
                     // must be not waiting, and we'll act like signalled
                 }
             }
             if (wasInterrupted)
             {
                 Thread.CurrentThread.Interrupt();
             }
         }
     }
 }
示例#3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="sync"></param>
 public virtual void DoWaitUninterruptibly(IQueuedSync sync)
 {
     lock (this)
     {
         if (!sync.Recheck(this))
         {
             bool wasInterrupted = false;
             while (_waiting)
             {
                 try
                 {
                     Monitor.Wait(this);
                 }
                 catch (ThreadInterruptedException)
                 {
                     wasInterrupted = true;
                 }
             }
             if (wasInterrupted)
             {
                 Thread.CurrentThread.Interrupt();
             }
         }
     }
 }
        public void SignalOnWaitingThread()
        {
            WaitNode    node           = new WaitNode();
            IQueuedSync mockQueuedSync = MockRepository.GenerateMock <IQueuedSync>();

            node.Signal(mockQueuedSync);
            mockQueuedSync.AssertWasCalled(m => m.TakeOver(node));
        }
示例#5
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="sync"></param>
 /// <param name="duration"></param>
 /// <returns></returns>
 public virtual bool DoTimedWait(IQueuedSync sync, TimeSpan duration)
 {
     lock (this)
     {
         if (sync.Recheck(this) || !_waiting)
         {
             return(true);
         }
         else if (duration.Ticks <= 0)
         {
             _waiting = false;
             return(false);
         }
         else
         {
             DateTime deadline = DateTime.Now.Add(duration);
             try
             {
                 for (;;)
                 {
                     Monitor.Wait(this, duration);
                     if (!_waiting)
                     {
                         return(true);
                     }
                     else
                     {
                         duration = deadline.Subtract(DateTime.Now);
                         if (duration.Ticks <= 0)
                         {
                             _waiting = false;
                             return(false);
                         }
                     }
                 }
             }
             catch (ThreadInterruptedException ex)
             {
                 if (_waiting)
                 {
                     _waiting = false;                             // invalidate for the signaller
                     throw ex;
                 }
                 else
                 {
                     Thread.CurrentThread.Interrupt();
                     return(true);
                 }
             }
         }
     }
 }
示例#6
0
 public virtual bool Signal(IQueuedSync sync)
 {
     lock (this)
     {
         var signalled = _waiting;
         if (signalled)
         {
             _waiting = false;
             Monitor.Pulse(this);
             sync.TakeOver(this);
         }
         return(signalled);
     }
 }
示例#7
0
		public virtual bool Signal(IQueuedSync sync)
		{
			lock (this)
			{
				bool signalled = _waiting;
				if (signalled)
				{
					_waiting = false;
					Monitor.Pulse(this);
					sync.TakeOver(this);
				}
				return signalled;
			}
		}
示例#8
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="sync"></param>
 /// <param name="duration"></param>
 /// <returns></returns>
 public virtual bool DoTimedWait( IQueuedSync sync, TimeSpan duration)
 {
     lock (this)
     {
         if (sync.Recheck(this) || !_waiting)
         {
             return true;
         }
         else if (duration.Ticks <= 0)
         {
             _waiting = false;
             return false;
         }
         else
         {
             DateTime deadline = DateTime.Now.Add(duration);
             try
             {
                 for (;; )
                 {
                     Monitor.Wait(this, duration);
                     if (!_waiting)
                         return true;
                     else
                     {
                         duration = deadline.Subtract(DateTime.Now);
                         if (duration.Ticks <= 0)
                         {
                             _waiting = false;
                             return false;
                         }
                     }
                 }
             }
             catch (ThreadInterruptedException ex)
             {
                 if (_waiting)
                 {
                     _waiting = false; // invalidate for the signaller
                     throw ex;
                 }
                 else
                 {
                     Thread.CurrentThread.Interrupt();
                     return true;
                 }
             }
         }
     }
 }
示例#9
0
 public virtual bool DoTimedWait(IQueuedSync sync, TimeSpan duration)
 {
     lock (this)
     {
         if (sync.Recheck(this) || !_waiting)
         {
             return(true);
         }
         if (duration.Ticks <= 0)
         {
             _waiting = false;
             return(false);
         }
         var deadline = DateTime.UtcNow.Add(duration);
         try
         {
             for (;;)
             {
                 Monitor.Wait(this, duration);
                 if (!_waiting) // definitely signalled
                 {
                     return(true);
                 }
                 duration = deadline.Subtract(DateTime.UtcNow);
                 if (duration.Ticks <= 0) // time out
                 {
                     _waiting = false;
                     return(false);
                 }
             }
         }
         catch (ThreadInterruptedException ex)
         {
             if (_waiting)         // no notification
             {
                 _waiting = false; // invalidate for the signaller
                 throw ex.PreserveStackTrace();
             }
             // thread was interrupted after it was notified
             Thread.CurrentThread.Interrupt();
             return(true);
         }
     }
 }
示例#10
0
        public virtual bool DoTimedWait( IQueuedSync sync, TimeSpan duration)
		{
			lock (this)
			{
			    if (sync.Recheck(this) || !_waiting)
				{
					return true;
				}
			    if (duration.Ticks <= 0)
			    {
			        _waiting = false;
			        return false;
			    }
			    DateTime deadline = DateTime.UtcNow.Add(duration);
			    try
			    {
			        for (;; )
			        {
			            Monitor.Wait(this, duration);
			            if (!_waiting) // definitely signalled
			                return true;
			            duration = deadline.Subtract(DateTime.UtcNow);
			            if (duration.Ticks <= 0) // time out
			            {
			                _waiting = false;
			                return false;
			            }
			        }
			    }
			    catch (ThreadInterruptedException ex)
			    {
			        if (_waiting) // no notification
			        {
			            _waiting = false; // invalidate for the signaller
			            throw SystemExtensions.PreserveStackTrace(ex);
			        }
                    // thread was interrupted after it was notified
			        Thread.CurrentThread.Interrupt();
			        return true;
			    }
			}
		}
示例#11
0
		public virtual void DoWaitUninterruptibly(IQueuedSync sync)
		{
			lock (this)
			{
				if (!sync.Recheck(this))
				{
					bool wasInterrupted = false;
					while (_waiting)
					{
						try
						{
							Monitor.Wait(this);
						}
						catch (ThreadInterruptedException)
						{
							wasInterrupted = true;
                            // no need to notify; if we were signalled, we
                            // must be not waiting, and we'll act like signalled
                        }
					}
					if (wasInterrupted)
					{
						Thread.CurrentThread.Interrupt();
					}
				}
			}
		}
示例#12
0
        public virtual void DoWait(IQueuedSync sync)
		{
			lock (this)
			{
				if (!sync.Recheck(this))
				{
					try
					{
						while (_waiting) Monitor.Wait(this);
					}
					catch (ThreadInterruptedException ex)
					{
						if (_waiting)
						{
							// no notification
							_waiting = false; // invalidate for the signaller
							throw SystemExtensions.PreserveStackTrace(ex);
						}
					    // thread was interrupted after it was notified
					    Thread.CurrentThread.Interrupt();
					    return;
					}
				}
			}
		}
示例#13
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sync"></param>
        public virtual void DoWaitUninterruptibly(IQueuedSync sync)
        {
            lock (this)
            {
                if (!sync.Recheck(this))
                {
                    bool wasInterrupted = false;
                    while (_waiting)
                    {
                        try
                        {
                            Monitor.Wait(this);
                        }
                        catch (ThreadInterruptedException)
                        {

                            wasInterrupted = true;
                        }
                    }
                    if (wasInterrupted)
                    {
                        Thread.CurrentThread.Interrupt();
                    }
                }
            }
        }