From ad37479995f02ed20859c55f2df7c959ce0b244c Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 29 Jan 2018 10:42:52 -0500 Subject: [PATCH] Use shared corelib files for ValueTask's System\Runtime\CompilerServices classes --- .../System.Threading.Tasks.Extensions.csproj | 21 +++- .../AsyncMethodBuilderAttribute.cs | 21 ---- .../AsyncValueTaskMethodBuilder.cs | 102 ------------------ .../ConfiguredValueTaskAwaitable.cs | 71 ------------ .../CompilerServices/ValueTaskAwaiter.cs | 37 ------- .../src/System/Threading/Tasks/ValueTask.cs | 3 + 6 files changed, 20 insertions(+), 235 deletions(-) delete mode 100644 src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs delete mode 100644 src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilder.cs delete mode 100644 src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/ConfiguredValueTaskAwaitable.cs delete mode 100644 src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/ValueTaskAwaiter.cs diff --git a/src/System.Threading.Tasks.Extensions/src/System.Threading.Tasks.Extensions.csproj b/src/System.Threading.Tasks.Extensions/src/System.Threading.Tasks.Extensions.csproj index 0bc93927d557..a869e16aec3f 100644 --- a/src/System.Threading.Tasks.Extensions/src/System.Threading.Tasks.Extensions.csproj +++ b/src/System.Threading.Tasks.Extensions/src/System.Threading.Tasks.Extensions.csproj @@ -23,11 +23,24 @@ - - - - + + + Common\CoreLib\System\Diagnostics\StackTraceHiddenAttribute.cs + + + Common\CoreLib\System\Runtime\CompilerServices\AsyncMethodBuilderAttribute.cs + + + Common\CoreLib\System\Runtime\CompilerServices\AsyncValueTaskMethodBuilder.cs + + + Common\CoreLib\System\Runtime\CompilerServices\ConfiguredValueTaskAwaitable.cs + + + Common\CoreLib\System\Runtime\CompilerServices\ValueTaskAwaiter.cs + + diff --git a/src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs b/src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs deleted file mode 100644 index 688a3a01ba72..000000000000 --- a/src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs +++ /dev/null @@ -1,21 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -namespace System.Runtime.CompilerServices -{ - /// - /// Indicates the type of the async method builder that should be used by a language compiler to - /// build the attributed type when used as the return type of an async method. - /// - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Delegate | AttributeTargets.Enum, Inherited = false, AllowMultiple = false)] - public sealed class AsyncMethodBuilderAttribute : Attribute - { - /// Initializes the . - /// The of the associated builder. - public AsyncMethodBuilderAttribute(Type builderType) => BuilderType = builderType; - - /// Gets the of the associated builder. - public Type BuilderType { get; } - } -} diff --git a/src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilder.cs b/src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilder.cs deleted file mode 100644 index 8cbcdc562fec..000000000000 --- a/src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilder.cs +++ /dev/null @@ -1,102 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Runtime.InteropServices; -using System.Security; -using System.Threading.Tasks; - -namespace System.Runtime.CompilerServices -{ - /// Represents a builder for asynchronous methods that returns a . - /// The type of the result. - [StructLayout(LayoutKind.Auto)] - public struct AsyncValueTaskMethodBuilder - { - /// The to which most operations are delegated. - private AsyncTaskMethodBuilder _methodBuilder; - /// The result for this builder, if it's completed before any awaits occur. - private TResult _result; - /// true if contains the synchronous result for the async method; otherwise, false. - private bool _haveResult; - /// true if the builder should be used for setting/getting the result; otherwise, false. - private bool _useBuilder; - - /// Creates an instance of the struct. - /// The initialized instance. - public static AsyncValueTaskMethodBuilder Create() => - new AsyncValueTaskMethodBuilder() { _methodBuilder = AsyncTaskMethodBuilder.Create() }; - - /// Begins running the builder with the associated state machine. - /// The type of the state machine. - /// The state machine instance, passed by reference. - public void Start(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine => - _methodBuilder.Start(ref stateMachine); // will provide the right ExecutionContext semantics - - /// Associates the builder with the specified state machine. - /// The state machine instance to associate with the builder. - public void SetStateMachine(IAsyncStateMachine stateMachine) => _methodBuilder.SetStateMachine(stateMachine); - - /// Marks the task as successfully completed. - /// The result to use to complete the task. - public void SetResult(TResult result) - { - if (_useBuilder) - { - _methodBuilder.SetResult(result); - } - else - { - _result = result; - _haveResult = true; - } - } - - /// Marks the task as failed and binds the specified exception to the task. - /// The exception to bind to the task. - public void SetException(Exception exception) => _methodBuilder.SetException(exception); - - /// Gets the task for this builder. - public ValueTask Task - { - get - { - if (_haveResult) - { - return new ValueTask(_result); - } - else - { - _useBuilder = true; - return new ValueTask(_methodBuilder.Task); - } - } - } - - /// Schedules the state machine to proceed to the next action when the specified awaiter completes. - /// The type of the awaiter. - /// The type of the state machine. - /// the awaiter - /// The state machine. - public void AwaitOnCompleted(ref TAwaiter awaiter, ref TStateMachine stateMachine) - where TAwaiter : INotifyCompletion - where TStateMachine : IAsyncStateMachine - { - _useBuilder = true; - _methodBuilder.AwaitOnCompleted(ref awaiter, ref stateMachine); - } - - /// Schedules the state machine to proceed to the next action when the specified awaiter completes. - /// The type of the awaiter. - /// The type of the state machine. - /// the awaiter - /// The state machine. - public void AwaitUnsafeOnCompleted(ref TAwaiter awaiter, ref TStateMachine stateMachine) - where TAwaiter : ICriticalNotifyCompletion - where TStateMachine : IAsyncStateMachine - { - _useBuilder = true; - _methodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine); - } - } -} diff --git a/src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/ConfiguredValueTaskAwaitable.cs b/src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/ConfiguredValueTaskAwaitable.cs deleted file mode 100644 index e0b92e5de185..000000000000 --- a/src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/ConfiguredValueTaskAwaitable.cs +++ /dev/null @@ -1,71 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Runtime.InteropServices; -using System.Threading.Tasks; - -namespace System.Runtime.CompilerServices -{ - /// Provides an awaitable type that enables configured awaits on a . - /// The type of the result produced. - [StructLayout(LayoutKind.Auto)] - public readonly struct ConfiguredValueTaskAwaitable - { - /// The wrapped . - private readonly ValueTask _value; - /// true to attempt to marshal the continuation back to the original context captured; otherwise, false. - private readonly bool _continueOnCapturedContext; - - /// Initializes the awaitable. - /// The wrapped . - /// - /// true to attempt to marshal the continuation back to the original synchronization context captured; otherwise, false. - /// - internal ConfiguredValueTaskAwaitable(ValueTask value, bool continueOnCapturedContext) - { - _value = value; - _continueOnCapturedContext = continueOnCapturedContext; - } - - /// Returns an awaiter for this instance. - public ConfiguredValueTaskAwaiter GetAwaiter() => - new ConfiguredValueTaskAwaiter(_value, _continueOnCapturedContext); - - /// Provides an awaiter for a . - [StructLayout(LayoutKind.Auto)] - public readonly struct ConfiguredValueTaskAwaiter : ICriticalNotifyCompletion - { - /// The value being awaited. - private readonly ValueTask _value; - /// The value to pass to ConfigureAwait. - private readonly bool _continueOnCapturedContext; - - /// Initializes the awaiter. - /// The value to be awaited. - /// The value to pass to ConfigureAwait. - internal ConfiguredValueTaskAwaiter(ValueTask value, bool continueOnCapturedContext) - { - _value = value; - _continueOnCapturedContext = continueOnCapturedContext; - } - - /// Gets whether the has completed. - public bool IsCompleted => _value.IsCompleted; - - /// Gets the result of the ValueTask. - public TResult GetResult() => - _value._task == null ? - _value._result : - _value._task.GetAwaiter().GetResult(); - - /// Schedules the continuation action for the . - public void OnCompleted(Action continuation) => - _value.AsTask().ConfigureAwait(_continueOnCapturedContext).GetAwaiter().OnCompleted(continuation); - - /// Schedules the continuation action for the . - public void UnsafeOnCompleted(Action continuation) => - _value.AsTask().ConfigureAwait(_continueOnCapturedContext).GetAwaiter().UnsafeOnCompleted(continuation); - } - } -} diff --git a/src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/ValueTaskAwaiter.cs b/src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/ValueTaskAwaiter.cs deleted file mode 100644 index 2774ba6ad354..000000000000 --- a/src/System.Threading.Tasks.Extensions/src/System/Runtime/CompilerServices/ValueTaskAwaiter.cs +++ /dev/null @@ -1,37 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Runtime.InteropServices; -using System.Threading.Tasks; - -namespace System.Runtime.CompilerServices -{ - /// Provides an awaiter for a . - public readonly struct ValueTaskAwaiter : ICriticalNotifyCompletion - { - /// The value being awaited. - private readonly ValueTask _value; - - /// Initializes the awaiter. - /// The value to be awaited. - internal ValueTaskAwaiter(ValueTask value) => _value = value; - - /// Gets whether the has completed. - public bool IsCompleted => _value.IsCompleted; - - /// Gets the result of the ValueTask. - public TResult GetResult() => - _value._task == null ? - _value._result : - _value._task.GetAwaiter().GetResult(); - - /// Schedules the continuation action for this ValueTask. - public void OnCompleted(Action continuation) => - _value.AsTask().ConfigureAwait(continueOnCapturedContext: true).GetAwaiter().OnCompleted(continuation); - - /// Schedules the continuation action for this ValueTask. - public void UnsafeOnCompleted(Action continuation) => - _value.AsTask().ConfigureAwait(continueOnCapturedContext: true).GetAwaiter().UnsafeOnCompleted(continuation); - } -} diff --git a/src/System.Threading.Tasks.Extensions/src/System/Threading/Tasks/ValueTask.cs b/src/System.Threading.Tasks.Extensions/src/System/Threading/Tasks/ValueTask.cs index f1f7227c1713..00c212a70d14 100644 --- a/src/System.Threading.Tasks.Extensions/src/System/Threading/Tasks/ValueTask.cs +++ b/src/System.Threading.Tasks.Extensions/src/System/Threading/Tasks/ValueTask.cs @@ -111,6 +111,9 @@ public Task AsTask() => // and the hash code we generate in GetHashCode. _task ?? Task.FromResult(_result); + /// Gets the task underlying this ValueTask. + internal Task AsTaskExpectNonNull() => AsTask(); // needed only for compilation; optimization only applies in coreclr's implementation + /// Gets whether the represents a completed operation. public bool IsCompleted => _task == null || _task.IsCompleted;