Ejemplo n.º 1
0
        private IRunnable PollTask()
        {
            Contract.Assert(InEventLoop);
            IRunnable task;

            if (!_taskQueue.TryDequeue(out task))
            {
                _emptyQueueEvent.Reset();
                if (!_taskQueue.TryDequeue(out task) && !IsShuttingDown)
                // revisit queue as producer might have put a task in meanwhile
                {
                    var nextScheduledTask = ScheduledTaskQueue.Peek();
                    if (nextScheduledTask != null)
                    {
                        var wakeupTimeout = new TimeSpan(nextScheduledTask.Deadline.When - MonotonicClock.GetTicks());
                        if (wakeupTimeout.Ticks > 0)
                        {
                            if (_emptyQueueEvent.Wait(wakeupTimeout))
                            {
                                // woken up before the next scheduled task was due
                                _taskQueue.TryDequeue(out task);
                            }
                        }
                    }
                    else
                    {
                        _emptyQueueEvent.Wait(); // wait until work is put into the queue
                        _taskQueue.TryDequeue(out task);
                    }
                }
            }
            return(task);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        protected bool FetchFromScheduledTaskQueue()
        {
            if (ScheduledTaskQueue.IsEmpty)
            {
                return(true);
            }

            var nanoTime      = PreciseTime.NanoTime();
            var scheduledTask = PollScheduledTask(nanoTime);
            var taskQueue     = _taskQueue;

            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);
        }