From: Stephen Toub Date: Thu, 24 Jan 2019 01:51:34 +0000 (-0500) Subject: Implement IAsyncDisposable.ConfigureAwait (#22160) X-Git-Tag: accepted/tizen/unified/20190813.215958~255 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9c6b200be7ea1af26a9d183d78e7ee5b06327639;p=platform%2Fupstream%2Fcoreclr.git Implement IAsyncDisposable.ConfigureAwait (#22160) --- diff --git a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems index 9ba6e27..954f2ef 100644 --- a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems @@ -511,6 +511,7 @@ + diff --git a/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/ConfiguredAsyncDisposable.cs b/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/ConfiguredAsyncDisposable.cs new file mode 100644 index 0000000..aa5e882 --- /dev/null +++ b/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/ConfiguredAsyncDisposable.cs @@ -0,0 +1,27 @@ +// 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; + +namespace System.Runtime.CompilerServices +{ + /// Provides a type that can be used to configure how awaits on an are performed. + [StructLayout(LayoutKind.Auto)] + public readonly struct ConfiguredAsyncDisposable + { + private readonly IAsyncDisposable _source; + private readonly bool _continueOnCapturedContext; + + internal ConfiguredAsyncDisposable(IAsyncDisposable source, bool continueOnCapturedContext) + { + _source = source; + _continueOnCapturedContext = continueOnCapturedContext; + } + + public ConfiguredValueTaskAwaitable DisposeAsync() => + // as with other "configured" awaitable-related type in CompilerServices, we don't null check to defend against + // misuse like `default(ConfiguredAsyncDisposable).DisposeAsync()`, which will null ref by design. + _source.DisposeAsync().ConfigureAwait(_continueOnCapturedContext); + } +} diff --git a/src/System.Private.CoreLib/shared/System/Threading/Tasks/TaskExtensions.cs b/src/System.Private.CoreLib/shared/System/Threading/Tasks/TaskExtensions.cs index 9192b14..75b4b0a 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Tasks/TaskExtensions.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Tasks/TaskExtensions.cs @@ -48,6 +48,13 @@ namespace System.Threading.Tasks Task.FromCanceled(new CancellationToken(true)); } + /// Configures how awaits on the tasks returned from an async disposable will be performed. + /// The source async disposable. + /// Whether to capture and marshal back to the current context. + /// The configured async disposable. + public static ConfiguredAsyncDisposable ConfigureAwait(this IAsyncDisposable source, bool continueOnCapturedContext) => + new ConfiguredAsyncDisposable(source, continueOnCapturedContext); + /// Configures how awaits on the tasks returned from an async iteration will be performed. /// The type of the objects being iterated. /// The source enumerable being iterated.