From 67f94ba67db9482c9f1d3ce0d52b8a9c9f673b98 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 7 Mar 2017 12:50:17 -0500 Subject: [PATCH] Add TaskExtensions to CoreLib Our TaskExtensions.Unwrap implementation in corefx is more allocation-heavy than the implementation we had in desktop and we could have in CoreLib, where CreateUnwrapPromise is available. By moving this implementation down to CoreLib, the code: ```C# Task toUnwrap = ...; Task t = toUnwrap.Unwrap(); ``` incurs 1 allocation instead of 4. Commit migrated from https://github.com/dotnet/coreclr/commit/d8f3e8eebee940cb3c52aee3df3298f1a143f304 --- .../src/mscorlib/System.Private.CoreLib.csproj | 1 + .../src/System/Threading/Tasks/TaskExtensions.cs | 48 ++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/coreclr/src/mscorlib/src/System/Threading/Tasks/TaskExtensions.cs diff --git a/src/coreclr/src/mscorlib/System.Private.CoreLib.csproj b/src/coreclr/src/mscorlib/System.Private.CoreLib.csproj index 5d5eb49..46c6081 100644 --- a/src/coreclr/src/mscorlib/System.Private.CoreLib.csproj +++ b/src/coreclr/src/mscorlib/System.Private.CoreLib.csproj @@ -708,6 +708,7 @@ + diff --git a/src/coreclr/src/mscorlib/src/System/Threading/Tasks/TaskExtensions.cs b/src/coreclr/src/mscorlib/src/System/Threading/Tasks/TaskExtensions.cs new file mode 100644 index 0000000..1098299 --- /dev/null +++ b/src/coreclr/src/mscorlib/src/System/Threading/Tasks/TaskExtensions.cs @@ -0,0 +1,48 @@ +// 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.Threading.Tasks +{ + /// Provides a set of static methods for working with specific kinds of instances. + public static class TaskExtensions + { + /// Creates a proxy that represents the asynchronous operation of a . + /// The to unwrap. + /// A that represents the asynchronous operation of the provided . + public static Task Unwrap(this Task task) + { + if (task == null) + { + throw new ArgumentNullException(nameof(task)); + } + + // If the task hasn't completed or was faulted/canceled, wrap it in an unwrap promise. Otherwise, + // it completed successfully. Return its inner task to avoid unnecessary wrapping, or if the inner + // task is null, return a canceled task to match the same semantics as CreateUnwrapPromise. + return + !task.IsRanToCompletion ? Task.CreateUnwrapPromise(task, lookForOce: false) : + task.Result ?? + Task.FromCanceled(new CancellationToken(true)); + } + + /// Creates a proxy that represents the asynchronous operation of a . + /// The to unwrap. + /// A that represents the asynchronous operation of the provided . + public static Task Unwrap(this Task> task) + { + if (task == null) + { + throw new ArgumentNullException(nameof(task)); + } + + // If the task hasn't completed or was faulted/canceled, wrap it in an unwrap promise. Otherwise, + // it completed successfully. Return its inner task to avoid unnecessary wrapping, or if the inner + // task is null, return a canceled task to match the same semantics as CreateUnwrapPromise. + return + !task.IsRanToCompletion ? Task.CreateUnwrapPromise(task, lookForOce: false) : + task.Result ?? + Task.FromCanceled(new CancellationToken(true)); + } + } +} -- 2.7.4