Skip to content

Commit

Permalink
Merge pull request dotnet#21051 from stephentoub/socket_perf
Browse files Browse the repository at this point in the history
Minor perf improvements to Sockets
  • Loading branch information
stephentoub authored Jun 14, 2017
2 parents 7208a27 + 7f22c8d commit 7854da4
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 173 deletions.
70 changes: 37 additions & 33 deletions src/Common/src/System/Net/SafeCloseSocket.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,53 +32,57 @@ public ThreadPoolBoundHandle IOCPBoundHandle

// Binds the Socket Win32 Handle to the ThreadPool's CompletionPort.
public ThreadPoolBoundHandle GetOrAllocateThreadPoolBoundHandle(bool trySkipCompletionPortOnSuccess)
{
// Check to see if the socket native _handle is already
// bound to the ThreadPool's completion port.
if (_released || _iocpBoundHandle == null)
{
GetOrAllocateThreadPoolBoundHandleSlow(trySkipCompletionPortOnSuccess);
}

return _iocpBoundHandle;
}

private void GetOrAllocateThreadPoolBoundHandleSlow(bool trySkipCompletionPortOnSuccess)
{
if (_released)
{
// Keep the exception message pointing at the external type.
throw new ObjectDisposedException(typeof(Socket).FullName);
}

// Check to see if the socket native _handle is already
// bound to the ThreadPool's completion port.
if (_iocpBoundHandle == null)
lock (_iocpBindingLock)
{
lock (_iocpBindingLock)
if (_iocpBoundHandle == null)
{
if (_iocpBoundHandle == null)
{
// Bind the socket native _handle to the ThreadPool.
if (NetEventSource.IsEnabled) NetEventSource.Info(this, "calling ThreadPool.BindHandle()");
// Bind the socket native _handle to the ThreadPool.
if (NetEventSource.IsEnabled) NetEventSource.Info(this, "calling ThreadPool.BindHandle()");

ThreadPoolBoundHandle boundHandle;
try
{
// The handle (this) may have been already released:
// E.g.: The socket has been disposed in the main thread. A completion callback may
// attempt starting another operation.
boundHandle = ThreadPoolBoundHandle.BindHandle(this);
}
catch (Exception exception)
{
if (ExceptionCheck.IsFatal(exception)) throw;
CloseAsIs();
throw;
}

// Try to disable completions for synchronous success, if requested
if (trySkipCompletionPortOnSuccess &&
CompletionPortHelper.SkipCompletionPortOnSuccess(boundHandle.Handle))
{
_skipCompletionPortOnSuccess = true;
}
ThreadPoolBoundHandle boundHandle;
try
{
// The handle (this) may have been already released:
// E.g.: The socket has been disposed in the main thread. A completion callback may
// attempt starting another operation.
boundHandle = ThreadPoolBoundHandle.BindHandle(this);
}
catch (Exception exception) when (!ExceptionCheck.IsFatal(exception))
{
CloseAsIs();
throw;
}

// Don't set this until after we've configured the handle above (if we did)
_iocpBoundHandle = boundHandle;
// Try to disable completions for synchronous success, if requested
if (trySkipCompletionPortOnSuccess &&
CompletionPortHelper.SkipCompletionPortOnSuccess(boundHandle.Handle))
{
_skipCompletionPortOnSuccess = true;
}

// Don't set this until after we've configured the handle above (if we did)
_iocpBoundHandle = boundHandle;
}
}

return _iocpBoundHandle;
}

public bool SkipCompletionPortOnSuccess
Expand Down
Loading

0 comments on commit 7854da4

Please sign in to comment.