From 7b4507f068d47f5672ffd6e9cb4b51613dba2425 Mon Sep 17 00:00:00 2001 From: SilverFox Date: Fri, 8 Nov 2019 11:05:01 +0800 Subject: [PATCH] Ignore WAKEUP_TASK in SingleThreadEventExecutor.PollTask --- .../Concurrency/SingleThreadEventExecutor.cs | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/DotNetty.Common/Concurrency/SingleThreadEventExecutor.cs b/src/DotNetty.Common/Concurrency/SingleThreadEventExecutor.cs index 225871962..78ff8d3dc 100644 --- a/src/DotNetty.Common/Concurrency/SingleThreadEventExecutor.cs +++ b/src/DotNetty.Common/Concurrency/SingleThreadEventExecutor.cs @@ -489,31 +489,36 @@ IRunnable PollTask() { Contract.Assert(this.InEventLoop); - IRunnable task; - if (!this.taskQueue.TryDequeue(out task)) + while (true) { - this.emptyEvent.Reset(); - if (!this.taskQueue.TryDequeue(out task) && !this.IsShuttingDown) // revisit queue as producer might have put a task in meanwhile + IRunnable task; + if (!this.taskQueue.TryDequeue(out task)) { - IScheduledRunnable nextScheduledTask = this.ScheduledTaskQueue.Peek(); - if (nextScheduledTask != null) + this.emptyEvent.Reset(); + if (!this.taskQueue.TryDequeue(out task) && !this.IsShuttingDown) // revisit queue as producer might have put a task in meanwhile { - PreciseTimeSpan wakeupTimeout = nextScheduledTask.Deadline - PreciseTimeSpan.FromStart; - if (wakeupTimeout.Ticks > 0) + IScheduledRunnable nextScheduledTask = this.ScheduledTaskQueue.Peek(); + if (nextScheduledTask != null) { - double timeout = wakeupTimeout.ToTimeSpan().TotalMilliseconds; - this.emptyEvent.Wait((int)Math.Min(timeout, int.MaxValue - 1)); + PreciseTimeSpan wakeupTimeout = nextScheduledTask.Deadline - PreciseTimeSpan.FromStart; + if (wakeupTimeout.Ticks > 0) + { + double timeout = wakeupTimeout.ToTimeSpan().TotalMilliseconds; + this.emptyEvent.Wait((int)Math.Min(timeout, int.MaxValue - 1)); + } + } + else + { + this.emptyEvent.Wait(); + this.taskQueue.TryDequeue(out task); } } - else - { - this.emptyEvent.Wait(); - this.taskQueue.TryDequeue(out task); - } + } + if (task != WAKEUP_TASK) + { + return task; } } - - return task; } sealed class NoOpRunnable : IRunnable