This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve ManualResetValueTaskSource prototype (dotnet#29468)
In .NET Core 2.1 we added the public `IValueTaskSource` and `IValueTaskSource<T>` interfaces, with associated support in `ValueTask` and `ValueTask<T>`, and while we implemented the interfaces on several types internally, we didn't expose any public implementations. We should consider exposing several in the future, including a manual-reset and an auto-reset IValueTaskSource implementation. We already have a ManualResetValueTaskSource implementation in our tests. This commit improves upon it in a few ways: - Separates out the logic into a separate public struct. The ManualResetValueTaskSource class wraps the struct, giving developers a choice to either use the class directly, or to embed the struct in their own implementation. - Fixes context capture to behave more similarly to Task, handling both SynchronizationContext and TaskSchedulers - Adds a prototype implementation of an IAsyncEnumerable, demonstrating how the compiler could utilize ManualResetValueTaskSourceLogic in its implementation. This is all still prototype, used only in tests. (cherry picked from commit df43abb)
- Loading branch information
1 parent
e74a664
commit 826ac29
Showing
8 changed files
with
528 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/Common/tests/System/Threading/Tasks/Sources/ManualResetValueTaskSourceFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// 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.ExceptionServices; | ||
using System.Threading.Tasks.Sources; | ||
|
||
namespace System.Threading.Tasks.Tests | ||
{ | ||
internal static class ManualResetValueTaskSourceFactory | ||
{ | ||
public static ManualResetValueTaskSource<T> Completed<T>(T result, Exception error = null) | ||
{ | ||
var vts = new ManualResetValueTaskSource<T>(); | ||
if (error != null) | ||
{ | ||
vts.SetException(error); | ||
} | ||
else | ||
{ | ||
vts.SetResult(result); | ||
} | ||
return vts; | ||
} | ||
|
||
public static ManualResetValueTaskSource<T> Delay<T>(int delayMs, T result, Exception error = null) | ||
{ | ||
var vts = new ManualResetValueTaskSource<T>(); | ||
Task.Delay(delayMs).ContinueWith(_ => | ||
{ | ||
if (error != null) | ||
{ | ||
vts.SetException(error); | ||
} | ||
else | ||
{ | ||
vts.SetResult(result); | ||
} | ||
}); | ||
return vts; | ||
} | ||
} | ||
} |
Oops, something went wrong.