#if NET20 || NET30 || NET35 || NET40 || !NET_4_6 // 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 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() { return new ConfiguredValueTaskAwaiter(_value, _continueOnCapturedContext); } /// Provides an awaiter for a . [StructLayout(LayoutKind.Auto)] public 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 { get { return _value.IsCompleted; } } /// Gets the result of the ValueTask. public TResult GetResult() { return _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); } } } } #endif