Exemple #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;
             }
         }
     }
 }
Exemple #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();
             }
         }
     }
 }
Exemple #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();
             }
         }
     }
 }
Exemple #4
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);
                 }
             }
         }
     }
 }
Exemple #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;
                 }
             }
         }
     }
 }
Exemple #6
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);
         }
     }
 }
Exemple #7
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;
			    }
			}
		}
Exemple #8
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();
					}
				}
			}
		}
Exemple #9
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;
					}
				}
			}
		}
Exemple #10
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();
                    }
                }
            }
        }