public void TryEnter(int milliSeconds, ref bool lockTaken) { //Thread.BeginCriticalRegion(); Watch sw = Watch.StartNew(); while (sw.ElapsedMilliseconds < milliSeconds) { TryEnter(ref lockTaken); } sw.Stop(); }
public static bool SpinUntil(Func <bool> condition, int millisecondsTimeout) { SpinWait sw = new SpinWait(); Watch watch = Watch.StartNew(); while (!condition()) { if (watch.ElapsedMilliseconds > millisecondsTimeout) { return(false); } sw.SpinOnce(); } return(true); }
public static bool SpinUntil(Func <bool> predicate, int milliseconds) { SpinWait sw = new SpinWait(); Watch watch = Watch.StartNew(); while (!predicate()) { if (watch.ElapsedMilliseconds > milliseconds) { return(false); } sw.SpinOnce(); } return(true); }
public bool Wait(int timeoutMilli, CancellationToken token) { if (timeoutMilli == -1) { Wait(); return(true); } Watch sw = Watch.StartNew(); long timeout = (long)timeoutMilli; bool result = Wait(() => sw.ElapsedMilliseconds > timeout || token.IsCancellationRequested); sw.Stop(); return(result); }
public bool Wait(int timeoutMilli) { if (timeoutMilli == -1) { Wait(); return(true); } Watch sw = Watch.StartNew(); long timeout = (long)timeoutMilli; bool result = Wait(() => sw.ElapsedMilliseconds > timeout); sw.Stop(); return(result); }
public bool Wait(int millisecondsTimeout) { if (millisecondsTimeout < -1) { throw new ArgumentOutOfRangeException("millisecondsTimeout", "millisecondsTimeout is a negative number other than -1"); } if (millisecondsTimeout == -1) { Wait(); return(true); } Watch s = Watch.StartNew(); // First spin for (int i = 0; i < spinCount && state == isNotSet; i++) { if (s.ElapsedMilliseconds >= millisecondsTimeout) { return(false); } sw.SpinOnce(); } // Then, fallback to classic Sleep's yielding while (state == isNotSet) { if (s.ElapsedMilliseconds >= millisecondsTimeout) { return(false); } sw.SpinOnce(); } return(true); }
public bool Wait(int millisecondsTimeout) { CheckState(); if (millisecondsTimeout < -1) { throw new ArgumentOutOfRangeException("millisecondsTimeout", "millisecondsTimeout is a negative number other than -1"); } if (millisecondsTimeout == -1) { Wait(); return(true); } do { int result = Interlocked.Decrement(ref currCount); if (result >= 0) { break; } // We revert back the operation result = Interlocked.Increment(ref currCount); Watch sw = Watch.StartNew(); while (Thread.VolatileRead(ref currCount) <= 0) { if (sw.ElapsedMilliseconds > millisecondsTimeout) { sw.Stop(); return(false); } wait.SpinOnce(); } } while (true); return(true); }
public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken) { CheckState(); if (millisecondsTimeout < -1) { throw new ArgumentOutOfRangeException("millisecondsTimeout", "millisecondsTimeout is a negative number other than -1"); } Watch sw = Watch.StartNew(); Func <bool> stopCondition = () => millisecondsTimeout >= 0 && sw.ElapsedMilliseconds > millisecondsTimeout; do { bool shouldWait; int result; do { cancellationToken.ThrowIfCancellationRequested(); if (stopCondition()) { return(false); } shouldWait = true; result = currCount; if (result > 0) { shouldWait = false; } else { break; } } while (Interlocked.CompareExchange(ref currCount, result - 1, result) != result); if (!shouldWait) { if (result == 1) { handle.Reset(); } break; } SpinWait wait = new SpinWait(); while (Thread.VolatileRead(ref currCount) <= 0) { cancellationToken.ThrowIfCancellationRequested(); if (stopCondition()) { return(false); } if (wait.Count > spinCount) { handle.WaitOne(Math.Min(Math.Max(millisecondsTimeout - (int)sw.ElapsedMilliseconds, 1), deepSleepTime)); } else { wait.SpinOnce(); } } } while (true); return(true); }