From 93cbc0974aa0475c586f40645e5b58a4b08ef017 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 24 Nov 2020 20:25:27 -0500 Subject: [PATCH] Remove ForceAsync from CryptoStream (#45150) --- .../Threading/Tasks/ForceAsyncAwaiter.cs | 45 ------------------- .../src/System.IO.Pipes.csproj | 2 - ...em.Security.Cryptography.Primitives.csproj | 2 - .../Security/Cryptography/CryptoStream.cs | 12 ++--- 4 files changed, 6 insertions(+), 55 deletions(-) delete mode 100644 src/libraries/Common/src/System/Threading/Tasks/ForceAsyncAwaiter.cs diff --git a/src/libraries/Common/src/System/Threading/Tasks/ForceAsyncAwaiter.cs b/src/libraries/Common/src/System/Threading/Tasks/ForceAsyncAwaiter.cs deleted file mode 100644 index 9b5d88f5b993b..0000000000000 --- a/src/libraries/Common/src/System/Threading/Tasks/ForceAsyncAwaiter.cs +++ /dev/null @@ -1,45 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Runtime.CompilerServices; - -namespace System.Threading.Tasks -{ - internal static partial class TaskAwaiters - { - /// - /// Returns an awaitable/awaiter that will ensure the continuation is executed - /// asynchronously on the thread pool, even if the task is already completed - /// by the time the await occurs. Effectively, it is equivalent to awaiting - /// with ConfigureAwait(false) and then queuing the continuation with Task.Run, - /// but it avoids the extra hop if the continuation already executed asynchronously. - /// - public static ForceAsyncAwaiter ForceAsync(this Task task) - { - return new ForceAsyncAwaiter(task); - } - } - - internal readonly struct ForceAsyncAwaiter : ICriticalNotifyCompletion - { - private readonly Task _task; - - internal ForceAsyncAwaiter(Task task) { _task = task; } - - public ForceAsyncAwaiter GetAwaiter() { return this; } - - public bool IsCompleted { get { return false; } } // the purpose of this type is to always force a continuation - - public void GetResult() { _task.GetAwaiter().GetResult(); } - - public void OnCompleted(Action action) - { - _task.ConfigureAwait(false).GetAwaiter().OnCompleted(action); - } - - public void UnsafeOnCompleted(Action action) - { - _task.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted(action); - } - } -} diff --git a/src/libraries/System.IO.Pipes/src/System.IO.Pipes.csproj b/src/libraries/System.IO.Pipes/src/System.IO.Pipes.csproj index 66f0da7b4d6a6..a89cd7202c08e 100644 --- a/src/libraries/System.IO.Pipes/src/System.IO.Pipes.csproj +++ b/src/libraries/System.IO.Pipes/src/System.IO.Pipes.csproj @@ -167,8 +167,6 @@ Link="Common\Interop\Unix\Interop.GetEUid.cs" /> - diff --git a/src/libraries/System.Security.Cryptography.Primitives/src/System.Security.Cryptography.Primitives.csproj b/src/libraries/System.Security.Cryptography.Primitives/src/System.Security.Cryptography.Primitives.csproj index 681bb084de8ac..e75bb29692d4f 100644 --- a/src/libraries/System.Security.Cryptography.Primitives/src/System.Security.Cryptography.Primitives.csproj +++ b/src/libraries/System.Security.Cryptography.Primitives/src/System.Security.Cryptography.Primitives.csproj @@ -25,8 +25,6 @@ - ReadAsyncInternal(byte[] buffer, int offset, int count, // async requests outstanding, we will block the application's main // thread if it does a second IO request until the first one completes. - SemaphoreSlim semaphore = AsyncActiveSemaphore; - await semaphore.WaitAsync(cancellationToken).ForceAsync(); + await AsyncActiveSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); try { return await ReadAsyncCore(buffer, offset, count, cancellationToken, useAsync: true).ConfigureAwait(false); } finally { - semaphore.Release(); + _lazyAsyncActiveSemaphore.Release(); } } @@ -495,15 +495,14 @@ private async Task WriteAsyncInternal(byte[] buffer, int offset, int count, Canc // async requests outstanding, we will block the application's main // thread if it does a second IO request until the first one completes. - SemaphoreSlim semaphore = AsyncActiveSemaphore; - await semaphore.WaitAsync(cancellationToken).ForceAsync(); + await AsyncActiveSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); try { await WriteAsyncCore(buffer, offset, count, cancellationToken, useAsync: true).ConfigureAwait(false); } finally { - semaphore.Release(); + _lazyAsyncActiveSemaphore.Release(); } } @@ -748,6 +747,7 @@ private void InitializeBuffer() } } + [MemberNotNull(nameof(_lazyAsyncActiveSemaphore))] private SemaphoreSlim AsyncActiveSemaphore { get