/// <inheritdoc cref="IEventExecutor"/> public override Task ShutdownGracefullyAsync(TimeSpan quietPeriod, TimeSpan timeout) { if (this.IsShuttingDown) { return(this.TerminationCompletion); } bool InLoop = this.InLoop; bool wakeup; int oldState; while (true) { if (this.IsShuttingDown) { return(this.TerminationCompletion); } int newState; wakeup = true; oldState = this.executionState; if (InLoop) { newState = ST_SHUTTING_DOWN; } else { switch (oldState) { case ST_NOT_STARTED: case ST_STARTED: newState = ST_SHUTTING_DOWN; break; default: newState = oldState; wakeup = false; break; } } if (Interlocked.CompareExchange(ref this.executionState, newState, oldState) == oldState) { break; } } this.gracefulShutdownQuietPeriod = PreciseTimeSpan.FromTimeSpan(quietPeriod); this.gracefulShutdownTimeout = PreciseTimeSpan.FromTimeSpan(timeout); // todo: revisit //if (oldState == ST_NOT_STARTED) //{ // scheduleExecution(); //} if (wakeup) { this.WakeUp(InLoop); } return(this.TerminationCompletion); }
protected IndependentThreadExecutor(IExecutorGroup parent, string threadName, TimeSpan breakoutInterval, IConcurrentQueue <IRunnable> taskQueue) : base(parent) { this.terminationCompletionSource = new TaskCompletionSource(); this.taskQueue = taskQueue; this.preciseBreakoutInterval = PreciseTimeSpan.FromTimeSpan(breakoutInterval); this.scheduler = new ExecutorTaskScheduler(this); this.thread = new Thread(this.Loop); if (string.IsNullOrEmpty(threadName)) { this.thread.Name = DefaultWorkerThreadName; } else { this.thread.Name = threadName; } this.thread.Start(); }