From 0742d113bedaaff35bd0159a5d8c33aacc38d3cd Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Mon, 29 Aug 2016 16:32:03 -0700 Subject: [PATCH] Make all calls to ThreadPool.QueueUserWorkItem through IThreadPool - This allows SocketOutputTests to cause QUWI to exec synchronously - To avoid allocations, the logger can't be captured making it "unsafe" --- .../Internal/Http/SocketOutput.cs | 10 +++++----- .../Internal/Infrastructure/IThreadPool.cs | 2 ++ .../Internal/Infrastructure/LoggingThreadPool.cs | 5 +++++ .../TestHelpers/SynchronousThreadPool.cs | 6 ++++++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/SocketOutput.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/SocketOutput.cs index 0b02da9748..d3064e27e9 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/SocketOutput.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/SocketOutput.cs @@ -288,7 +288,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http if (blockToReturn != null) { - ThreadPool.QueueUserWorkItem(_returnBlocks, blockToReturn); + _threadPool.UnsafeRun(_returnBlocks, blockToReturn); } } @@ -542,8 +542,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http private class WriteContext { - private static WaitCallback _returnWrittenBlocks = (state) => ReturnWrittenBlocks((MemoryPoolBlock)state); - private static WaitCallback _completeWrite = (state) => ((WriteContext)state).CompleteOnThreadPool(); + private static readonly WaitCallback _returnWrittenBlocks = (state) => ReturnWrittenBlocks((MemoryPoolBlock)state); + private static readonly WaitCallback _completeWrite = (state) => ((WriteContext)state).CompleteOnThreadPool(); private SocketOutput Self; private UvWriteReq _writeReq; @@ -658,7 +658,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http } else { - ThreadPool.QueueUserWorkItem(_completeWrite, this); + Self._threadPool.UnsafeRun(_completeWrite, this); } } @@ -697,7 +697,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http } block.Next = null; - ThreadPool.QueueUserWorkItem(_returnWrittenBlocks, _lockedStart.Block); + Self._threadPool.UnsafeRun(_returnWrittenBlocks, _lockedStart.Block); } private static void ReturnWrittenBlocks(MemoryPoolBlock block) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/IThreadPool.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/IThreadPool.cs index e4fe4eb40a..af5b6df175 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/IThreadPool.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/IThreadPool.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Threading; using System.Threading.Tasks; namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure @@ -12,5 +13,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure void Cancel(TaskCompletionSource tcs); void Error(TaskCompletionSource tcs, Exception ex); void Run(Action action); + void UnsafeRun(WaitCallback action, object state); } } \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/LoggingThreadPool.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/LoggingThreadPool.cs index 6cdf0014c2..bbbac1b6f7 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/LoggingThreadPool.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/LoggingThreadPool.cs @@ -63,6 +63,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure ThreadPool.QueueUserWorkItem(_runAction, action); } + public void UnsafeRun(WaitCallback action, object state) + { + ThreadPool.QueueUserWorkItem(action, state); + } + public void Complete(TaskCompletionSource tcs) { ThreadPool.QueueUserWorkItem(_completeTcs, tcs); diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/SynchronousThreadPool.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/SynchronousThreadPool.cs index ac7e2717e4..15994044d2 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/SynchronousThreadPool.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/SynchronousThreadPool.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; @@ -28,5 +29,10 @@ namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers { action(); } + + public void UnsafeRun(WaitCallback action, object state) + { + action(state); + } } }