// // Allocates a new {SocketAsyncEngine, handle} pair. // private static void AllocateToken(SocketAsyncContext context, out SocketAsyncEngine engine, out IntPtr handle) { lock (s_lock) { if (s_currentEngine == null) { s_currentEngine = new SocketAsyncEngine(); } engine = s_currentEngine; handle = s_currentEngine.AllocateHandle(context); } }
private IntPtr AllocateHandle(SocketAsyncContext context) { Debug.Assert(Monitor.IsEntered(s_lock), "Expected s_lock to be held"); Debug.Assert(!IsFull, "Expected !IsFull"); IntPtr handle = _nextHandle; _handleToContextMap.Add(handle, context); _nextHandle = IntPtr.Add(_nextHandle, 1); _outstandingHandles = IntPtr.Add(_outstandingHandles, 1); if (IsFull) { // We'll need to create a new event port for the next handle. s_currentEngine = null; } Debug.Assert(handle != ShutdownHandle, $"Expected handle != ShutdownHandle: {handle}"); return(handle); }