Example #1
0
 public void OnCompleted(Action continuation)
 {
     if (_tsocket.SetWritableContinuation(continuation))
     {
         TransportThread.RegisterForWritable(_tsocket);
     }
     else
     {
         continuation();
     }
 }
            public ThreadContext(TransportThread transportThread, LinuxTransportOptions transportOptions, IConnectionHandler connectionHandler, ILogger logger)
            {
                TransportThread   = transportThread;
                ConnectionHandler = connectionHandler;

                Sockets           = new Dictionary <int, TSocket>();
                Logger            = logger;
                AcceptSockets     = new List <TSocket>();
                _schedulerAdding  = new Queue <ScheduledAction>(1024);
                _schedulerRunning = new Queue <ScheduledAction>(1024);
                _epollState       = EPollBlocked;
                SendScheduler     = transportOptions.DeferSend ? this as IScheduler : InlineScheduler.Default;
            }
Example #3
0
            public unsafe ThreadContext(TransportThread transportThread)
            {
                _transportThread      = transportThread;
                _connectionDispatcher = transportThread.ConnectionDispatcher;
                _sockets              = new Dictionary <int, TSocket>();
                _logger               = _transportThread.LoggerFactory.CreateLogger($"{nameof(RedHatX)}.{nameof(TransportThread)}.{_transportThread.ThreadId}");
                _acceptSockets        = new List <TSocket>();
                _transportOptions     = transportThread.TransportOptions;
                _scheduledSendAdding  = new List <ScheduledSend>(1024);
                _scheduledSendRunning = new List <ScheduledSend>(1024);
                _epollState           = EPollBlocked;
                if (_transportOptions.AioReceive | _transportOptions.AioSend)
                {
                    _aioEventsMemory     = AllocMemory(sizeof(AioEvent) * EventBufferLength);
                    _aioCbsMemory        = AllocMemory(sizeof(AioCb) * EventBufferLength);
                    _aioCbsTableMemory   = AllocMemory(sizeof(AioCb *) * EventBufferLength);
                    _ioVectorTableMemory = AllocMemory(sizeof(IOVector) * IoVectorsPerAioSocket * EventBufferLength);
                    for (int i = 0; i < EventBufferLength; i++)
                    {
                        AioCbsTable[i] = &AioCbs[i];
                    }
                    if (_transportOptions.AioSend)
                    {
                        _aioSendBuffers = new ReadOnlySequence <byte> [EventBufferLength];
                    }
                }
                int maxMemoryHandleCount = TSocket.MaxIOVectorReceiveLength;

                if (_transportOptions.AioReceive || _transportOptions.AioSend)
                {
                    maxMemoryHandleCount = Math.Max(maxMemoryHandleCount, EventBufferLength);
                }
                if (_transportOptions.DeferSend)
                {
                    maxMemoryHandleCount = Math.Max(maxMemoryHandleCount, TSocket.MaxIOVectorSendLength);
                }
                MemoryHandles = new MemoryHandle[maxMemoryHandleCount];

                // These members need to be Disposed
                _epoll     = EPoll.Create();
                _epollFd   = _epoll.DangerousGetHandle().ToInt32();
                MemoryPool = CreateMemoryPool();
                _pipeEnds  = PipeEnd.CreatePair(blocking: false);
                if (_aioEventsMemory != IntPtr.Zero)
                {
                    AioInterop.IoSetup(EventBufferLength, out _aioContext).ThrowOnError();
                }
            }
Example #4
0
        private TransportThread[] CreateTransportThreads()
        {
            var         threads         = new TransportThread[_transportOptions.ThreadCount];
            IList <int> preferredCpuIds = null;

            if (_transportOptions.SetThreadAffinity)
            {
                preferredCpuIds = GetPreferredCpuIds();
            }
            int cpuIdx = 0;

            for (int i = 0; i < _transportOptions.ThreadCount; i++)
            {
                int cpuId    = preferredCpuIds == null ? -1 : preferredCpuIds[cpuIdx++ % preferredCpuIds.Count];
                int threadId = Interlocked.Increment(ref s_threadId);
                var thread   = new TransportThread(_endPoint, _connectionHandler, _transportOptions, threadId, cpuId, _loggerFactory);
                threads[i] = thread;
            }
            return(threads);
        }