public static void Sleep(OsState os, TimeSpan time) { if (time == new TimeSpan(0)) { return; } TickManager tickManager = os.GetTickManager(); // Attempt to avoid overflow by doing the addition unsigned ulong currentTick = (ulong)tickManager.GetTick().GetInt64Value(); ulong timeoutTick = (ulong)tickManager.ConvertToTick(time).GetInt64Value(); ulong absoluteEndTick = currentTick + timeoutTick + 1; var endTick = new Tick((long)Math.Min(long.MaxValue, absoluteEndTick)); Tick curTick = tickManager.GetTick(); // Sleep in a loop until the requested time has past. while (curTick < endTick) { Tick remaining = endTick - curTick; int sleepTimeMs = (int)ConvertToImplTime(os, remaining).GetMilliSeconds(); System.Threading.Thread.Sleep(sleepTimeMs); curTick = tickManager.GetTick(); } }
public static TimeSpan ConvertToImplTime(OsState os, Tick tick) { TickManager tickManager = os.GetTickManager(); TimeSpan ts = tickManager.ConvertToTimespan(tick); // .NET allows sleeping up to int.MaxValue milliseconds at a time. long timeMs = Math.Min(int.MaxValue, ts.GetMilliSeconds()); return(TimeSpan.FromMilliSeconds(timeMs)); }
public TimeoutHelper(OsState os, TimeSpan timeout) { _os = os; if (timeout == new TimeSpan(0)) { // If timeout is zero, don't do relative tick calculations. _endTick = new Tick(0); } else { TickManager tickManager = os.GetTickManager(); // Attempt to avoid overflow by doing the addition unsigned ulong currentTick = (ulong)tickManager.GetTick().GetInt64Value(); ulong timeoutTick = (ulong)tickManager.ConvertToTick(timeout).GetInt64Value(); ulong absoluteEndTick = currentTick + timeoutTick + 1; _endTick = new Tick((long)Math.Min(long.MaxValue, absoluteEndTick)); } }
// Todo: Use configuration object if/when more options are added public OsResourceManager(ITickGenerator tickGenerator) { TickManager = new TickManager(tickGenerator); }