@@ -27,7 +27,6 @@ public class SingleThreadEventExecutor : AbstractScheduledEventExecutor
2727 readonly MpscLinkedQueue < IRunnable > taskQueue = new MpscLinkedQueue < IRunnable > ( ) ;
2828 Thread thread ;
2929 volatile int executionState = ST_NOT_STARTED ;
30- readonly TimeSpan breakoutInterval ;
3130 readonly PreciseTimeSpan preciseBreakoutInterval ;
3231 PreciseTimeSpan lastExecutionTime ;
3332 readonly ManualResetEventSlim emptyEvent = new ManualResetEventSlim ( ) ;
@@ -41,7 +40,6 @@ public class SingleThreadEventExecutor : AbstractScheduledEventExecutor
4140 public SingleThreadEventExecutor ( string threadName , TimeSpan breakoutInterval )
4241 {
4342 this . terminationCompletionSource = new TaskCompletionSource ( ) ;
44- this . breakoutInterval = breakoutInterval ;
4543 this . preciseBreakoutInterval = PreciseTimeSpan . FromTimeSpan ( breakoutInterval ) ;
4644 this . scheduler = new ExecutorTaskScheduler ( this ) ;
4745 this . thread = new Thread ( this . Loop )
@@ -400,10 +398,23 @@ IRunnable PollTask()
400398 if ( task == null )
401399 {
402400 this . emptyEvent . Reset ( ) ;
403- if ( ( task = this . taskQueue . Dequeue ( ) ) == null // revisit queue as producer might have put a task in meanwhile
404- && this . emptyEvent . Wait ( this . breakoutInterval ) )
401+ if ( ( task = this . taskQueue . Dequeue ( ) ) == null ) // revisit queue as producer might have put a task in meanwhile
405402 {
406- task = this . taskQueue . Dequeue ( ) ;
403+ IScheduledRunnable nextScheduledTask = this . ScheduledTaskQueue . Peek ( ) ;
404+ if ( nextScheduledTask != null )
405+ {
406+ TimeSpan wakeUpTimeout = ( nextScheduledTask . Deadline - PreciseTimeSpan . FromStart ) . ToTimeSpan ( ) ;
407+ if ( this . emptyEvent . Wait ( wakeUpTimeout ) )
408+ {
409+ // woken up before the next scheduled task was due
410+ task = this . taskQueue . Dequeue ( ) ;
411+ }
412+ }
413+ else
414+ {
415+ this . emptyEvent . Wait ( ) ;
416+ task = this . taskQueue . Dequeue ( ) ;
417+ }
407418 }
408419 }
409420
0 commit comments