Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/System.Net.Sockets/src/System.Net.Sockets.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@
<Reference Include="Microsoft.Win32.Primitives" />
<Reference Include="System.Buffers" />
<Reference Include="System.Collections" />
<Reference Include="System.Collections.Concurrent" />
<Reference Include="System.Diagnostics.Debug" />
<Reference Include="System.Diagnostics.Tracing" />
<Reference Include="System.IO.FileSystem" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
Expand Down Expand Up @@ -124,9 +124,8 @@ public bool TryRegister(SafeSocketHandle socket, out Interop.Error error)

//
// Maps handle values to SocketAsyncContext instances.
// Must be accessed under s_lock.
//
private readonly Dictionary<IntPtr, SocketAsyncContext> _handleToContextMap = new Dictionary<IntPtr, SocketAsyncContext>();
private readonly ConcurrentDictionary<IntPtr, SocketAsyncContext> _handleToContextMap = new ConcurrentDictionary<IntPtr, SocketAsyncContext>();

//
// True if we've reached the handle value limit for this event port, and thus must allocate a new event port
Expand Down Expand Up @@ -193,7 +192,7 @@ private IntPtr AllocateHandle(SocketAsyncContext context)
Debug.Assert(!IsFull, "Expected !IsFull");

IntPtr handle = _nextHandle;
_handleToContextMap.Add(handle, context);
_handleToContextMap.TryAdd(handle, context);

_nextHandle = IntPtr.Add(_nextHandle, 1);
_outstandingHandles = IntPtr.Add(_outstandingHandles, 1);
Expand All @@ -210,7 +209,7 @@ private void FreeHandle(IntPtr handle)

lock (s_lock)
{
if (_handleToContextMap.Remove(handle))
if (_handleToContextMap.TryRemove(handle, out _))
{
_outstandingHandles = IntPtr.Subtract(_outstandingHandles, 1);
Debug.Assert(_outstandingHandles.ToInt64() >= 0, $"Unexpected _outstandingHandles: {_outstandingHandles}");
Expand All @@ -235,18 +234,6 @@ private void FreeHandle(IntPtr handle)
}
}

private SocketAsyncContext GetContextFromHandle(IntPtr handle)
{
Debug.Assert(handle != ShutdownHandle, $"Expected handle != ShutdownHandle: {handle}");
Debug.Assert(handle.ToInt64() < MaxHandles.ToInt64(), $"Unexpected values: handle={handle}, MaxHandles={MaxHandles}");
lock (s_lock)
{
SocketAsyncContext context;
_handleToContextMap.TryGetValue(handle, out context);
return context;
}
}

private SocketAsyncEngine()
{
_port = (IntPtr)(-1);
Expand Down Expand Up @@ -339,7 +326,8 @@ private void EventLoop()
}
else
{
SocketAsyncContext context = GetContextFromHandle(handle);
Debug.Assert(handle.ToInt64() < MaxHandles.ToInt64(), $"Unexpected values: handle={handle}, MaxHandles={MaxHandles}");
_handleToContextMap.TryGetValue(handle, out SocketAsyncContext context);
if (context != null)
{
context.HandleEvents(_buffer[i].Events);
Expand Down