From 553aed1c77aa1903d1d79d0a1e4bd8bcb66db189 Mon Sep 17 00:00:00 2001 From: Tom Deseyn Date: Wed, 17 May 2017 10:30:06 +0200 Subject: [PATCH] Refactor --- .../Net/Sockets/SocketAsyncEngine.Unix.cs | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs b/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs index c7b81138a169..f9aec91675c7 100644 --- a/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs +++ b/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs @@ -102,7 +102,7 @@ public bool TryRegister(SafeCloseSocket socket, Interop.Sys.SocketEvents current // private static readonly IntPtr MaxHandles = IntPtr.Size == 4 ? (IntPtr)int.MaxValue : (IntPtr)long.MaxValue; #endif - private static readonly IntPtr MinHandlesAdditionalEngine = EngineCount == 1 ? MaxHandles : (IntPtr)32; + private static readonly IntPtr MinHandlesForAdditionalEngine = EngineCount == 1 ? MaxHandles : (IntPtr)32; // // Sentinel handle value to identify events from the "shutdown pipe," used to signal an event loop to stop @@ -134,6 +134,16 @@ public bool TryRegister(SafeCloseSocket socket, Interop.Sys.SocketEvents current // private bool IsFull { get { return _nextHandle == MaxHandles; } } + // True if we've don't have sufficient active sockets to allow allocating a new engine. + private bool HasLowNumberOfSockets + { + get + { + return IntPtr.Size == 4 ? _outstandingHandles.ToInt32() < MinHandlesForAdditionalEngine.ToInt32() : + _outstandingHandles.ToInt64() < MinHandlesForAdditionalEngine.ToInt64(); + } + } + // // Allocates a new {SocketAsyncEngine, handle} pair. // @@ -144,12 +154,11 @@ private static void AllocateToken(SocketAsyncContext context, out SocketAsyncEng engine = s_currentEngines[s_allocateFromEngine]; if (engine == null) { - // Minimize the number of engines on applications which have a low number of concurrent sockets. + // We minimize the number of engines on applications that have a low number of concurrent sockets. for (int i = 0; i < s_allocateFromEngine; i++) { var previousEngine = s_currentEngines[i]; - if (previousEngine == null || - previousEngine._outstandingHandles.ToInt64() < MinHandlesAdditionalEngine.ToInt64()) + if (previousEngine == null || previousEngine.HasLowNumberOfSockets) { s_allocateFromEngine = i; engine = previousEngine; @@ -170,8 +179,8 @@ private static void AllocateToken(SocketAsyncContext context, out SocketAsyncEng s_currentEngines[s_allocateFromEngine] = null; } - // Round-robin to the next engine. - if (engine._outstandingHandles.ToInt64() >= MinHandlesAdditionalEngine.ToInt64()) + // Round-robin to the next engine once we have sufficient sockets on this one. + if (!engine.HasLowNumberOfSockets) { s_allocateFromEngine = (s_allocateFromEngine + 1) % EngineCount; }