Exemplo n.º 1
0
        /// <summary>Creates a new instance of <see cref="MultithreadEventExecutorGroup{T1, T2}"/>.</summary>
        protected MultithreadEventExecutorGroup(int nThreads,
                                                IEventExecutorChooserFactory <TEventExecutor> chooserFactory,
                                                Func <TExecutorGroup, TEventExecutor> eventExecutorFactory)
        {
            if (nThreads <= 0)
            {
                ThrowHelper.ThrowArgumentException_Positive(nThreads, ExceptionArgument.nThreads);
            }
            if (chooserFactory is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.chooserFactory);
            }
            if (eventExecutorFactory is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.eventExecutorFactory);
            }

            _children = new TEventExecutor[nThreads];
            var terminationTasks = new Task[nThreads];

            for (int i = 0; i < nThreads; i++)
            {
                TEventExecutor eventLoop = null;
                bool           success   = false;
                try
                {
                    eventLoop = eventExecutorFactory((TExecutorGroup)this);
                    success   = true;
                }
                catch (Exception ex)
                {
                    ThrowHelper.ThrowInvalidOperationException_FailedToCreateAChildEventLoop(ex);
                }
                finally
                {
                    if (!success)
                    {
                        Task.WhenAll(_children
                                     .Take(i)
                                     .Select(loop => loop.ShutdownGracefullyAsync()))
                        .Wait();
                    }
                }

                _children[i]        = eventLoop;
                terminationTasks[i] = eventLoop.TerminationCompletion;
            }
            TerminationCompletion = Task.WhenAll(terminationTasks);

            _chooser = chooserFactory.NewChooser(_children);
        }
Exemplo n.º 2
0
        public WorkerEventLoopGroup(DispatcherEventLoopGroup eventLoopGroup, int nThreads, IThreadFactory threadFactory, IEventExecutorChooserFactory <WorkerEventLoop> chooserFactory, IRejectedExecutionHandler rejectedHandler, TimeSpan breakoutInterval)
        {
            if (eventLoopGroup is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.eventLoopGroup);
            }
            if (chooserFactory is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.chooserFactory);
            }
            if ((uint)(nThreads - 1) > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentException_Positive(nThreads, ExceptionArgument.nThreads);
            }

            _dispatcherLoop = eventLoopGroup.Dispatcher;
            PipeName        = _dispatcherLoop.PipeName;

            // Wait until the pipe is listening to connect
            _dispatcherLoop.WaitForLoopRun(StartTimeout);

            nThreads = 0u >= (uint)nThreads ? DefaultEventLoopThreadCount : nThreads;

            _eventLoops = new WorkerEventLoop[nThreads];
            var terminationTasks = new Task[nThreads];

            for (int i = 0; i < nThreads; i++)
            {
                WorkerEventLoop eventLoop = null;
                bool            success   = false;
                try
                {
                    eventLoop = new WorkerEventLoop(this, threadFactory, rejectedHandler, breakoutInterval);
                    success   = eventLoop.ConnectTask.Wait(StartTimeout);
                    if (!success)
                    {
                        ThrowHelper.ThrowTimeoutException(PipeName);
                    }
                }
                catch (Exception ex)
                {
                    ThrowHelper.ThrowInvalidOperationException_CreateChild(ex);
                }
                finally
                {
                    if (!success)
                    {
                        Task.WhenAll(_eventLoops.Take(i).Select(loop => loop.ShutdownGracefullyAsync())).Wait();
                    }
                }

                _eventLoops[i]      = eventLoop;
                terminationTasks[i] = eventLoop.TerminationCompletion;
            }

            _chooser = chooserFactory.NewChooser(_eventLoops);

            TerminationCompletion = Task.WhenAll(terminationTasks);
        }
Exemplo n.º 3
0
 public WorkerEventLoopGroup(DispatcherEventLoopGroup eventLoopGroup, int nThreads, IEventExecutorChooserFactory <WorkerEventLoop> chooserFactory, IRejectedExecutionHandler rejectedHandler, TimeSpan breakoutInterval)
     : this(eventLoopGroup, nThreads, DefaultThreadFactory <WorkerEventLoopGroup> .Instance, chooserFactory, rejectedHandler, breakoutInterval)
 {
 }
Exemplo n.º 4
0
 public WorkerEventLoopGroup(DispatcherEventLoopGroup eventLoopGroup, int nThreads, IEventExecutorChooserFactory <WorkerEventLoop> chooserFactory, IRejectedExecutionHandler rejectedHandler)
     : this(eventLoopGroup, nThreads, chooserFactory, rejectedHandler, LoopExecutor.DefaultBreakoutInterval)
 {
 }
Exemplo n.º 5
0
 /// <summary>Creates a new instance of <see cref="DefaultEventExecutorGroup"/>.</summary>
 public DefaultEventExecutorGroup(int nThreads, IThreadFactory threadFactory, IEventExecutorChooserFactory <DefaultEventExecutor> chooserFactory,
                                  IRejectedExecutionHandler rejectedHandler, IEventExecutorTaskQueueFactory queueFactory)
     : base(nThreads, chooserFactory, group => new DefaultEventExecutor(group, threadFactory, rejectedHandler, queueFactory))
 {
 }
