Beispiel #1
0
        protected SingleThreadEventExecutorOld(IEventExecutorGroup parent, string threadName, TimeSpan breakoutInterval, IQueue <IRunnable> taskQueue)
            : base(parent)
        {
            _firstTask     = true;
            _emptyEvent    = new ManualResetEventSlim(false, 1);
            _shutdownHooks = new HashSet <Action>();

            _loopAction     = Loop;
            _loopCoreAciton = LoopCore;

            _terminationCompletionSource = NewPromise();
            _taskQueue = taskQueue;
            _preciseBreakoutInterval = PreciseTimeSpan.FromTimeSpan(breakoutInterval);
            _scheduler = new ExecutorTaskScheduler(this);
            _thread    = new Thread(_loopAction);
            if (string.IsNullOrEmpty(threadName))
            {
                _thread.Name = DefaultWorkerThreadName;
            }
            else
            {
                _thread.Name = threadName;
            }
            _thread.Start();
        }
Beispiel #2
0
 protected SingleThreadEventExecutor(IEventExecutorGroup parent, string threadName, TimeSpan breakoutInterval, IQueue <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 #3
0
 public SingleThreadEventExecutor(string threadName, TimeSpan breakoutInterval)
 {
     this.terminationCompletionSource = new TaskCompletionSource();
     this.preciseBreakoutInterval     = PreciseTimeSpan.FromTimeSpan(breakoutInterval);
     this.scheduler = new ExecutorTaskScheduler(this);
     this.thread    = new Thread(this.Loop)
     {
         IsBackground = true
     };
     if (string.IsNullOrEmpty(threadName))
     {
         this.thread.Name = DefaultWorkerThreadName;
     }
     else
     {
         this.thread.Name = threadName;
     }
     this.thread.Start();
 }
Beispiel #4
0
        protected SingleThreadEventExecutor(IEventExecutorGroup parent, string threadName, TimeSpan breakoutInterval, IQueue <IRunnable> taskQueue)
            : base(parent)
        {
            _loopAction     = Loop;
            _loopCoreAciton = LoopCore;

            _terminationCompletionSource = NewPromise();
            _taskQueue = taskQueue;
            _preciseBreakoutInterval = PreciseTimeSpan.FromTimeSpan(breakoutInterval);
            _scheduler = new ExecutorTaskScheduler(this);
            _thread    = new Thread(_loopAction);
            if (string.IsNullOrEmpty(threadName))
            {
                _thread.Name = DefaultWorkerThreadName;
            }
            else
            {
                _thread.Name = threadName;
            }
            _thread.Start();
        }
Beispiel #5
0
        public LoopExecutor(IEventLoopGroup parent, string threadName, TimeSpan breakoutInterval) : base(parent)
        {
            this.preciseBreakoutInterval = PreciseTimeSpan.FromTimeSpan(breakoutInterval);
            this.preciseTimerInterval    = PreciseTimeSpan.FromTimeSpan(TimeSpan.FromTicks(breakoutInterval.Ticks * 2));

            this.terminationCompletionSource = new TaskCompletionSource();
            this.taskQueue = PlatformDependent.NewMpscQueue <IRunnable>();
            this.scheduler = new ExecutorTaskScheduler(this);

            this.loop        = new Loop();
            this.asyncHandle = new Async(this.loop, RunAllTasksCallback, this);
            this.timerHandle = new Timer(this.loop, RunAllTasksCallback, this);
            string name = string.Format(DefaultWorkerThreadName, this.loop.Handle);

            if (!string.IsNullOrEmpty(threadName))
            {
                name = $"{name} ({threadName})";
            }
            this.thread = new XThread(RunLoop)
            {
                Name = name
            };
        }
Beispiel #6
0
        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.InEventLoop;
            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)
            //{
            //    wakeup(inEventLoop);
            //}

            return(this.TerminationCompletion);
        }
Beispiel #7
0
        /// <inheritdoc cref="IEventExecutor"/>
        public override Task ShutdownGracefullyAsync(TimeSpan quietPeriod, TimeSpan timeout)
        {
            if (quietPeriod < TimeSpan.Zero)
            {
                ThrowHelper.ThrowArgumentException_MustBeGreaterThanOrEquelToZero(quietPeriod);
            }
            if (timeout < quietPeriod)
            {
                ThrowHelper.ThrowArgumentException_MustBeGreaterThanQuietPeriod(timeout, quietPeriod);
            }

            if (IsShuttingDown)
            {
                return(TerminationCompletion);
            }

            bool inEventLoop = InEventLoop;
            bool wakeup;
            int  thisState = Volatile.Read(ref v_executionState);
            int  oldState;

            do
            {
                if (IsShuttingDown)
                {
                    return(TerminationCompletion);
                }
                int newState;
                wakeup   = true;
                oldState = thisState;
                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;
                    }
                }
                thisState = Interlocked.CompareExchange(ref v_executionState, newState, oldState);
            } while (thisState != oldState);
            _gracefulShutdownQuietPeriod = PreciseTimeSpan.FromTimeSpan(quietPeriod);
            _gracefulShutdownTimeout     = PreciseTimeSpan.FromTimeSpan(timeout);

            // TODO: revisit
            //if (ensureThreadStarted(oldState))
            //{
            //    return terminationFuture;
            //}

            if (wakeup)
            {
                WakeUp(inEventLoop);
            }

            return(TerminationCompletion);
        }