bool FetchFromScheduledTaskQueue() { PreciseTimeSpan nanoTime = PreciseTimeSpan.FromStart; IScheduledRunnable scheduledTask = this.PollScheduledTask(nanoTime); while (scheduledTask != null) { if (!this.taskQueue.TryEnqueue(scheduledTask)) { // No space left in the task queue add it back to the scheduledTaskQueue so we pick it up again. this.ScheduledTaskQueue.Enqueue(scheduledTask); return(false); } scheduledTask = this.PollScheduledTask(nanoTime); } return(true); }
private void Schedule(IScheduledRunnable task) { StartRunDelayTask(); this.Execute((state) => { lock (ScheduledTaskQueue) { if (this.ScheduledTaskQueue.Count > MaxTaskCount) { throw new Exception($"延迟任务队列超过{MaxTaskCount}条"); } this.ScheduledTaskQueue.Enqueue(task); Monitor.Pulse(ScheduledTaskQueue); } return(Task.CompletedTask); }, null); }
protected IScheduledRunnable PollScheduledTask(PreciseTimeSpan nanoTime) { Contract.Assert(this.InEventLoop); IScheduledRunnable scheduledTask = this.ScheduledTaskQueue.Peek(); if (scheduledTask == null) { return(null); } if (scheduledTask.Deadline <= nanoTime) { this.ScheduledTaskQueue.Dequeue(); return(scheduledTask); } return(null); }
internal void ScheduleFromEventLoop(IScheduledRunnable task) { // nextTaskId a long and so there is no chance it will overflow back to 0 var nextTaskId = ++_nextTaskId; if (nextTaskId == long.MaxValue) { _nextTaskId = 0; } var isBacklogEmpty = !HasTasks && _scheduledTaskQueue.IsEmpty; _ = _scheduledTaskQueue.TryEnqueue(task.SetId(nextTaskId)); if (isBacklogEmpty) { // 在 Libuv.LoopExecutor 中,当任务执行完毕,清空任务队列后,后续如果只有 ScheduledTask 入队的情况下, // 并不会激发线程进行任务处理,需唤醒 EnusreWakingUp(true); } }
private bool FetchFromScheduledTaskQueue() { if (ScheduledTaskQueue.IsEmpty) { return(true); } PreciseTimeSpan nanoTime = PreciseTimeSpan.FromStart; IScheduledRunnable scheduledTask = PollScheduledTask(nanoTime); while (scheduledTask is object) { if (!_taskQueue.TryEnqueue(scheduledTask)) { // No space left in the task queue add it back to the scheduledTaskQueue so we pick it up again. _ = ScheduledTaskQueue.TryEnqueue(scheduledTask); return(false); } scheduledTask = PollScheduledTask(nanoTime); } return(true); }
private void RunDelayTask() { lock (ScheduledTaskQueue) { IScheduledRunnable nextScheduledTask = this.ScheduledTaskQueue.Peek(); if (nextScheduledTask != null) { var tempDelay = nextScheduledTask.TimeStamp - DateUtils.GetTimeStamp(); if (tempDelay > 0) { Monitor.Wait(ScheduledTaskQueue, (int)tempDelay); } else { this.ScheduledTaskQueue.Dequeue(); Execute(nextScheduledTask); } } else { Monitor.Wait(ScheduledTaskQueue); } } }
protected bool HasScheduledTasks() { IScheduledRunnable scheduledTask = this.ScheduledTaskQueue.Peek(); return(scheduledTask != null && scheduledTask.Deadline <= PreciseTimeSpan.FromStart); }
protected PreciseTimeSpan NextScheduledTaskNanos() { IScheduledRunnable nextScheduledRunnable = this.PeekScheduledTask(); return(nextScheduledRunnable?.Deadline ?? PreciseTimeSpan.MinusOne); }
int IComparable <IScheduledRunnable> .CompareTo(IScheduledRunnable other) { Contract.Requires(other != null); return(this.Deadline.CompareTo(other.Deadline)); }
public int CompareTo(IScheduledRunnable other) { return((int)(this.TimeStamp - other.TimeStamp)); }
public int CompareTo(IScheduledRunnable other) { return(Deadline.CompareTo(other.Deadline)); }
internal void ScheduleFromEventLoop(IScheduledRunnable task) { // nextTaskId a long and so there is no chance it will overflow back to 0 _ = ScheduledTaskQueue.TryEnqueue(task.SetId(++_nextTaskId)); }
public int CompareTo(IScheduledRunnable other) { return Deadline.CompareTo(other.Deadline); }
protected bool TryPeekScheduledTask(out IScheduledRunnable task) { return(_scheduledTaskQueue.TryPeek(out task)); }
int IComparable <IScheduledRunnable> .CompareTo(IScheduledRunnable other) { Require.NotNull(other); return(this.Deadline.CompareTo(other.Deadline)); }