Exemplo n.º 6
0
 /// <summary>Creates a new instance of <see cref="DefaultEventExecutorGroup"/>.</summary>
 public DefaultEventExecutorGroup(int nThreads, IThreadFactory threadFactory, IEventExecutorChooserFactory <DefaultEventExecutor> chooserFactory,
                                  IRejectedExecutionHandler rejectedHandler, int maxPendingTasks)
     : base(nThreads, chooserFactory, group => new DefaultEventExecutor(group, threadFactory, rejectedHandler, maxPendingTasks))
 {
 }
Exemplo n.º 7
0
 /// <summary>Creates a new instance of <see cref="DefaultEventExecutorGroup"/>.</summary>
 public DefaultEventExecutorGroup(int nThreads, IEventExecutorChooserFactory <DefaultEventExecutor> chooserFactory, IEventExecutorTaskQueueFactory queueFactory)
     : base(nThreads, chooserFactory, group => new DefaultEventExecutor(group, queueFactory))
 {
 }
Exemplo n.º 8
0
 /// <summary>Creates a new instance of <see cref="MultithreadEventLoopGroup"/>.</summary>
 public MultithreadEventLoopGroup(int nThreads, IEventExecutorChooserFactory <SingleThreadEventLoop> chooserFactory)
     : base(nThreads, chooserFactory, DefaultEventLoopFactory)
 {
 }
Exemplo n.º 9
0
 /// <summary>Creates a new instance of <see cref="DefaultEventExecutorGroup"/>.</summary>
 public DefaultEventExecutorGroup(int nThreads, IEventExecutorChooserFactory <DefaultEventExecutor> chooserFactory, IRejectedExecutionHandler rejectedHandler)
     : base(nThreads, chooserFactory, group => new DefaultEventExecutor(group, rejectedHandler, queueFactory: null))
 {
 }
Exemplo n.º 10
0
 /// <summary>Creates a new instance of <see cref="DefaultEventExecutorGroup"/>.</summary>
 public DefaultEventExecutorGroup(int nThreads, IEventExecutorChooserFactory <DefaultEventExecutor> chooserFactory)
     : base(nThreads, chooserFactory, DefaultEventExecutorFactory)
 {
 }
Exemplo n.º 11
0
 /// <summary>Creates a new instance of <see cref="DefaultEventExecutorGroup"/>.</summary>
 public DefaultEventExecutorGroup(IEventExecutorChooserFactory <DefaultEventExecutor> chooserFactory)
     : base(0, chooserFactory, DefaultEventExecutorFactory)
 {
 }
Exemplo n.º 12
0
 /// <summary>Creates a new instance of <see cref="MultithreadEventLoopGroup"/>.</summary>
 public MultithreadEventLoopGroup(int nThreads, IThreadFactory threadFactory, IEventExecutorChooserFactory <SingleThreadEventLoop> chooserFactory,
                                  IRejectedExecutionHandler rejectedHandler, TimeSpan breakoutInterval)
     : base(nThreads, chooserFactory, group => new SingleThreadEventLoop(group, threadFactory, rejectedHandler, breakoutInterval))
 {
 }
Exemplo n.º 13
0
 /// <summary>Creates a new instance of <see cref="MultithreadEventLoopGroup"/>.</summary>
 public MultithreadEventLoopGroup(int nThreads, IEventExecutorChooserFactory <SingleThreadEventLoop> chooserFactory, IRejectedExecutionHandler rejectedHandler)
     : base(nThreads, chooserFactory, group => new SingleThreadEventLoop(group, rejectedHandler))
 {
 }
Exemplo n.º 14
0
 /// <summary>Creates a new instance of <see cref="MultithreadEventLoopGroup"/>.</summary>
 public MultithreadEventLoopGroup(int nThreads, IEventExecutorChooserFactory <SingleThreadEventLoop> chooserFactory, TimeSpan breakoutInterval)
     : base(nThreads, chooserFactory, group => new SingleThreadEventLoop(group, breakoutInterval))
 {
 }
Exemplo n.º 15
0
 public WorkerEventLoopGroup(DispatcherEventLoopGroup eventLoopGroup, int nThreads, IEventExecutorChooserFactory <WorkerEventLoop> chooserFactory)
     : this(eventLoopGroup, nThreads, chooserFactory, RejectedExecutionHandlers.Reject())
 {
 }
 /// <summary>Creates a new instance of <see cref="MultithreadEventLoopGroup{TGroup, TExecutor}"/>.</summary>
 protected MultithreadEventLoopGroup(int nThreads,
                                     IEventExecutorChooserFactory <TEventLoop> chooserFactory,
                                     Func <TLoopGroup, TEventLoop> eventLoopFactory)
     : base(0u >= (uint)nThreads ? DefaultEventLoopThreadCount : nThreads, chooserFactory, eventLoopFactory)
 {
 }
Exemplo n.º 17
0
 /// <summary>Creates a new instance of <see cref="DefaultEventExecutorGroup"/>.</summary>
 public DefaultEventExecutorGroup(int nThreads, IEventExecutorChooserFactory <DefaultEventExecutor> chooserFactory, int maxPendingTasks)
     : base(nThreads, chooserFactory, group => new DefaultEventExecutor(group, maxPendingTasks))
 {
 }