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; } } } }
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(); } } } }
/// <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(); } } } }
/// <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); } } } } }
/// <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; } } } } }
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); } } }
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; } } }
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(); } } } }
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; } } } }