public void DeleteTimer(IOThreadTimer timer) { int index = timer.index; Fx.Assert(index > 0, ""); Fx.Assert(index <= this.count, ""); IOThreadTimer[] tempTimers = this.timers; for (;;) { int parentIndex = index / 2; if (parentIndex >= 1) { IOThreadTimer parentTimer = tempTimers[parentIndex]; tempTimers[index] = parentTimer; parentTimer.index = index; } else { break; } index = parentIndex; } timer.index = 0; timer.dueTime = 0; tempTimers[1] = null; DeleteMinTimerCore(); }
public bool UpdateTimer(IOThreadTimer timer, long newDueTime) { int index = timer.index; IOThreadTimer[] tempTimers = this.timers; int tempCount = this.count; Fx.Assert(index > 0, ""); Fx.Assert(index <= tempCount, ""); int parentIndex = index / 2; if (parentIndex == 0 || tempTimers[parentIndex].dueTime <= newDueTime) { int leftChildIndex = index * 2; if (leftChildIndex > tempCount || tempTimers[leftChildIndex].dueTime >= newDueTime) { int rightChildIndex = leftChildIndex + 1; if (rightChildIndex > tempCount || tempTimers[rightChildIndex].dueTime >= newDueTime) { timer.dueTime = newDueTime; return(index == 1); } } } DeleteTimer(timer); InsertTimer(timer, newDueTime); return(true); }
public AsyncQueueWaiter(TimeSpan timeout, AsyncCallback callback, object state) : base(callback, state) { if (timeout != TimeSpan.MaxValue) { this.timer = new IOThreadTimer(timerCallback, this, false); this.timer.Set(timeout); } }
public void DeleteMinTimer() { IOThreadTimer minTimer = this.MinTimer; DeleteMinTimerCore(); minTimer.index = 0; minTimer.dueTime = 0; }
public void CancelTimer() { if (this.timer != null) { this.timer.Cancel(); this.timer = null; } }
public void SetTimer(Action <object> timerCallback, object timerState, TimeSpan timeout) { if (this.timer != null) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.MustCancelOldTimer)); } this.originalTimeout = timeout; this.timer = new IOThreadTimer(timerCallback, timerState, false); this.timer.Set(timeout); }
public AsyncQueueReader(InputQueue <T> inputQueue, TimeSpan timeout, AsyncCallback callback, object state) : base(callback, state) { if (inputQueue.AsyncCallbackGenerator != null) { base.VirtualCallback = new VirtualCallbackDelegate(inputQueue.AsyncCallbackGenerator()); } this.inputQueue = inputQueue; if (timeout != TimeSpan.MaxValue) { this.timer = new IOThreadTimer(timerCallback, this, false); this.timer.Set(timeout); } }
static void UpdateWaitableTimer(TimerGroup timerGroup) { WaitableTimer waitableTimer = timerGroup.WaitableTimer; IOThreadTimer minTimer = timerGroup.TimerQueue.MinTimer; long timeDiff = waitableTimer.DueTime - minTimer.dueTime; if (timeDiff < 0) { timeDiff = -timeDiff; } if (timeDiff > minTimer.maxSkew) { waitableTimer.Set(minTimer.dueTime); } }
public bool InsertTimer(IOThreadTimer timer, long dueTime) { Fx.Assert(timer.index == 0, "Timer should not have an index."); IOThreadTimer[] tempTimers = this.timers; int index = this.count + 1; if (index == tempTimers.Length) { tempTimers = new IOThreadTimer[tempTimers.Length * 2]; Array.Copy(this.timers, tempTimers, this.timers.Length); this.timers = tempTimers; } this.count = index; if (index > 1) { for (;;) { int parentIndex = index / 2; if (parentIndex == 0) { break; } IOThreadTimer parent = tempTimers[parentIndex]; if (parent.dueTime > dueTime) { tempTimers[index] = parent; parent.index = index; index = parentIndex; } else { break; } } } tempTimers[index] = timer; timer.index = index; timer.dueTime = dueTime; return(index == 1); }
static void ScheduleElapsedTimers(TimerGroup timerGroup, long now) { TimerQueue timerQueue = timerGroup.TimerQueue; while (timerQueue.Count > 0) { IOThreadTimer timer = timerQueue.MinTimer; long timeDiff = timer.dueTime - now; if (timeDiff <= timer.maxSkew) { timerQueue.DeleteMinTimer(); ActionItem.Schedule(timer.callback, timer.callbackState); } else { break; } } }
public bool Cancel(IOThreadTimer timer) { lock (ThisLock) { if (timer.index > 0) { TimerGroup timerGroup = timer.timerGroup; TimerQueue timerQueue = timerGroup.TimerQueue; timerQueue.DeleteTimer(timer); if (timerQueue.Count > 0) { UpdateWaitableTimer(timerGroup); } else { TimerGroup otherTimerGroup = GetOtherTimerGroup(timerGroup); if (otherTimerGroup.TimerQueue.Count == 0) { long now = Ticks.Now; long thisGroupRemainingTime = timerGroup.WaitableTimer.DueTime - now; long otherGroupRemainingTime = otherTimerGroup.WaitableTimer.DueTime - now; if (thisGroupRemainingTime > maxTimeToWaitForMoreTimers && otherGroupRemainingTime > maxTimeToWaitForMoreTimers) { timerGroup.WaitableTimer.Set(Ticks.Add(now, maxTimeToWaitForMoreTimers)); } } } return(true); } else { return(false); } } }
public void Set(IOThreadTimer timer, long dueTime) { long timeDiff = dueTime - timer.dueTime; if (timeDiff < 0) { timeDiff = -timeDiff; } if (timeDiff > timer.maxSkew) { lock (ThisLock) { TimerGroup timerGroup = timer.timerGroup; TimerQueue timerQueue = timerGroup.TimerQueue; if (timer.index > 0) { if (timerQueue.UpdateTimer(timer, dueTime)) { UpdateWaitableTimer(timerGroup); } } else { if (timerQueue.InsertTimer(timer, dueTime)) { UpdateWaitableTimer(timerGroup); if (timerQueue.Count == 1) { EnsureWaitScheduled(); } } } } } }
public bool UpdateTimer(IOThreadTimer timer, long newDueTime) { int index = timer.index; IOThreadTimer[] tempTimers = this.timers; int tempCount = this.count; Fx.Assert(index > 0, ""); Fx.Assert(index <= tempCount, ""); int parentIndex = index / 2; if (parentIndex == 0 || tempTimers[parentIndex].dueTime <= newDueTime) { int leftChildIndex = index * 2; if (leftChildIndex > tempCount || tempTimers[leftChildIndex].dueTime >= newDueTime) { int rightChildIndex = leftChildIndex + 1; if (rightChildIndex > tempCount || tempTimers[rightChildIndex].dueTime >= newDueTime) { timer.dueTime = newDueTime; return index == 1; } } } DeleteTimer(timer); InsertTimer(timer, newDueTime); return true; }
public bool InsertTimer(IOThreadTimer timer, long dueTime) { Fx.Assert(timer.index == 0, "Timer should not have an index."); IOThreadTimer[] tempTimers = this.timers; int index = this.count + 1; if (index == tempTimers.Length) { tempTimers = new IOThreadTimer[tempTimers.Length * 2]; Array.Copy(this.timers, tempTimers, this.timers.Length); this.timers = tempTimers; } this.count = index; if (index > 1) { for (;;) { int parentIndex = index / 2; if (parentIndex == 0) { break; } IOThreadTimer parent = tempTimers[parentIndex]; if (parent.dueTime > dueTime) { tempTimers[index] = parent; parent.index = index; index = parentIndex; } else { break; } } } tempTimers[index] = timer; timer.index = index; timer.dueTime = dueTime; return index == 1; }
public bool Cancel(IOThreadTimer timer) { lock (ThisLock) { if (timer.index > 0) { TimerGroup timerGroup = timer.timerGroup; TimerQueue timerQueue = timerGroup.TimerQueue; timerQueue.DeleteTimer(timer); if (timerQueue.Count > 0) { UpdateWaitableTimer(timerGroup); } else { TimerGroup otherTimerGroup = GetOtherTimerGroup(timerGroup); if (otherTimerGroup.TimerQueue.Count == 0) { long now = Ticks.Now; long thisGroupRemainingTime = timerGroup.WaitableTimer.DueTime - now; long otherGroupRemainingTime = otherTimerGroup.WaitableTimer.DueTime - now; if (thisGroupRemainingTime > maxTimeToWaitForMoreTimers && otherGroupRemainingTime > maxTimeToWaitForMoreTimers) { timerGroup.WaitableTimer.Set(Ticks.Add(now, maxTimeToWaitForMoreTimers)); } } } return true; } else { return false; } } }
void DeleteMinTimerCore() { int currentCount = this.count; if (currentCount == 1) { this.count = 0; this.timers[1] = null; } else { IOThreadTimer[] tempTimers = this.timers; IOThreadTimer lastTimer = tempTimers[currentCount]; this.count = --currentCount; int index = 1; for (;;) { int leftChildIndex = index * 2; if (leftChildIndex > currentCount) { break; } int childIndex; IOThreadTimer child; if (leftChildIndex < currentCount) { IOThreadTimer leftChild = tempTimers[leftChildIndex]; int rightChildIndex = leftChildIndex + 1; IOThreadTimer rightChild = tempTimers[rightChildIndex]; if (rightChild.dueTime < leftChild.dueTime) { child = rightChild; childIndex = rightChildIndex; } else { child = leftChild; childIndex = leftChildIndex; } } else { childIndex = leftChildIndex; child = tempTimers[childIndex]; } if (lastTimer.dueTime > child.dueTime) { tempTimers[index] = child; child.index = index; } else { break; } index = childIndex; if (leftChildIndex >= currentCount) { break; } } tempTimers[index] = lastTimer; lastTimer.index = index; tempTimers[currentCount + 1] = null; } }
public void SetTimer(Action<object> timerCallback, object timerState, TimeSpan timeout) { if (this.timer != null) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.MustCancelOldTimer)); } this.originalTimeout = timeout; this.timer = new IOThreadTimer(timerCallback, timerState, false); this.timer.Set(timeout); }