Example #1
0
        public unsafe RIOThreadPool(RIO rio, IntPtr socket, CancellationToken token)
        {
            _socket = socket;
            _rio = rio;
            _token = token;

            _maxThreads = Environment.ProcessorCount;

            _workers = new WorkBundle[_maxThreads];
            for (var i = 0; i < _workers.Length; i++)
            {
                var worker = new WorkBundle()
                {
                    id = i,
                    bufferPool = new RIOBufferPool(_rio)
                };
                worker.completionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, IntPtr.Zero, 0, 0);

                if (worker.completionPort == IntPtr.Zero)
                {
                    var error = GetLastError();
                    RIOImports.WSACleanup();
                    throw new Exception(String.Format("ERROR: CreateIoCompletionPort returned {0}", error));
                }

                var completionMethod = new RIO_NOTIFICATION_COMPLETION()
                {
                    Type = RIO_NOTIFICATION_COMPLETION_TYPE.IOCP_COMPLETION,
                    Iocp = new RIO_NOTIFICATION_COMPLETION_IOCP()
                    {
                        IocpHandle = worker.completionPort,
                        QueueCorrelation = (ulong)i,
                        Overlapped = (NativeOverlapped*)(-1)// nativeOverlapped
                    }
                };
                worker.completionQueue = _rio.CreateCompletionQueue(MaxOutsandingCompletions, completionMethod);

                if (worker.completionQueue == IntPtr.Zero)
                {
                    var error = RIOImports.WSAGetLastError();
                    RIOImports.WSACleanup();
                    throw new Exception(String.Format("ERROR: CreateCompletionQueue returned {0}", error));
                }

                worker.connections = new ConcurrentDictionary<long, TcpConnection>();
                worker.thread = new Thread(GetThreadStart(i));
                worker.thread.IsBackground = true;
                _workers[i] = worker;
            }

            // gc
            //GC.Collect(2, GCCollectionMode.Forced, true, true);
            //GC.WaitForPendingFinalizers();
            //GC.Collect(2, GCCollectionMode.Forced, true, true);

            GC.Collect(2, GCCollectionMode.Forced, true);
            GC.WaitForPendingFinalizers();
            GC.Collect(2, GCCollectionMode.Forced, true);

            for (var i = 0; i < _workers.Length; i++)
            {
                // pin buffers
                _workers[i].bufferPool.Initalize();
            }


            for (var i = 0; i < _workers.Length; i++)
            {
                _workers[i].thread.Start();
            }
        }
Example #2
0
        public unsafe RIOThreadPool(RIO rio, IntPtr socket, CancellationToken token)
        {
            _socket = socket;
            _rio    = rio;
            _token  = token;

            _maxThreads = Environment.ProcessorCount;

            _workers = new WorkBundle[_maxThreads];
            for (var i = 0; i < _workers.Length; i++)
            {
                var worker = new WorkBundle()
                {
                    id         = i,
                    bufferPool = new RIOBufferPool(_rio)
                };
                worker.completionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, IntPtr.Zero, 0, 0);

                if (worker.completionPort == IntPtr.Zero)
                {
                    var error = GetLastError();
                    RIOImports.WSACleanup();
                    throw new Exception(String.Format("ERROR: CreateIoCompletionPort returned {0}", error));
                }

                var completionMethod = new RIO_NOTIFICATION_COMPLETION()
                {
                    Type = RIO_NOTIFICATION_COMPLETION_TYPE.IOCP_COMPLETION,
                    Iocp = new RIO_NOTIFICATION_COMPLETION_IOCP()
                    {
                        IocpHandle       = worker.completionPort,
                        QueueCorrelation = (ulong)i,
                        Overlapped       = (NativeOverlapped *)(-1)// nativeOverlapped
                    }
                };
                worker.completionQueue = _rio.CreateCompletionQueue(MaxOutsandingCompletions, completionMethod);

                if (worker.completionQueue == IntPtr.Zero)
                {
                    var error = RIOImports.WSAGetLastError();
                    RIOImports.WSACleanup();
                    throw new Exception(String.Format("ERROR: CreateCompletionQueue returned {0}", error));
                }

                worker.connections         = new ConcurrentDictionary <long, TcpConnection>();
                worker.thread              = new Thread(GetThreadStart(i));
                worker.thread.IsBackground = true;
                _workers[i] = worker;
            }

            // gc
            //GC.Collect(2, GCCollectionMode.Forced, true, true);
            //GC.WaitForPendingFinalizers();
            //GC.Collect(2, GCCollectionMode.Forced, true, true);

            GC.Collect(2, GCCollectionMode.Forced, true);
            GC.WaitForPendingFinalizers();
            GC.Collect(2, GCCollectionMode.Forced, true);

            for (var i = 0; i < _workers.Length; i++)
            {
                // pin buffers
                _workers[i].bufferPool.Initalize();
            }


            for (var i = 0; i < _workers.Length; i++)
            {
                _workers[i].thread.Start();
            }
        }