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++;

                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);
        }
        bool FetchFromScheduledTaskQueue()
        {
            PreciseTimeSpan    nanoTime      = PreciseTimeSpan.FromStart;
            IScheduledRunnable scheduledTask = this.PollScheduledTask(nanoTime);

            while (scheduledTask != null)
            {
                if (!this.taskQueue.TryEnqueue(scheduledTask))
                {
                    // No space left in the task queue add it back to the scheduledTaskQueue so we pick it up again.
                    this.ScheduledTaskQueue.Enqueue(scheduledTask);
                    return(false);
                }
                scheduledTask = this.PollScheduledTask(nanoTime);
            }
            return(true);
        }
 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();
 }
Beispiel #4
0
 protected ScheduledAsyncTask(Executor executor, PreciseTimeSpan deadline, TaskCompletionSource promise, CancellationToken cancellationToken)
     : base(executor, deadline, promise)
 {
     this.cancellationToken             = cancellationToken;
     this.cancellationTokenRegistration = cancellationToken.Register(s => ((ScheduledAsyncTask)s).Cancel(), this);
 }
Beispiel #5
0
 public RunnableScheduledTask(Executor executor, IRunnable action, PreciseTimeSpan deadline)
     : base(executor, deadline, new TaskCompletionSource())
 {
     this.action = action;
 }
Beispiel #6
0
 public ActionScheduledAsyncTask(Executor executor, Action action, PreciseTimeSpan deadline, CancellationToken cancellationToken)
     : base(executor, deadline, new TaskCompletionSource(), cancellationToken)
 {
     this.action = action;
 }
 public StateActionScheduledAsyncTask(Executor executor, Action <object> action, object state, PreciseTimeSpan deadline,
                                      CancellationToken cancellationToken)
     : base(executor, deadline, new TaskCompletionSource(state), cancellationToken)
 {
     this.action = action;
 }
 public StateActionScheduledTask(Executor executor, Action <object> action, object state, PreciseTimeSpan deadline)
     : base(executor, deadline, new TaskCompletionSource(state))
 {
     this.action = action;
 }
        /// <inheritdoc cref="IEventExecutor"/>
        public override Task ShutdownGracefullyAsync(TimeSpan quietPeriod, TimeSpan timeout)
        {
            Contract.Requires(quietPeriod >= TimeSpan.Zero);
            Contract.Requires(timeout >= quietPeriod);

            if (this.IsShuttingDown)
            {
                return(this.TerminationCompletion);
            }

            bool inEventLoop = this.InLoop;
            bool wakeup;
            int  oldState;

            while (true)
            {
                if (this.IsShuttingDown)
                {
                    return(this.TerminationCompletion);
                }
                int newState;
                wakeup   = true;
                oldState = this.executionState;
                if (inEventLoop)
                {
                    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(inEventLoop);
            }

            return(this.TerminationCompletion);
        }
Beispiel #10
0
        public virtual 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 StateWithContextScheduledAsyncTask(this, action, context, state, PreciseTimeSpan.Deadline(delay), cancellationToken)).Completion);
        }
Beispiel #11
0
        public virtual IScheduledTask Schedule(Action <object, object> action, object context, object state, TimeSpan delay)
        {
            Require.NotNull(action);

            return(this.Schedule(new StateActionWithContextScheduledTask(this, action, context, state, PreciseTimeSpan.Deadline(delay))));
        }
Beispiel #12
0
        public virtual IScheduledTask Schedule(Action action, TimeSpan delay)
        {
            Require.NotNull(action);

            return(this.Schedule(new ActionScheduledTask(this, action, PreciseTimeSpan.Deadline(delay))));
        }
Beispiel #13
0
 protected ScheduledTask(Executor executor, PreciseTimeSpan deadline, TaskCompletionSource promise)
 {
     this.Executor = executor;
     this.Promise  = promise;
     this.Deadline = deadline;
 }