From: Stephen Toub Date: Fri, 24 Feb 2023 16:59:28 +0000 (-0500) Subject: Add and use public TaskToAsyncResult helper type (#82557) X-Git-Tag: accepted/tizen/unified/riscv/20231226.055536~3827 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6387a9eb56098a889021190747d31f07246dd9f2;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Add and use public TaskToAsyncResult helper type (#82557) * Add and use public TaskToAsyncResult helper type * Disable thread blocking test on browser --- diff --git a/src/libraries/Common/src/System/IO/ReadOnlyMemoryStream.cs b/src/libraries/Common/src/System/IO/ReadOnlyMemoryStream.cs index 5e73fd5..8bc8320 100644 --- a/src/libraries/Common/src/System/IO/ReadOnlyMemoryStream.cs +++ b/src/libraries/Common/src/System/IO/ReadOnlyMemoryStream.cs @@ -143,12 +143,12 @@ namespace System.IO #endif public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), callback, state); public override int EndRead(IAsyncResult asyncResult) { EnsureNotClosed(); - return TaskToApm.End(asyncResult); + return TaskToAsyncResult.End(asyncResult); } #if !NETFRAMEWORK && !NETSTANDARD2_0 diff --git a/src/libraries/Common/src/System/Threading/Tasks/TaskToApm.cs b/src/libraries/Common/src/System/Threading/Tasks/TaskToApm.cs deleted file mode 100644 index a308e1d..0000000 --- a/src/libraries/Common/src/System/Threading/Tasks/TaskToApm.cs +++ /dev/null @@ -1,133 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// Helper methods for using Tasks to implement the APM pattern. -// -// Example usage, wrapping a Task-returning FooAsync method with Begin/EndFoo methods: -// -// public IAsyncResult BeginFoo(..., AsyncCallback? callback, object? state) => -// TaskToApm.Begin(FooAsync(...), callback, state); -// -// public int EndFoo(IAsyncResult asyncResult) => -// TaskToApm.End(asyncResult); - -using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; - -namespace System.Threading.Tasks -{ - /// - /// Provides support for efficiently using Tasks to implement the APM (Begin/End) pattern. - /// - internal static class TaskToApm - { - /// - /// Marshals the Task as an IAsyncResult, using the supplied callback and state - /// to implement the APM pattern. - /// - /// The Task to be marshaled. - /// The callback to be invoked upon completion. - /// The state to be stored in the IAsyncResult. - /// An IAsyncResult to represent the task's asynchronous operation. - public static IAsyncResult Begin(Task task, AsyncCallback? callback, object? state) => - new TaskAsyncResult(task, state, callback); - - /// Processes an IAsyncResult returned by Begin. - /// The IAsyncResult to unwrap. - public static void End(IAsyncResult asyncResult) - { - if (GetTask(asyncResult) is Task t) - { - t.GetAwaiter().GetResult(); - return; - } - - ThrowArgumentException(asyncResult); - } - - /// Processes an IAsyncResult returned by Begin. - /// The IAsyncResult to unwrap. - public static TResult End(IAsyncResult asyncResult) - { - if (GetTask(asyncResult) is Task task) - { - return task.GetAwaiter().GetResult(); - } - - ThrowArgumentException(asyncResult); - return default!; // unreachable - } - - /// Gets the task represented by the IAsyncResult. - public static Task? GetTask(IAsyncResult asyncResult) => (asyncResult as TaskAsyncResult)?._task; - - /// Throws an argument exception for the invalid . - [DoesNotReturn] - private static void ThrowArgumentException(IAsyncResult asyncResult) - { - throw asyncResult is null ? - new ArgumentNullException(nameof(asyncResult)) : - new ArgumentException(null, nameof(asyncResult)); - } - - /// Provides a simple IAsyncResult that wraps a Task. - /// - /// We could use the Task as the IAsyncResult if the Task's AsyncState is the same as the object state, - /// but that's very rare, in particular in a situation where someone cares about allocation, and always - /// using TaskAsyncResult simplifies things and enables additional optimizations. - /// - internal sealed class TaskAsyncResult : IAsyncResult - { - /// The wrapped Task. - internal readonly Task _task; - /// Callback to invoke when the wrapped task completes. - private readonly AsyncCallback? _callback; - - /// Initializes the IAsyncResult with the Task to wrap and the associated object state. - /// The Task to wrap. - /// The new AsyncState value. - /// Callback to invoke when the wrapped task completes. - internal TaskAsyncResult(Task task, object? state, AsyncCallback? callback) - { - Debug.Assert(task != null); - _task = task; - AsyncState = state; - - if (task.IsCompleted) - { - // Synchronous completion. Invoke the callback. No need to store it. - CompletedSynchronously = true; - callback?.Invoke(this); - } - else if (callback != null) - { - // Asynchronous completion, and we have a callback; schedule it. We use OnCompleted rather than ContinueWith in - // order to avoid running synchronously if the task has already completed by the time we get here but still run - // synchronously as part of the task's completion if the task completes after (the more common case). - _callback = callback; - _task.ConfigureAwait(continueOnCapturedContext: false) - .GetAwaiter() - .OnCompleted(InvokeCallback); // allocates a delegate, but avoids a closure - } - } - - /// Invokes the callback. - private void InvokeCallback() - { - Debug.Assert(!CompletedSynchronously); - Debug.Assert(_callback != null); - _callback.Invoke(this); - } - - /// Gets a user-defined object that qualifies or contains information about an asynchronous operation. - public object? AsyncState { get; } - /// Gets a value that indicates whether the asynchronous operation completed synchronously. - /// This is set lazily based on whether the has completed by the time this object is created. - public bool CompletedSynchronously { get; } - /// Gets a value that indicates whether the asynchronous operation has completed. - public bool IsCompleted => _task.IsCompleted; - /// Gets a that is used to wait for an asynchronous operation to complete. - public WaitHandle AsyncWaitHandle => ((IAsyncResult)_task).AsyncWaitHandle; - } - } -} diff --git a/src/libraries/Common/src/System/Threading/Tasks/TaskToAsyncResult.cs b/src/libraries/Common/src/System/Threading/Tasks/TaskToAsyncResult.cs new file mode 100644 index 0000000..fdfd25e --- /dev/null +++ b/src/libraries/Common/src/System/Threading/Tasks/TaskToAsyncResult.cs @@ -0,0 +1,171 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics; + +namespace System.Threading.Tasks +{ + /// + /// Provides methods for using to implement the Asynchronous Programming Model + /// pattern based on "Begin" and "End" methods. + /// +#if SYSTEM_PRIVATE_CORELIB + public +#else + internal +#endif + static class TaskToAsyncResult + { + /// Creates a new from the specified , optionally invoking when the task has completed. + /// The to be wrapped in an . + /// The callback to be invoked upon 's completion. If , no callback will be invoked. + /// The state to be stored in the . + /// An to represent the task's asynchronous operation. This instance will also be passed to when it's invoked. + /// is null. + /// + /// In conjunction with the or methods, this method may be used + /// to implement the Begin/End pattern (also known as the Asynchronous Programming Model pattern, or APM). It is recommended to not expose this pattern + /// in new code; the methods on are intended only to help implement such Begin/End methods when they must be exposed, for example + /// because a base class provides virtual methods for the pattern, or when they've already been exposed and must remain for compatibility. These methods enable + /// implementing all of the core asynchronous logic via s and then easily implementing Begin/End methods around that functionality. + /// + public static IAsyncResult Begin(Task task, AsyncCallback? callback, object? state) + { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(task); +#else + if (task is null) + { + throw new ArgumentNullException(nameof(task)); + } +#endif + + return new TaskAsyncResult(task, state, callback); + } + + /// Waits for the wrapped by the returned by to complete. + /// The for which to wait. + /// is null. + /// was not produced by a call to . + /// This will propagate any exception stored in the wrapped . + public static void End(IAsyncResult asyncResult) => + Unwrap(asyncResult).GetAwaiter().GetResult(); + + /// Waits for the wrapped by the returned by to complete. + /// The for which to wait. + /// The result of the wrapped by the . + /// is null. + /// was not produced by a call to . + /// This will propagate any exception stored in the wrapped . + public static TResult End(IAsyncResult asyncResult) => + Unwrap(asyncResult).GetAwaiter().GetResult(); + + /// Extracts the underlying from an created by . + /// The created by . + /// The wrapped by the . + /// is null. + /// was not produced by a call to . + public static Task Unwrap(IAsyncResult asyncResult) + { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(asyncResult); +#else + if (asyncResult is null) + { + throw new ArgumentNullException(nameof(asyncResult)); + } +#endif + + if ((asyncResult as TaskAsyncResult)?._task is not Task task) + { + throw new ArgumentException(null, nameof(asyncResult)); + } + + return task; + } + + /// Extracts the underlying from an created by . + /// The created by . + /// The wrapped by the . + /// is null. + /// + /// was not produced by a call to , + /// or the provided to was used a generic type parameter + /// that's different from the supplied to this call. + /// + public static Task Unwrap(IAsyncResult asyncResult) + { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(asyncResult); +#else + if (asyncResult is null) + { + throw new ArgumentNullException(nameof(asyncResult)); + } +#endif + + if ((asyncResult as TaskAsyncResult)?._task is not Task task) + { + throw new ArgumentException(null, nameof(asyncResult)); + } + + return task; + } + + /// Provides a simple that wraps a . + /// + /// We could use the Task as the IAsyncResult if the Task's AsyncState is the same as the object state, + /// but that's very rare, in particular in a situation where someone cares about allocation, and always + /// using TaskAsyncResult simplifies things and enables additional optimizations. + /// + private sealed class TaskAsyncResult : IAsyncResult + { + /// The wrapped Task. + internal readonly Task _task; + /// Callback to invoke when the wrapped task completes. + private readonly AsyncCallback? _callback; + + /// Initializes the IAsyncResult with the Task to wrap and the associated object state. + /// The Task to wrap. + /// The new AsyncState value. + /// Callback to invoke when the wrapped task completes. + internal TaskAsyncResult(Task task, object? state, AsyncCallback? callback) + { + Debug.Assert(task is not null); + + _task = task; + AsyncState = state; + + if (task.IsCompleted) + { + // The task has already completed. Treat this as synchronous completion. + // Invoke the callback; no need to store it. + CompletedSynchronously = true; + callback?.Invoke(this); + } + else if (callback is not null) + { + // Asynchronous completion, and we have a callback; schedule it. We use OnCompleted rather than ContinueWith in + // order to avoid running synchronously if the task has already completed by the time we get here but still run + // synchronously as part of the task's completion if the task completes after (the more common case). + _callback = callback; + _task.ConfigureAwait(continueOnCapturedContext: false) + .GetAwaiter() + .OnCompleted(() => _callback.Invoke(this)); + } + } + + /// + public object? AsyncState { get; } + + /// + public bool CompletedSynchronously { get; } + + /// + public bool IsCompleted => _task.IsCompleted; + + /// + public WaitHandle AsyncWaitHandle => ((IAsyncResult)_task).AsyncWaitHandle; + } + } +} diff --git a/src/libraries/Common/tests/Common.Tests.csproj b/src/libraries/Common/tests/Common.Tests.csproj index 372048c..5ed2a1e 100644 --- a/src/libraries/Common/tests/Common.Tests.csproj +++ b/src/libraries/Common/tests/Common.Tests.csproj @@ -100,8 +100,6 @@ Link="System\Net\Sockets\Fletcher32.cs" /> - diff --git a/src/libraries/Common/tests/StreamConformanceTests/StreamConformanceTests.csproj b/src/libraries/Common/tests/StreamConformanceTests/StreamConformanceTests.csproj index 5514898..eebde98 100644 --- a/src/libraries/Common/tests/StreamConformanceTests/StreamConformanceTests.csproj +++ b/src/libraries/Common/tests/StreamConformanceTests/StreamConformanceTests.csproj @@ -8,7 +8,6 @@ - diff --git a/src/libraries/Common/tests/System/IO/Compression/CompressionStreamUnitTestBase.cs b/src/libraries/Common/tests/System/IO/Compression/CompressionStreamUnitTestBase.cs index 31156d7..17ec46f 100644 --- a/src/libraries/Common/tests/System/IO/Compression/CompressionStreamUnitTestBase.cs +++ b/src/libraries/Common/tests/System/IO/Compression/CompressionStreamUnitTestBase.cs @@ -549,10 +549,10 @@ namespace System.IO.Compression isSync = sync; } - public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) => TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state); - public override int EndRead(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); - public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) => TaskToApm.Begin(WriteAsync(buffer, offset, count), callback, state); - public override void EndWrite(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) => TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), callback, state); + public override int EndRead(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); + public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) => TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count), callback, state); + public override void EndWrite(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); public override async Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { diff --git a/src/libraries/Common/tests/System/IO/ConnectedStreams.cs b/src/libraries/Common/tests/System/IO/ConnectedStreams.cs index b8f3fe1..f5da8c8 100644 --- a/src/libraries/Common/tests/System/IO/ConnectedStreams.cs +++ b/src/libraries/Common/tests/System/IO/ConnectedStreams.cs @@ -159,14 +159,14 @@ namespace System.IO } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), callback, state); public override int EndRead(IAsyncResult asyncResult) { ThrowIfDisposed(); ThrowIfReadingNotSupported(); - return TaskToApm.End(asyncResult); + return TaskToAsyncResult.End(asyncResult); } public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) @@ -229,14 +229,14 @@ namespace System.IO } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(WriteAsync(buffer, offset, count), callback, state); + TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count), callback, state); public override void EndWrite(IAsyncResult asyncResult) { ThrowIfDisposed(); ThrowIfWritingNotSupported(); - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); } public override long Length => throw new NotSupportedException(); @@ -348,12 +348,12 @@ namespace System.IO } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), callback, state); public override int EndRead(IAsyncResult asyncResult) { ThrowIfDisposed(); - return TaskToApm.End(asyncResult); + return TaskToAsyncResult.End(asyncResult); } public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) @@ -402,12 +402,12 @@ namespace System.IO } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(WriteAsync(buffer, offset, count), callback, state); + TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count), callback, state); public override void EndWrite(IAsyncResult asyncResult) { ThrowIfDisposed(); - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); } public override long Length => throw new NotSupportedException(); diff --git a/src/libraries/Common/tests/System/IO/DelayStream.cs b/src/libraries/Common/tests/System/IO/DelayStream.cs index aeb6678..e0ece4e 100644 --- a/src/libraries/Common/tests/System/IO/DelayStream.cs +++ b/src/libraries/Common/tests/System/IO/DelayStream.cs @@ -43,9 +43,9 @@ namespace System.IO } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), callback, state); - public override int EndRead(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + public override int EndRead(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); public override async Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { @@ -72,9 +72,9 @@ namespace System.IO } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(WriteAsync(buffer, offset, count), callback, state); + TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count), callback, state); - public override void EndWrite(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + public override void EndWrite(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); public override void Write(byte[] buffer, int offset, int count) { diff --git a/src/libraries/System.IO.Compression.Brotli/src/System.IO.Compression.Brotli.csproj b/src/libraries/System.IO.Compression.Brotli/src/System.IO.Compression.Brotli.csproj index 6c884b2..df43122 100644 --- a/src/libraries/System.IO.Compression.Brotli/src/System.IO.Compression.Brotli.csproj +++ b/src/libraries/System.IO.Compression.Brotli/src/System.IO.Compression.Brotli.csproj @@ -1,4 +1,4 @@ - + true $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-unix;$(NetCoreAppCurrent) @@ -20,8 +20,6 @@ - diff --git a/src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/dec/BrotliStream.Decompress.cs b/src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/dec/BrotliStream.Decompress.cs index be4fe9d..ac596b9 100644 --- a/src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/dec/BrotliStream.Decompress.cs +++ b/src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/dec/BrotliStream.Decompress.cs @@ -96,7 +96,7 @@ namespace System.IO.Compression /// The current implementation does not support the read operation. /// This call cannot be completed. public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); /// Waits for the pending asynchronous read to complete. (Consider using the method instead.) /// The reference to the pending asynchronous request to finish. @@ -105,7 +105,7 @@ namespace System.IO.Compression /// did not originate from a method on the current stream. /// The end operation cannot be performed because the stream is closed. public override int EndRead(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); /// Asynchronously reads a sequence of bytes from the current Brotli stream, writes them to a byte array starting at a specified index, advances the position within the Brotli stream by the number of bytes read, and monitors cancellation requests. /// The buffer to write the data into. diff --git a/src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/enc/BrotliStream.Compress.cs b/src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/enc/BrotliStream.Compress.cs index 1ecfc0b..207c552 100644 --- a/src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/enc/BrotliStream.Compress.cs +++ b/src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/enc/BrotliStream.Compress.cs @@ -94,13 +94,13 @@ namespace System.IO.Compression /// The current implementation does not support the write operation. /// The write operation cannot be performed because the stream is closed. public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); + TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); /// Handles the end of an asynchronous write operation. (Consider using the method instead.) /// The object that represents the asynchronous call. /// The underlying stream is closed or . public override void EndWrite(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); /// Asynchronously writes compressed bytes to the underlying Brotli stream from the specified byte array. /// The buffer that contains the data to compress. diff --git a/src/libraries/System.IO.Compression.Brotli/tests/System.IO.Compression.Brotli.Tests.csproj b/src/libraries/System.IO.Compression.Brotli/tests/System.IO.Compression.Brotli.Tests.csproj index 64fa67a..167caaa 100644 --- a/src/libraries/System.IO.Compression.Brotli/tests/System.IO.Compression.Brotli.Tests.csproj +++ b/src/libraries/System.IO.Compression.Brotli/tests/System.IO.Compression.Brotli.Tests.csproj @@ -24,8 +24,6 @@ Link="Common\System\IO\Compression\ZipTestHelper.cs" /> - diff --git a/src/libraries/System.IO.Compression/src/System.IO.Compression.csproj b/src/libraries/System.IO.Compression/src/System.IO.Compression.csproj index d05a1d7..798fccd 100644 --- a/src/libraries/System.IO.Compression/src/System.IO.Compression.csproj +++ b/src/libraries/System.IO.Compression/src/System.IO.Compression.csproj @@ -39,8 +39,6 @@ - diff --git a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/DeflateManagedStream.cs b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/DeflateManagedStream.cs index 96baf10..d161f74 100644 --- a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/DeflateManagedStream.cs +++ b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/DeflateManagedStream.cs @@ -152,10 +152,10 @@ namespace System.IO.Compression } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); public override int EndRead(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); private ValueTask ReadAsyncInternal(Memory buffer, CancellationToken cancellationToken) { diff --git a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs index 2afd554..18b6cb9 100644 --- a/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs +++ b/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs @@ -356,13 +356,13 @@ namespace System.IO.Compression throw new InvalidDataException(SR.TruncatedData); public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); public override int EndRead(IAsyncResult asyncResult) { EnsureDecompressionMode(); EnsureNotDisposed(); - return TaskToApm.End(asyncResult); + return TaskToAsyncResult.End(asyncResult); } public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) @@ -758,13 +758,13 @@ namespace System.IO.Compression } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); + TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); public override void EndWrite(IAsyncResult asyncResult) { EnsureCompressionMode(); EnsureNotDisposed(); - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); } public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) diff --git a/src/libraries/System.IO.Compression/src/System/IO/Compression/GZipStream.cs b/src/libraries/System.IO.Compression/src/System/IO/Compression/GZipStream.cs index f59e473..c4b743b 100644 --- a/src/libraries/System.IO.Compression/src/System/IO/Compression/GZipStream.cs +++ b/src/libraries/System.IO.Compression/src/System/IO/Compression/GZipStream.cs @@ -72,7 +72,7 @@ namespace System.IO.Compression } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); public override int EndRead(IAsyncResult asyncResult) => _deflateStream.EndRead(asyncResult); @@ -100,7 +100,7 @@ namespace System.IO.Compression } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); + TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); public override void EndWrite(IAsyncResult asyncResult) => _deflateStream.EndWrite(asyncResult); diff --git a/src/libraries/System.IO.Compression/tests/System.IO.Compression.Tests.csproj b/src/libraries/System.IO.Compression/tests/System.IO.Compression.Tests.csproj index 518be0f..7715606 100644 --- a/src/libraries/System.IO.Compression/tests/System.IO.Compression.Tests.csproj +++ b/src/libraries/System.IO.Compression/tests/System.IO.Compression.Tests.csproj @@ -39,7 +39,6 @@ - diff --git a/src/libraries/System.IO.Pipelines/src/System.IO.Pipelines.csproj b/src/libraries/System.IO.Pipelines/src/System.IO.Pipelines.csproj index 33678ec..21a7012 100644 --- a/src/libraries/System.IO.Pipelines/src/System.IO.Pipelines.csproj +++ b/src/libraries/System.IO.Pipelines/src/System.IO.Pipelines.csproj @@ -11,8 +11,6 @@ System.IO.Pipelines.PipeReader - @@ -57,6 +55,10 @@ System.IO.Pipelines.PipeReader + + + + diff --git a/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeReaderStream.cs b/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeReaderStream.cs index 19128cc..798defa 100644 --- a/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeReaderStream.cs +++ b/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeReaderStream.cs @@ -75,10 +75,10 @@ namespace System.IO.Pipelines public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); public sealed override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(ReadAsync(buffer, offset, count, default), callback, state); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, default), callback, state); public sealed override int EndRead(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { diff --git a/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeWriterStream.cs b/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeWriterStream.cs index dc9c34a..2dc15a5 100644 --- a/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeWriterStream.cs +++ b/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeWriterStream.cs @@ -61,10 +61,10 @@ namespace System.IO.Pipelines public override void SetLength(long value) => throw new NotSupportedException(); public sealed override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(WriteAsync(buffer, offset, count, default), callback, state); + TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, default), callback, state); public sealed override void EndWrite(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public override void Write(byte[] buffer, int offset, int count) => WriteAsync(buffer, offset, count).GetAwaiter().GetResult(); 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 8f50a32..4047a77 100644 --- a/src/libraries/System.IO.Pipes/src/System.IO.Pipes.csproj +++ b/src/libraries/System.IO.Pipes/src/System.IO.Pipes.csproj @@ -24,8 +24,6 @@ - diff --git a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.cs b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.cs index a0652e7..069ff0e 100644 --- a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.cs +++ b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.cs @@ -162,10 +162,10 @@ namespace System.IO.Pipes } public System.IAsyncResult BeginWaitForConnection(AsyncCallback? callback, object? state) => - TaskToApm.Begin(WaitForConnectionAsync(), callback, state); + TaskToAsyncResult.Begin(WaitForConnectionAsync(), callback, state); public void EndWaitForConnection(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); // Server can only connect from Disconnected state private void CheckConnectOperationsServer() diff --git a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs index ce0607a..bba2948 100644 --- a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs +++ b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs @@ -103,10 +103,10 @@ namespace System.IO.Pipes } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) - => TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); + => TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); public override int EndRead(IAsyncResult asyncResult) - => TaskToApm.End(asyncResult); + => TaskToAsyncResult.End(asyncResult); public override void Write(byte[] buffer, int offset, int count) { @@ -177,10 +177,10 @@ namespace System.IO.Pipes } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) - => TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state); + => TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state); public override void EndWrite(IAsyncResult asyncResult) - => TaskToApm.End(asyncResult); + => TaskToAsyncResult.End(asyncResult); internal static string GetPipePath(string serverName, string pipeName) { diff --git a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs index 229b604..aed30e5 100644 --- a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs +++ b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs @@ -109,7 +109,7 @@ namespace System.IO.Pipes public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) { if (_isAsync) - return TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); + return TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); else return base.BeginRead(buffer, offset, count, callback, state); } @@ -117,7 +117,7 @@ namespace System.IO.Pipes public override int EndRead(IAsyncResult asyncResult) { if (_isAsync) - return TaskToApm.End(asyncResult); + return TaskToAsyncResult.End(asyncResult); else return base.EndRead(asyncResult); } @@ -205,7 +205,7 @@ namespace System.IO.Pipes public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) { if (_isAsync) - return TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state); + return TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state); else return base.BeginWrite(buffer, offset, count, callback, state); } @@ -213,7 +213,7 @@ namespace System.IO.Pipes public override void EndWrite(IAsyncResult asyncResult) { if (_isAsync) - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); else base.EndWrite(asyncResult); } diff --git a/src/libraries/System.IO.Ports/src/System.IO.Ports.csproj b/src/libraries/System.IO.Ports/src/System.IO.Ports.csproj index 5797c2c5..958fd6b 100644 --- a/src/libraries/System.IO.Ports/src/System.IO.Ports.csproj +++ b/src/libraries/System.IO.Ports/src/System.IO.Ports.csproj @@ -138,8 +138,10 @@ System.IO.Ports.SerialPort Link="Common\Interop\Unix\Interop.IOErrors.cs" /> - + + + + diff --git a/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialStream.Unix.cs b/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialStream.Unix.cs index 2d22028..b207ac3 100644 --- a/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialStream.Unix.cs +++ b/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialStream.Unix.cs @@ -520,7 +520,7 @@ namespace System.IO.Ports public override IAsyncResult BeginRead(byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject) { - return TaskToApm.Begin(ReadAsync(array, offset, numBytes), userCallback, stateObject); + return TaskToAsyncResult.Begin(ReadAsync(array, offset, numBytes), userCallback, stateObject); } // Will wait `timeout` miliseconds or until reading or writing is possible @@ -576,7 +576,7 @@ namespace System.IO.Ports public override IAsyncResult BeginWrite(byte[] array, int offset, int count, AsyncCallback userCallback, object stateObject) { - return TaskToApm.Begin(WriteAsync(array, offset, count), userCallback, stateObject); + return TaskToAsyncResult.Begin(WriteAsync(array, offset, count), userCallback, stateObject); } public override void EndWrite(IAsyncResult asyncResult) @@ -586,7 +586,7 @@ namespace System.IO.Ports { try { - return TaskToApm.End(asyncResult); + return TaskToAsyncResult.End(asyncResult); } catch (OperationCanceledException) { diff --git a/src/libraries/System.IO/tests/System.IO.Tests.csproj b/src/libraries/System.IO/tests/System.IO.Tests.csproj index c0d66ac..c7286f4 100644 --- a/src/libraries/System.IO/tests/System.IO.Tests.csproj +++ b/src/libraries/System.IO/tests/System.IO.Tests.csproj @@ -61,7 +61,6 @@ - diff --git a/src/libraries/System.Memory.Data/src/System.Memory.Data.csproj b/src/libraries/System.Memory.Data/src/System.Memory.Data.csproj index 0ed88b1..639e4a3 100644 --- a/src/libraries/System.Memory.Data/src/System.Memory.Data.csproj +++ b/src/libraries/System.Memory.Data/src/System.Memory.Data.csproj @@ -12,7 +12,6 @@ System.BinaryData - @@ -20,6 +19,10 @@ System.BinaryData + + + + diff --git a/src/libraries/System.Net.Http.WinHttpHandler/src/System.Net.Http.WinHttpHandler.csproj b/src/libraries/System.Net.Http.WinHttpHandler/src/System.Net.Http.WinHttpHandler.csproj index 3921f51..e6dd2f5 100644 --- a/src/libraries/System.Net.Http.WinHttpHandler/src/System.Net.Http.WinHttpHandler.csproj +++ b/src/libraries/System.Net.Http.WinHttpHandler/src/System.Net.Http.WinHttpHandler.csproj @@ -79,8 +79,6 @@ System.Net.Http.WinHttpHandler Link="Common\System\Runtime\ExceptionServices\ExceptionStackTrace.cs" /> - @@ -112,6 +110,10 @@ System.Net.Http.WinHttpHandler Link="Common\DisableRuntimeMarshalling.cs" /> + + + + diff --git a/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpRequestStream.cs b/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpRequestStream.cs index c50f09b..ebbb43e 100644 --- a/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpRequestStream.cs +++ b/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpRequestStream.cs @@ -143,10 +143,10 @@ namespace System.Net.Http } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); + TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); public override void EndWrite(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public override long Seek(long offset, SeekOrigin origin) { diff --git a/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpResponseStream.cs b/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpResponseStream.cs index fd0bfee..dccbaea 100644 --- a/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpResponseStream.cs +++ b/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpResponseStream.cs @@ -210,10 +210,10 @@ namespace System.Net.Http } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); public override int EndRead(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); private async Task ReadAsyncCore(byte[] buffer, int offset, int count, CancellationToken token) { diff --git a/src/libraries/System.Net.Http.WinHttpHandler/tests/UnitTests/System.Net.Http.WinHttpHandler.Unit.Tests.csproj b/src/libraries/System.Net.Http.WinHttpHandler/tests/UnitTests/System.Net.Http.WinHttpHandler.Unit.Tests.csproj index 739f32d..791b003 100644 --- a/src/libraries/System.Net.Http.WinHttpHandler/tests/UnitTests/System.Net.Http.WinHttpHandler.Unit.Tests.csproj +++ b/src/libraries/System.Net.Http.WinHttpHandler/tests/UnitTests/System.Net.Http.WinHttpHandler.Unit.Tests.csproj @@ -57,8 +57,6 @@ Link="Common\System\Text\SimpleRegex.cs" /> - - - - diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpBaseStream.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpBaseStream.cs index 0bcc913..1d90cd6 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpBaseStream.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpBaseStream.cs @@ -13,16 +13,16 @@ namespace System.Net.Http public sealed override bool CanSeek => false; public sealed override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(ReadAsync(buffer, offset, count, default), callback, state); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, default), callback, state); public sealed override int EndRead(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public sealed override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(WriteAsync(buffer, offset, count, default), callback, state); + TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, default), callback, state); public sealed override void EndWrite(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public sealed override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs index 4fa0beb..13e4122 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs @@ -1031,10 +1031,10 @@ namespace System.Net.Http } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); + TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); public override void EndWrite(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public override void WriteByte(byte value) { diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs b/src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs index eb7893a..156abcc 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs @@ -533,10 +533,10 @@ namespace System.Net.Http ReadAsyncPrivate(buffer, cancellationToken); public override IAsyncResult BeginRead(byte[] array, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(ReadAsync(array, offset, count, CancellationToken.None), asyncCallback, asyncState); + TaskToAsyncResult.Begin(ReadAsync(array, offset, count, CancellationToken.None), asyncCallback, asyncState); public override int EndRead(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public async ValueTask ReadAsyncPrivate(Memory buffer, CancellationToken cancellationToken) { diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/System.Net.Http.Functional.Tests.csproj b/src/libraries/System.Net.Http/tests/FunctionalTests/System.Net.Http.Functional.Tests.csproj index 90c828e..f09fad6 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/System.Net.Http.Functional.Tests.csproj +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/System.Net.Http.Functional.Tests.csproj @@ -45,8 +45,6 @@ Link="Common\Interop\Unix\Interop.Libraries.cs" /> - - - diff --git a/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj b/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj index 127119d..254be8e 100644 --- a/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj +++ b/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj @@ -19,8 +19,6 @@ Link="Common\System\Net\Logging\NetEventSource.Common.cs" /> - diff --git a/src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs b/src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs index cf049da..0ee39a3 100644 --- a/src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs +++ b/src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs @@ -164,16 +164,16 @@ namespace System.Net } public static IAsyncResult BeginGetHostEntry(IPAddress address, AsyncCallback? requestCallback, object? stateObject) => - TaskToApm.Begin(GetHostEntryAsync(address), requestCallback, stateObject); + TaskToAsyncResult.Begin(GetHostEntryAsync(address), requestCallback, stateObject); public static IAsyncResult BeginGetHostEntry(string hostNameOrAddress, AsyncCallback? requestCallback, object? stateObject) => - TaskToApm.Begin(GetHostEntryAsync(hostNameOrAddress), requestCallback, stateObject); + TaskToAsyncResult.Begin(GetHostEntryAsync(hostNameOrAddress), requestCallback, stateObject); public static IPHostEntry EndGetHostEntry(IAsyncResult asyncResult) { ArgumentNullException.ThrowIfNull(asyncResult); - return TaskToApm.End(asyncResult); + return TaskToAsyncResult.End(asyncResult); } public static IPAddress[] GetHostAddresses(string hostNameOrAddress) @@ -241,13 +241,13 @@ namespace System.Net (Task)GetHostEntryOrAddressesCoreAsync(hostNameOrAddress, justReturnParsedIp: true, throwOnIIPAny: true, justAddresses: true, family, cancellationToken); public static IAsyncResult BeginGetHostAddresses(string hostNameOrAddress, AsyncCallback? requestCallback, object? state) => - TaskToApm.Begin(GetHostAddressesAsync(hostNameOrAddress), requestCallback, state); + TaskToAsyncResult.Begin(GetHostAddressesAsync(hostNameOrAddress), requestCallback, state); public static IPAddress[] EndGetHostAddresses(IAsyncResult asyncResult) { ArgumentNullException.ThrowIfNull(asyncResult); - return TaskToApm.End(asyncResult); + return TaskToAsyncResult.End(asyncResult); } [Obsolete("GetHostByName has been deprecated. Use GetHostEntry instead.")] @@ -265,14 +265,14 @@ namespace System.Net [Obsolete("BeginGetHostByName has been deprecated. Use BeginGetHostEntry instead.")] public static IAsyncResult BeginGetHostByName(string hostName, AsyncCallback? requestCallback, object? stateObject) => - TaskToApm.Begin(GetHostEntryCoreAsync(hostName, justReturnParsedIp: true, throwOnIIPAny: true, AddressFamily.Unspecified, CancellationToken.None), requestCallback, stateObject); + TaskToAsyncResult.Begin(GetHostEntryCoreAsync(hostName, justReturnParsedIp: true, throwOnIIPAny: true, AddressFamily.Unspecified, CancellationToken.None), requestCallback, stateObject); [Obsolete("EndGetHostByName has been deprecated. Use EndGetHostEntry instead.")] public static IPHostEntry EndGetHostByName(IAsyncResult asyncResult) { ArgumentNullException.ThrowIfNull(asyncResult); - return TaskToApm.End(asyncResult); + return TaskToAsyncResult.End(asyncResult); } [Obsolete("GetHostByAddress has been deprecated. Use GetHostEntry instead.")] @@ -328,7 +328,7 @@ namespace System.Net [Obsolete("BeginResolve has been deprecated. Use BeginGetHostEntry instead.")] public static IAsyncResult BeginResolve(string hostName, AsyncCallback? requestCallback, object? stateObject) => - TaskToApm.Begin(GetHostEntryCoreAsync(hostName, justReturnParsedIp: false, throwOnIIPAny: false, AddressFamily.Unspecified, CancellationToken.None), requestCallback, stateObject); + TaskToAsyncResult.Begin(GetHostEntryCoreAsync(hostName, justReturnParsedIp: false, throwOnIIPAny: false, AddressFamily.Unspecified, CancellationToken.None), requestCallback, stateObject); [Obsolete("EndResolve has been deprecated. Use EndGetHostEntry instead.")] public static IPHostEntry EndResolve(IAsyncResult asyncResult) @@ -337,16 +337,11 @@ namespace System.Net try { - ipHostEntry = TaskToApm.End(asyncResult); + ipHostEntry = TaskToAsyncResult.End(asyncResult); } catch (SocketException ex) { - object? asyncState = asyncResult switch - { - Task t => t.AsyncState, - TaskToApm.TaskAsyncResult twar => twar._task.AsyncState, - _ => null - }; + object? asyncState = TaskToAsyncResult.Unwrap(asyncResult).AsyncState; IPAddress? address = asyncState switch { diff --git a/src/libraries/System.Net.NetworkInformation/src/System.Net.NetworkInformation.csproj b/src/libraries/System.Net.NetworkInformation/src/System.Net.NetworkInformation.csproj index e8621ab..c9b2bbb 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System.Net.NetworkInformation.csproj +++ b/src/libraries/System.Net.NetworkInformation/src/System.Net.NetworkInformation.csproj @@ -1,4 +1,4 @@ - + true $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-android;$(NetCoreAppCurrent)-osx;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-tvos;$(NetCoreAppCurrent)-freebsd;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-solaris;$(NetCoreAppCurrent) @@ -51,7 +51,6 @@ - diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPGlobalProperties.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPGlobalProperties.cs index b460cef..3090e47 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPGlobalProperties.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemIPGlobalProperties.cs @@ -368,10 +368,10 @@ namespace System.Net.NetworkInformation } public override IAsyncResult BeginGetUnicastAddresses(AsyncCallback? callback, object? state) => - TaskToApm.Begin(GetUnicastAddressesAsync(), callback, state); + TaskToAsyncResult.Begin(GetUnicastAddressesAsync(), callback, state); public override UnicastIPAddressInformationCollection EndGetUnicastAddresses(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public override UnicastIPAddressInformationCollection GetUnicastAddresses() => GetUnicastAddressesAsync().GetAwaiter().GetResult(); diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixIPGlobalProperties.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixIPGlobalProperties.cs index 9de6b8b..f26aa57 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixIPGlobalProperties.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixIPGlobalProperties.cs @@ -43,12 +43,12 @@ namespace System.Net.NetworkInformation public override IAsyncResult BeginGetUnicastAddresses(AsyncCallback? callback, object? state) { Task t = GetUnicastAddressesAsync(); - return TaskToApm.Begin(t, callback, state); + return TaskToAsyncResult.Begin(t, callback, state); } public override UnicastIPAddressInformationCollection EndGetUnicastAddresses(IAsyncResult asyncResult) { - return TaskToApm.End(asyncResult); + return TaskToAsyncResult.End(asyncResult); } public sealed override Task GetUnicastAddressesAsync() diff --git a/src/libraries/System.Net.Quic/src/System.Net.Quic.csproj b/src/libraries/System.Net.Quic/src/System.Net.Quic.csproj index b7b6b7e..ebbed23 100644 --- a/src/libraries/System.Net.Quic/src/System.Net.Quic.csproj +++ b/src/libraries/System.Net.Quic/src/System.Net.Quic.csproj @@ -22,7 +22,6 @@ - diff --git a/src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.Stream.cs b/src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.Stream.cs index 8bf9799..dd6fa5a 100644 --- a/src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.Stream.cs +++ b/src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.Stream.cs @@ -90,11 +90,11 @@ public partial class QuicStream : Stream /// public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) - => TaskToApm.Begin(ReadAsync(buffer, offset, count, default), callback, state); + => TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, default), callback, state); /// public override int EndRead(IAsyncResult asyncResult) - => TaskToApm.End(asyncResult); + => TaskToAsyncResult.End(asyncResult); /// public override int Read(byte[] buffer, int offset, int count) @@ -153,11 +153,11 @@ public partial class QuicStream : Stream /// public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) - => TaskToApm.Begin(WriteAsync(buffer, offset, count, default), callback, state); + => TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, default), callback, state); /// public override void EndWrite(IAsyncResult asyncResult) - => TaskToApm.End(asyncResult); + => TaskToAsyncResult.End(asyncResult); /// public override void Write(byte[] buffer, int offset, int count) diff --git a/src/libraries/System.Net.Quic/tests/FunctionalTests/System.Net.Quic.Functional.Tests.csproj b/src/libraries/System.Net.Quic/tests/FunctionalTests/System.Net.Quic.Functional.Tests.csproj index c088044..2b31dee 100644 --- a/src/libraries/System.Net.Quic/tests/FunctionalTests/System.Net.Quic.Functional.Tests.csproj +++ b/src/libraries/System.Net.Quic/tests/FunctionalTests/System.Net.Quic.Functional.Tests.csproj @@ -16,7 +16,6 @@ - diff --git a/src/libraries/System.Net.Requests/src/System.Net.Requests.csproj b/src/libraries/System.Net.Requests/src/System.Net.Requests.csproj index 051c2bc..505dc8a 100644 --- a/src/libraries/System.Net.Requests/src/System.Net.Requests.csproj +++ b/src/libraries/System.Net.Requests/src/System.Net.Requests.csproj @@ -76,8 +76,6 @@ Link="Common\System\Net\SecurityProtocol.cs" /> - t = Task.Factory.StartNew(s => ((FileWebRequest)s!).CreateWriteStream(), this, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); - return TaskToApm.Begin(t, callback, state); + return TaskToAsyncResult.Begin(t, callback, state); } public override Task GetRequestStreamAsync() @@ -219,7 +219,7 @@ namespace System.Net CheckAndMarkAsyncGetResponsePending(); Task t = Task.Factory.StartNew(s => ((FileWebRequest)s!).CreateResponse(), this, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); - return TaskToApm.Begin(t, callback, state); + return TaskToAsyncResult.Begin(t, callback, state); } public override Task GetResponseAsync() @@ -236,14 +236,14 @@ namespace System.Net public override Stream EndGetRequestStream(IAsyncResult asyncResult) { - Stream stream = TaskToApm.End(asyncResult); + Stream stream = TaskToAsyncResult.End(asyncResult); _writePending = false; return stream; } public override WebResponse EndGetResponse(IAsyncResult asyncResult) { - WebResponse response = TaskToApm.End(asyncResult); + WebResponse response = TaskToAsyncResult.End(asyncResult); _readPending = false; return response; } diff --git a/src/libraries/System.Net.Security/src/System.Net.Security.csproj b/src/libraries/System.Net.Security/src/System.Net.Security.csproj index bc44d7a..b594f3f 100644 --- a/src/libraries/System.Net.Security/src/System.Net.Security.csproj +++ b/src/libraries/System.Net.Security/src/System.Net.Security.csproj @@ -90,8 +90,6 @@ - - TaskToApm.Begin(AuthenticateAsClientAsync(credential, binding, targetName, requiredProtectionLevel, allowedImpersonationLevel), asyncCallback, asyncState); + TaskToAsyncResult.Begin(AuthenticateAsClientAsync(credential, binding, targetName, requiredProtectionLevel, allowedImpersonationLevel), asyncCallback, asyncState); - public virtual void EndAuthenticateAsClient(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + public virtual void EndAuthenticateAsClient(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); public virtual void AuthenticateAsServer() => AuthenticateAsServer((NetworkCredential)CredentialCache.DefaultCredentials, policy: null, ProtectionLevel.EncryptAndSign, TokenImpersonationLevel.Identification); @@ -149,9 +149,9 @@ namespace System.Net.Security public virtual IAsyncResult BeginAuthenticateAsServer( NetworkCredential credential, ExtendedProtectionPolicy? policy, ProtectionLevel requiredProtectionLevel, TokenImpersonationLevel requiredImpersonationLevel, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(AuthenticateAsServerAsync(credential, policy, requiredProtectionLevel, requiredImpersonationLevel), asyncCallback, asyncState); + TaskToAsyncResult.Begin(AuthenticateAsServerAsync(credential, policy, requiredProtectionLevel, requiredImpersonationLevel), asyncCallback, asyncState); - public virtual void EndAuthenticateAsServer(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + public virtual void EndAuthenticateAsServer(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); public virtual void AuthenticateAsClient() => AuthenticateAsClient((NetworkCredential)CredentialCache.DefaultCredentials, binding: null, string.Empty, ProtectionLevel.EncryptAndSign, TokenImpersonationLevel.Identification); @@ -518,16 +518,16 @@ namespace System.Net.Security } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(ReadAsync(buffer, offset, count), asyncCallback, asyncState); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), asyncCallback, asyncState); public override int EndRead(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(WriteAsync(buffer, offset, count), asyncCallback, asyncState); + TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count), asyncCallback, asyncState); public override void EndWrite(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); private void ThrowIfExceptional() { diff --git a/src/libraries/System.Net.Security/src/System/Net/Security/SslStream.cs b/src/libraries/System.Net.Security/src/System/Net/Security/SslStream.cs index a086fc9..9f4ac13 100644 --- a/src/libraries/System.Net.Security/src/System/Net/Security/SslStream.cs +++ b/src/libraries/System.Net.Security/src/System/Net/Security/SslStream.cs @@ -248,9 +248,9 @@ namespace System.Net.Security } internal IAsyncResult BeginAuthenticateAsClient(SslClientAuthenticationOptions sslClientAuthenticationOptions, CancellationToken cancellationToken, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(AuthenticateAsClientAsync(sslClientAuthenticationOptions, cancellationToken)!, asyncCallback, asyncState); + TaskToAsyncResult.Begin(AuthenticateAsClientAsync(sslClientAuthenticationOptions, cancellationToken)!, asyncCallback, asyncState); - public virtual void EndAuthenticateAsClient(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + public virtual void EndAuthenticateAsClient(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); // // Server side auth. @@ -287,13 +287,13 @@ namespace System.Net.Security } private IAsyncResult BeginAuthenticateAsServer(SslServerAuthenticationOptions sslServerAuthenticationOptions, CancellationToken cancellationToken, AsyncCallback? asyncCallback, object? asyncState) => - TaskToApm.Begin(AuthenticateAsServerAsync(sslServerAuthenticationOptions, cancellationToken)!, asyncCallback, asyncState); + TaskToAsyncResult.Begin(AuthenticateAsServerAsync(sslServerAuthenticationOptions, cancellationToken)!, asyncCallback, asyncState); - public virtual void EndAuthenticateAsServer(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + public virtual void EndAuthenticateAsServer(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); - internal IAsyncResult BeginShutdown(AsyncCallback? asyncCallback, object? asyncState) => TaskToApm.Begin(ShutdownAsync(), asyncCallback, asyncState); + internal IAsyncResult BeginShutdown(AsyncCallback? asyncCallback, object? asyncState) => TaskToAsyncResult.Begin(ShutdownAsync(), asyncCallback, asyncState); - internal static void EndShutdown(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + internal static void EndShutdown(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); public TransportContext TransportContext => new SslStreamContext(this); @@ -767,25 +767,25 @@ namespace System.Net.Security public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) { ThrowIfExceptionalOrNotAuthenticated(); - return TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); + return TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); } public override int EndRead(IAsyncResult asyncResult) { ThrowIfExceptionalOrNotAuthenticated(); - return TaskToApm.End(asyncResult); + return TaskToAsyncResult.End(asyncResult); } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) { ThrowIfExceptionalOrNotAuthenticated(); - return TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); + return TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState); } public override void EndWrite(IAsyncResult asyncResult) { ThrowIfExceptionalOrNotAuthenticated(); - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); } public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) diff --git a/src/libraries/System.Net.Security/tests/EnterpriseTests/System.Net.Security.Enterprise.Tests.csproj b/src/libraries/System.Net.Security/tests/EnterpriseTests/System.Net.Security.Enterprise.Tests.csproj index 809f2f3..3714c1b 100644 --- a/src/libraries/System.Net.Security/tests/EnterpriseTests/System.Net.Security.Enterprise.Tests.csproj +++ b/src/libraries/System.Net.Security/tests/EnterpriseTests/System.Net.Security.Enterprise.Tests.csproj @@ -19,7 +19,5 @@ Link="Common\System\Net\EnterpriseTests\EnterpriseTestConfiguration.cs" /> - - \ No newline at end of file + diff --git a/src/libraries/System.Net.Security/tests/FunctionalTests/System.Net.Security.Tests.csproj b/src/libraries/System.Net.Security/tests/FunctionalTests/System.Net.Security.Tests.csproj index 56e1f32..0db9e97 100644 --- a/src/libraries/System.Net.Security/tests/FunctionalTests/System.Net.Security.Tests.csproj +++ b/src/libraries/System.Net.Security/tests/FunctionalTests/System.Net.Security.Tests.csproj @@ -91,8 +91,6 @@ Link="CommonTest\System\Security\Cryptography\X509Certificates\RevocationResponder.cs" /> - - diff --git a/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj b/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj index e50db90..457d891 100644 --- a/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj +++ b/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj @@ -73,8 +73,6 @@ Link="Common\System\Net\TcpValidationHelpers.cs" /> - diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs index 8aa270f..6c10d2c 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs @@ -2236,21 +2236,21 @@ namespace System.Net.Sockets } public IAsyncResult BeginConnect(EndPoint remoteEP, AsyncCallback? callback, object? state) => - TaskToApm.Begin(ConnectAsync(remoteEP), callback, state); + TaskToAsyncResult.Begin(ConnectAsync(remoteEP), callback, state); public IAsyncResult BeginConnect(string host, int port, AsyncCallback? requestCallback, object? state) => - TaskToApm.Begin(ConnectAsync(host, port), requestCallback, state); + TaskToAsyncResult.Begin(ConnectAsync(host, port), requestCallback, state); public IAsyncResult BeginConnect(IPAddress address, int port, AsyncCallback? requestCallback, object? state) => - TaskToApm.Begin(ConnectAsync(address, port), requestCallback, state); + TaskToAsyncResult.Begin(ConnectAsync(address, port), requestCallback, state); public IAsyncResult BeginConnect(IPAddress[] addresses, int port, AsyncCallback? requestCallback, object? state) => - TaskToApm.Begin(ConnectAsync(addresses, port), requestCallback, state); + TaskToAsyncResult.Begin(ConnectAsync(addresses, port), requestCallback, state); - public void EndConnect(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + public void EndConnect(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); public IAsyncResult BeginDisconnect(bool reuseSocket, AsyncCallback? callback, object? state) => - TaskToApm.Begin(DisconnectAsync(reuseSocket).AsTask(), callback, state); + TaskToAsyncResult.Begin(DisconnectAsync(reuseSocket).AsTask(), callback, state); public void Disconnect(bool reuseSocket) { @@ -2273,14 +2273,14 @@ namespace System.Net.Sockets _localEndPoint = null; } - public void EndDisconnect(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + public void EndDisconnect(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); public IAsyncResult BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback? callback, object? state) { ThrowIfDisposed(); ValidateBufferArguments(buffer, offset, size); - return TaskToApm.Begin(SendAsync(new ReadOnlyMemory(buffer, offset, size), socketFlags, default).AsTask(), callback, state); + return TaskToAsyncResult.Begin(SendAsync(new ReadOnlyMemory(buffer, offset, size), socketFlags, default).AsTask(), callback, state); } public IAsyncResult? BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback? callback, object? state) @@ -2296,14 +2296,14 @@ namespace System.Net.Sockets } errorCode = SocketError.Success; - return TaskToApm.Begin(t, callback, state); + return TaskToAsyncResult.Begin(t, callback, state); } public IAsyncResult BeginSend(IList> buffers, SocketFlags socketFlags, AsyncCallback? callback, object? state) { ThrowIfDisposed(); - return TaskToApm.Begin(SendAsync(buffers, socketFlags), callback, state); + return TaskToAsyncResult.Begin(SendAsync(buffers, socketFlags), callback, state); } public IAsyncResult? BeginSend(IList> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback? callback, object? state) @@ -2318,10 +2318,10 @@ namespace System.Net.Sockets } errorCode = SocketError.Success; - return TaskToApm.Begin(t, callback, state); + return TaskToAsyncResult.Begin(t, callback, state); } - public int EndSend(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + public int EndSend(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); public int EndSend(IAsyncResult asyncResult, out SocketError errorCode) => EndSendReceive(asyncResult, out errorCode); @@ -2342,10 +2342,10 @@ namespace System.Net.Sockets if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"::DoBeginSendFile() SRC:{LocalEndPoint} DST:{RemoteEndPoint} fileName:{fileName}"); - return TaskToApm.Begin(SendFileAsync(fileName, preBuffer, postBuffer, flags).AsTask(), callback, state); + return TaskToAsyncResult.Begin(SendFileAsync(fileName, preBuffer, postBuffer, flags).AsTask(), callback, state); } - public void EndSendFile(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + public void EndSendFile(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); public IAsyncResult BeginSendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP, AsyncCallback? callback, object? state) { @@ -2354,16 +2354,16 @@ namespace System.Net.Sockets ArgumentNullException.ThrowIfNull(remoteEP); Task t = SendToAsync(buffer.AsMemory(offset, size), socketFlags, remoteEP).AsTask(); - return TaskToApm.Begin(t, callback, state); + return TaskToAsyncResult.Begin(t, callback, state); } - public int EndSendTo(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + public int EndSendTo(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); public IAsyncResult BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback? callback, object? state) { ThrowIfDisposed(); ValidateBufferArguments(buffer, offset, size); - return TaskToApm.Begin(ReceiveAsync(new ArraySegment(buffer, offset, size), socketFlags, fromNetworkStream: false, default).AsTask(), callback, state); + return TaskToAsyncResult.Begin(ReceiveAsync(new ArraySegment(buffer, offset, size), socketFlags, fromNetworkStream: false, default).AsTask(), callback, state); } public IAsyncResult? BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback? callback, object? state) @@ -2379,13 +2379,13 @@ namespace System.Net.Sockets } errorCode = SocketError.Success; - return TaskToApm.Begin(t, callback, state); + return TaskToAsyncResult.Begin(t, callback, state); } public IAsyncResult BeginReceive(IList> buffers, SocketFlags socketFlags, AsyncCallback? callback, object? state) { ThrowIfDisposed(); - return TaskToApm.Begin(ReceiveAsync(buffers, socketFlags), callback, state); + return TaskToAsyncResult.Begin(ReceiveAsync(buffers, socketFlags), callback, state); } public IAsyncResult? BeginReceive(IList> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback? callback, object? state) @@ -2400,21 +2400,17 @@ namespace System.Net.Sockets } errorCode = SocketError.Success; - return TaskToApm.Begin(t, callback, state); + return TaskToAsyncResult.Begin(t, callback, state); } - public int EndReceive(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + public int EndReceive(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); public int EndReceive(IAsyncResult asyncResult, out SocketError errorCode) => EndSendReceive(asyncResult, out errorCode); private static int EndSendReceive(IAsyncResult asyncResult, out SocketError errorCode) { - if (TaskToApm.GetTask(asyncResult) is not Task ti) - { - ArgumentNullException.ThrowIfNull(asyncResult); - throw new ArgumentException(null, nameof(asyncResult)); - } + Task ti = TaskToAsyncResult.Unwrap(asyncResult); if (!ti.IsCompleted) { @@ -2448,7 +2444,7 @@ namespace System.Net.Sockets EndPoint resultEp = t.Result.RemoteEndPoint; if (!remoteEP.Equals(resultEp)) remoteEP = resultEp; } - IAsyncResult asyncResult = TaskToApm.Begin(t, callback, state); + IAsyncResult asyncResult = TaskToAsyncResult.Begin(t, callback, state); if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"size:{size} returning AsyncResult:{asyncResult}"); return asyncResult; } @@ -2461,7 +2457,7 @@ namespace System.Net.Sockets throw new ArgumentException(SR.Format(SR.net_InvalidEndPointAddressFamily, endPoint.AddressFamily, _addressFamily), nameof(endPoint)); } - SocketReceiveMessageFromResult result = TaskToApm.End(asyncResult); + SocketReceiveMessageFromResult result = TaskToAsyncResult.End(asyncResult); if (!endPoint.Equals(result.RemoteEndPoint)) { endPoint = result.RemoteEndPoint; @@ -2486,7 +2482,7 @@ namespace System.Net.Sockets if (!remoteEP.Equals(resultEp)) remoteEP = resultEp; } - return TaskToApm.Begin(t, callback, state); + return TaskToAsyncResult.Begin(t, callback, state); } public int EndReceiveFrom(IAsyncResult asyncResult, ref EndPoint endPoint) @@ -2497,7 +2493,7 @@ namespace System.Net.Sockets throw new ArgumentException(SR.Format(SR.net_InvalidEndPointAddressFamily, endPoint.AddressFamily, _addressFamily), nameof(endPoint)); } - SocketReceiveFromResult result = TaskToApm.End(asyncResult); + SocketReceiveFromResult result = TaskToAsyncResult.End(asyncResult); if (!endPoint.Equals(result.RemoteEndPoint)) { endPoint = result.RemoteEndPoint; @@ -2506,9 +2502,9 @@ namespace System.Net.Sockets } public IAsyncResult BeginAccept(AsyncCallback? callback, object? state) => - TaskToApm.Begin(AcceptAsync(), callback, state); + TaskToAsyncResult.Begin(AcceptAsync(), callback, state); - public Socket EndAccept(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); + public Socket EndAccept(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult); // This method provides support for legacy BeginAccept methods that take a "receiveSize" argument and // allow data to be received as part of the accept operation. @@ -2547,7 +2543,7 @@ namespace System.Net.Sockets BeginAccept(acceptSocket: null, receiveSize, callback, state); public IAsyncResult BeginAccept(Socket? acceptSocket, int receiveSize, AsyncCallback? callback, object? state) => - TaskToApm.Begin(AcceptAndReceiveHelperAsync(acceptSocket, receiveSize), callback, state); + TaskToAsyncResult.Begin(AcceptAndReceiveHelperAsync(acceptSocket, receiveSize), callback, state); public Socket EndAccept(out byte[] buffer, IAsyncResult asyncResult) { @@ -2560,7 +2556,7 @@ namespace System.Net.Sockets public Socket EndAccept(out byte[] buffer, out int bytesTransferred, IAsyncResult asyncResult) { Socket s; - (s, buffer, bytesTransferred) = TaskToApm.End<(Socket, byte[], int)>(asyncResult); + (s, buffer, bytesTransferred) = TaskToAsyncResult.End<(Socket, byte[], int)>(asyncResult); return s; } diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPListener.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPListener.cs index 6ee9ff7..4cb923f 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPListener.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPListener.cs @@ -201,13 +201,13 @@ namespace System.Net.Sockets } public IAsyncResult BeginAcceptSocket(AsyncCallback? callback, object? state) => - TaskToApm.Begin(AcceptSocketAsync(), callback, state); + TaskToAsyncResult.Begin(AcceptSocketAsync(), callback, state); public Socket EndAcceptSocket(IAsyncResult asyncResult) => EndAcceptCore(asyncResult); public IAsyncResult BeginAcceptTcpClient(AsyncCallback? callback, object? state) => - TaskToApm.Begin(AcceptTcpClientAsync(), callback, state); + TaskToAsyncResult.Begin(AcceptTcpClientAsync(), callback, state); public TcpClient EndAcceptTcpClient(IAsyncResult asyncResult) => EndAcceptCore(asyncResult); @@ -283,7 +283,7 @@ namespace System.Net.Sockets { try { - return TaskToApm.End(asyncResult); + return TaskToAsyncResult.End(asyncResult); } catch (SocketException) when (!_active) { diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/System.Net.Sockets.Tests.csproj b/src/libraries/System.Net.Sockets/tests/FunctionalTests/System.Net.Sockets.Tests.csproj index ad1851d..a1367f4 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/System.Net.Sockets.Tests.csproj +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/System.Net.Sockets.Tests.csproj @@ -92,8 +92,6 @@ Link="Common\System\Threading\Tasks\TaskTimeoutExtensions.cs" /> - Common\System\Threading\OpenExistingResult.cs - - Common\System\Threading\Tasks\TaskToApm.cs + + Common\System\Threading\Tasks\TaskToAsyncResult.cs diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/BufferedStream.cs b/src/libraries/System.Private.CoreLib/src/System/IO/BufferedStream.cs index aabdf3c..40abcbd 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/BufferedStream.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/BufferedStream.cs @@ -725,10 +725,10 @@ namespace System.IO } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); public override int EndRead(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public override int ReadByte() { @@ -1138,10 +1138,10 @@ namespace System.IO } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state); + TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state); public override void EndWrite(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public override void WriteByte(byte value) { diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs index 9e8f37f..a221e80 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs @@ -456,10 +456,10 @@ namespace System.IO.Strategies } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) - => TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); + => TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); public override int EndRead(IAsyncResult asyncResult) - => TaskToApm.End(asyncResult); + => TaskToAsyncResult.End(asyncResult); public override void Write(byte[] buffer, int offset, int count) { @@ -739,10 +739,10 @@ namespace System.IO.Strategies } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) - => TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state); + => TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state); public override void EndWrite(IAsyncResult asyncResult) - => TaskToApm.End(asyncResult); + => TaskToAsyncResult.End(asyncResult); public override void SetLength(long value) { diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/OSFileStreamStrategy.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/OSFileStreamStrategy.cs index 83746a5..febe3d1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/OSFileStreamStrategy.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/OSFileStreamStrategy.cs @@ -246,10 +246,10 @@ namespace System.IO.Strategies } public sealed override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(WriteAsync(buffer, offset, count), callback, state); + TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count), callback, state); public sealed override void EndWrite(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public sealed override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => WriteAsync(new ReadOnlyMemory(buffer, offset, count), cancellationToken).AsTask(); @@ -261,10 +261,10 @@ namespace System.IO.Strategies } public sealed override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), callback, state); public sealed override int EndRead(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public sealed override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => ReadAsync(new Memory(buffer, offset, count), cancellationToken).AsTask(); diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs index f3b723b..8c1c9bb 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs @@ -1041,16 +1041,16 @@ namespace System.IO Task.CompletedTask; public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(Task.s_defaultResultTask, callback, state); + TaskToAsyncResult.Begin(Task.s_defaultResultTask, callback, state); public override int EndRead(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(Task.CompletedTask, callback, state); + TaskToAsyncResult.Begin(Task.CompletedTask, callback, state); public override void EndWrite(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public override int Read(byte[] buffer, int offset, int count) => 0; diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/TranscodingStream.cs b/src/libraries/System.Private.CoreLib/src/System/Text/TranscodingStream.cs index 65be587..198e999 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/TranscodingStream.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/TranscodingStream.cs @@ -83,10 +83,10 @@ namespace System.Text } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) - => TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); + => TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) - => TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state); + => TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state); protected override void Dispose(bool disposing) { @@ -162,10 +162,10 @@ namespace System.Text } public override int EndRead(IAsyncResult asyncResult) - => TaskToApm.End(asyncResult); + => TaskToAsyncResult.End(asyncResult); public override void EndWrite(IAsyncResult asyncResult) - => TaskToApm.End(asyncResult); + => TaskToAsyncResult.End(asyncResult); #pragma warning disable CS3016 // Arrays as attribute arguments is not CLS-compliant #pragma warning disable CS8774 // Member must have a non-null value when exiting. diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 8388419..2de2139 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -4,8 +4,6 @@ // Changes to this file must follow the https://aka.ms/api-review process. // ------------------------------------------------------------------------------ -using System.Text; - namespace Microsoft.Win32.SafeHandles { public abstract partial class CriticalHandleMinusOneIsInvalid : System.Runtime.InteropServices.CriticalHandle @@ -14950,6 +14948,14 @@ namespace System.Threading.Tasks public new System.Threading.Tasks.Task WaitAsync(System.TimeSpan timeout) { throw null; } public new System.Threading.Tasks.Task WaitAsync(System.TimeSpan timeout, System.Threading.CancellationToken cancellationToken) { throw null; } } + public static partial class TaskToAsyncResult + { + public static System.IAsyncResult Begin(System.Threading.Tasks.Task task, System.AsyncCallback? callback, object? state) { throw null; } + public static void End(System.IAsyncResult asyncResult) { throw null; } + public static TResult End(System.IAsyncResult asyncResult) { throw null; } + public static System.Threading.Tasks.Task Unwrap(System.IAsyncResult asyncResult) { throw null; } + public static System.Threading.Tasks.Task Unwrap(System.IAsyncResult asyncResult) { throw null; } + } public partial class UnobservedTaskExceptionEventArgs : System.EventArgs { public UnobservedTaskExceptionEventArgs(System.AggregateException exception) { } diff --git a/src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj b/src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj index af007a2..197dba5 100644 --- a/src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj +++ b/src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj @@ -1,4 +1,4 @@ - + true $(DefineConstants);INTERNAL_ASYMMETRIC_IMPLEMENTATIONS @@ -269,8 +269,6 @@ Link="Common\System\Security\Cryptography\Utf8DataEncoding.cs" /> - diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoStream.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoStream.cs index c1cc9a9..f85b396 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoStream.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoStream.cs @@ -249,10 +249,10 @@ namespace System.Security.Cryptography } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); + TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); public override int EndRead(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public override int ReadByte() { @@ -488,10 +488,10 @@ namespace System.Security.Cryptography } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => - TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state); + TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state); public override void EndWrite(IAsyncResult asyncResult) => - TaskToApm.End(asyncResult); + TaskToAsyncResult.End(asyncResult); public override void Write(byte[] buffer, int offset, int count) { diff --git a/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj b/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj index bfbb590..86e3d51 100644 --- a/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj +++ b/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj @@ -215,8 +215,6 @@ Link="CommonTest\System\Security\Cryptography\CryptoUtils.cs" /> - diff --git a/src/libraries/System.Text.Encoding/tests/System.Text.Encoding.Tests.csproj b/src/libraries/System.Text.Encoding/tests/System.Text.Encoding.Tests.csproj index 305e53a..a0c5f7f 100644 --- a/src/libraries/System.Text.Encoding/tests/System.Text.Encoding.Tests.csproj +++ b/src/libraries/System.Text.Encoding/tests/System.Text.Encoding.Tests.csproj @@ -86,7 +86,6 @@ - @@ -99,4 +98,4 @@ - \ No newline at end of file + diff --git a/src/libraries/System.Threading.Tasks/tests/System.Threading.Tasks.Tests.csproj b/src/libraries/System.Threading.Tasks/tests/System.Threading.Tasks.Tests.csproj index 8bebfb2..468c31b 100644 --- a/src/libraries/System.Threading.Tasks/tests/System.Threading.Tasks.Tests.csproj +++ b/src/libraries/System.Threading.Tasks/tests/System.Threading.Tasks.Tests.csproj @@ -16,6 +16,7 @@ + diff --git a/src/libraries/System.Threading.Tasks/tests/TaskToAsyncResultTests.cs b/src/libraries/System.Threading.Tasks/tests/TaskToAsyncResultTests.cs new file mode 100644 index 0000000..57d703f --- /dev/null +++ b/src/libraries/System.Threading.Tasks/tests/TaskToAsyncResultTests.cs @@ -0,0 +1,164 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Xunit; +using System.Threading; +using System.Threading.Tasks; + +namespace System.Threading.Tasks.Tests +{ + public class TaskToAsyncResultTests + { + [Fact] + public void InvalidArguments_ThrowExceptions() + { + AssertExtensions.Throws("task", () => TaskToAsyncResult.Begin(null, null, null)); + AssertExtensions.Throws("task", () => TaskToAsyncResult.Begin(null, iar => { }, "test")); + + AssertExtensions.Throws("asyncResult", () => TaskToAsyncResult.End(null)); + AssertExtensions.Throws("asyncResult", () => TaskToAsyncResult.End(null)); + + AssertExtensions.Throws("asyncResult", () => TaskToAsyncResult.End(new NonTaskIAsyncResult())); + AssertExtensions.Throws("asyncResult", () => TaskToAsyncResult.End(new NonTaskIAsyncResult())); + AssertExtensions.Throws("asyncResult", () => TaskToAsyncResult.End(Task.FromResult((long)42))); + + AssertExtensions.Throws("asyncResult", () => TaskToAsyncResult.Unwrap(new NonTaskIAsyncResult())); + AssertExtensions.Throws("asyncResult", () => TaskToAsyncResult.Unwrap(new NonTaskIAsyncResult())); + AssertExtensions.Throws("asyncResult", () => TaskToAsyncResult.Unwrap(Task.FromResult((long)42))); + } + + [Fact] + public async Task BeginFromTask_UnwrapTask_EndFromTask_Roundtrips() + { + var tcs = new TaskCompletionSource(); + object state = new object(); + + IAsyncResult ar = TaskToAsyncResult.Begin(tcs.Task, null, state); + Assert.NotNull(ar); + Assert.Same(state, ar.AsyncState); + Assert.NotNull(ar.AsyncWaitHandle); + Assert.False(ar.CompletedSynchronously); + Assert.False(ar.IsCompleted); + + Assert.Same(tcs.Task, TaskToAsyncResult.Unwrap(ar)); + Assert.Same(tcs.Task, TaskToAsyncResult.Unwrap(ar)); + + tcs.SetResult(42); + await tcs.Task; + + Assert.True(ar.IsCompleted); + Assert.False(ar.CompletedSynchronously); + Assert.True(ar.AsyncWaitHandle.WaitOne(0)); + + TaskToAsyncResult.End(ar); + Assert.Equal(42, TaskToAsyncResult.End(ar)); + } + + [Fact] + public void BeginFromTask_CompletedSynchronously_CallbackInvokedSynchronously() + { + Task t = Task.FromResult(42); + object state = new object(); + + int id = Environment.CurrentManagedThreadId; + + IAsyncResult arCallback = null; + IAsyncResult ar = TaskToAsyncResult.Begin(t, iar => + { + arCallback = iar; + + Assert.True(iar.CompletedSynchronously); + Assert.True(iar.IsCompleted); + Assert.Same(state, iar.AsyncState); + + Assert.Equal(id, Environment.CurrentManagedThreadId); + + Assert.Equal(42, TaskToAsyncResult.End(iar)); + }, state); + + Assert.Same(ar, arCallback); + Assert.True(ar.CompletedSynchronously); + Assert.True(ar.IsCompleted); + Assert.Same(state, ar.AsyncState); + } + + [Fact] + public async void BeginFromTask_CompletedAsynchronously_CallbackInvokedAsynchronously() + { + var tcs = new TaskCompletionSource(); + var invoked = new TaskCompletionSource(); + + var tl = new ThreadLocal(); + tl.Value = 42; + IAsyncResult ar = TaskToAsyncResult.Begin(tcs.Task, iar => + { + Assert.NotEqual(42, tl.Value); + Assert.False(iar.CompletedSynchronously); + Assert.True(iar.IsCompleted); + Assert.Null(iar.AsyncState); + invoked.SetResult(); + }, null); + tl.Value = 0; + + Assert.False(invoked.Task.IsCompleted); + Assert.False(ar.CompletedSynchronously); + Assert.False(ar.IsCompleted); + Assert.Null(ar.AsyncState); + Assert.NotNull(ar.AsyncWaitHandle); + Assert.False(ar.AsyncWaitHandle.WaitOne(0)); + + tcs.SetResult(); + await invoked.Task; + + Assert.False(ar.CompletedSynchronously); + Assert.True(ar.IsCompleted); + Assert.Null(ar.AsyncState); + Assert.NotNull(ar.AsyncWaitHandle); + Assert.True(ar.AsyncWaitHandle.WaitOne(0)); + } + + [Fact] + public void EndFromTask_PropagatesExceptions() + { + IAsyncResult ar = TaskToAsyncResult.Begin(Task.FromException(new FormatException()), null, null); + Assert.Throws(() => TaskToAsyncResult.End(ar)); + + ar = TaskToAsyncResult.Begin(Task.FromException(new FormatException()), null, null); + Assert.Throws(() => TaskToAsyncResult.End(ar)); + } + + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] + public async Task WithFromAsync_IAsyncResult_Roundtrips() + { + var tcs = new TaskCompletionSource(); + var invoked = new TaskCompletionSource(); + _ = Task.Factory.FromAsync(TaskToAsyncResult.Begin(tcs.Task, null, null), iar => + { + invoked.SetResult(); + }); + tcs.SetResult(); + await invoked.Task; + } + + [Fact] + public async Task WithFromAsync_Delegate_Roundtrips() + { + var tcs = new TaskCompletionSource(); + var invoked = new TaskCompletionSource(); + _ = Task.Factory.FromAsync( + (callback, state) => TaskToAsyncResult.Begin(tcs.Task, callback, state), + iar => invoked.SetResult(), + new object()); + tcs.SetResult(); + await invoked.Task; + } + } + + internal sealed class NonTaskIAsyncResult : IAsyncResult + { + public object? AsyncState { get; set; } + public WaitHandle AsyncWaitHandle { get; set; } + public bool CompletedSynchronously { get; set; } + public bool IsCompleted { get; set; } + } +} diff --git a/src/mono/mono/tests/threadpool-exceptions8.cs b/src/mono/mono/tests/threadpool-exceptions8.cs index 2049b6e..33ed30d 100644 --- a/src/mono/mono/tests/threadpool-exceptions8.cs +++ b/src/mono/mono/tests/threadpool-exceptions8.cs @@ -70,7 +70,7 @@ public class Tests { var d = new UnhandledExceptionEventHandler (helper.OnUnhandled); AppDomain.CurrentDomain.UnhandledException += d; - // this is TaskToApm.Begin (..., callback) where the callback is helper.WhenCompleted + // this is TaskToAsyncResult.Begin (..., callback) where the callback is helper.WhenCompleted Task.Delay (100).GetAwaiter().OnCompleted (helper.WhenCompleted); var wasSet = helper.WaitForExn (10000); // wait upto 10 seconds for the task to throw