void RunAllTasks() { this.FetchFromScheduledTaskQueue(); PreciseTimeSpan deadline = PreciseTimeSpan.Deadline(this.preciseBreakoutInterval); long runTasks = 0; PreciseTimeSpan executionTime = PreciseTimeSpan.Zero; while (true) { IRunnable task = this.PollTask(); if (task == null || this.IsShuttingDown) { break; } try { task.Run(); } catch (Exception ex) { Logger.Warn("A task raised an exception.", ex); } runTasks++; // Check timeout every 64 tasks because nanoTime() is relatively expensive. // XXX: Hard-coded value - will make it configurable if it is really a problem. if ((runTasks & 0x3F) == 0) { executionTime = PreciseTimeSpan.FromStart; if (executionTime >= deadline) { break; } } } if (this.IsShuttingDown) { this.ShutdownLoop(); } else { if (executionTime != PreciseTimeSpan.Zero) { // Start the timer once to check the task queue later this.timerHandle.Start(this.preciseTimerInterval.Ticks, 1); } } }
public void Schedule(Action <object> action, object state, TimeSpan delay, CancellationToken cancellationToken) { var queueNode = new ScheduledTaskQueueNode(action, state, PreciseTimeSpan.Deadline(delay), cancellationToken); if (this.InEventLoop) { this.scheduledTaskQueue.Enqueue(queueNode); } else { this.Execute(e => ((SingleThreadEventExecutor)e).scheduledTaskQueue.Enqueue(queueNode), this); // it is an allocation but it should not happen often (cross-thread scheduling) } }
public override Task ScheduleAsync(Action <object> action, object state, TimeSpan delay, CancellationToken cancellationToken) { var scheduledTask = new StateActionScheduledTask(action, state, PreciseTimeSpan.Deadline(delay), cancellationToken); if (this.InEventLoop) { this.ScheduledTaskQueue.Enqueue(scheduledTask); } else { this.Execute(AddScheduledTaskAction, this, scheduledTask); } return(scheduledTask.Completion); }
public override Task ScheduleAsync(Action action, TimeSpan delay, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return(TaskEx.Cancelled); } if (!cancellationToken.CanBeCanceled) { return(this.Schedule(action, delay).Completion); } return(this.Schedule(new ActionScheduledAsyncTask(this, action, PreciseTimeSpan.Deadline(delay), cancellationToken)).Completion); }
bool RunAllTasks(PreciseTimeSpan timeout) { this.FetchFromScheduledTaskQueue(); IRunnable task = this.PollTask(); if (task == null) { return(false); } PreciseTimeSpan deadline = PreciseTimeSpan.Deadline(timeout); long runTasks = 0; PreciseTimeSpan executionTime; while (true) { try { task.Run(); } catch (Exception ex) { Logger.Warn("A task raised an exception.", ex); } runTasks++; // Check timeout every 64 tasks because nanoTime() is relatively expensive. // XXX: Hard-coded value - will make it configurable if it is really a problem. if ((runTasks & 0x3F) == 0) { executionTime = PreciseTimeSpan.FromStart; if (executionTime >= deadline) { break; } } task = this.PollTask(); if (task == null) { executionTime = PreciseTimeSpan.FromStart; break; } } this.lastExecutionTime = executionTime; return(true); }
public static async Task EventuallyAsync(Func <Task <bool> > testFunc, TimeSpan interval, TimeSpan timeout) { PreciseTimeSpan deadline = PreciseTimeSpan.Deadline(timeout); while (true) { if (await testFunc()) { return; } if (PreciseTimeSpan.FromStart - deadline > PreciseTimeSpan.Zero) { Assert.True(false, "Did not reach expected state in time."); } await Task.Delay(interval); } }
private bool RunAllTasks(PreciseTimeSpan timeout) { _ = FetchFromScheduledTaskQueue(); IRunnable task = PollTask(); if (task is null) { AfterRunningAllTasks(); return(false); } PreciseTimeSpan deadline = PreciseTimeSpan.Deadline(timeout); long runTasks = 0; PreciseTimeSpan executionTime; while (true) { SafeExecute(task); runTasks++; // Check timeout every 64 tasks because nanoTime() is relatively expensive. // XXX: Hard-coded value - will make it configurable if it is really a problem. if (0ul >= (ulong)(runTasks & 0x3F)) { executionTime = PreciseTimeSpan.FromStart; if (executionTime >= deadline) { break; } } task = PollTask(); if (task is null) { executionTime = PreciseTimeSpan.FromStart; break; } } AfterRunningAllTasks(); _lastExecutionTime = executionTime; return(true); }
public override bool WaitTermination(TimeSpan timeout) { PreciseTimeSpan deadline = PreciseTimeSpan.Deadline(timeout); for (int i = 0; i < _eventLoops.Length; i++) { var executor = _eventLoops[i]; for (; ;) { PreciseTimeSpan timeLeft = deadline - PreciseTimeSpan.FromStart; if (timeLeft <= PreciseTimeSpan.Zero) { goto LoopEnd; } if (executor.WaitTermination(timeLeft.ToTimeSpan())) { break; } } } LoopEnd: return(IsTerminated); }
public override Task ScheduleAsync(Action <object, object> action, object context, object state, TimeSpan delay, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return(TaskEx.Cancelled); } if (!cancellationToken.CanBeCanceled) { return(this.Schedule(action, context, state, delay).Completion); } return(this.Schedule(new StateActionWithContextScheduledAsyncTask(this, action, context, state, PreciseTimeSpan.Deadline(delay), cancellationToken)).Completion); }
public override IScheduledTask Schedule(Action <object, object> action, object context, object state, TimeSpan delay) { Contract.Requires(action != null); return(this.Schedule(new StateActionWithContextScheduledTask(this, action, context, state, PreciseTimeSpan.Deadline(delay)))); }
public override IScheduledTask Schedule(Action action, TimeSpan delay) { Contract.Requires(action != null); return(this.Schedule(new ActionScheduledTask(this, action, PreciseTimeSpan.Deadline(delay)))); }
public override IScheduledTask Schedule(Action action, TimeSpan delay) { return(this.Schedule(new ActionScheduledTask(this, action, PreciseTimeSpan.Deadline(delay)))); }
public override IScheduledTask Schedule(Action <object> action, object state, TimeSpan delay) { return(this.Schedule(new StateActionScheduledTask(this, action, state, PreciseTimeSpan.Deadline(delay)))); }