-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prototyping T RuntimeHelpers.Await<T>(Task<T>)
#2941
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,8 +178,8 @@ public static unsafe ReadOnlySpan<T> CreateSpan<T>(RuntimeFieldHandle fldHandle) | |
|
||
#if !NATIVEAOT | ||
[Intrinsic] | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
[BypassReadyToRun] | ||
[MethodImpl(MethodImplOptions.NoInlining | (MethodImplOptions)0x0400)] // NoInlining | Async | ||
public static void AwaitAwaiterFromRuntimeAsync<TAwaiter>(TAwaiter awaiter) where TAwaiter : INotifyCompletion | ||
{ | ||
ref RuntimeAsyncAwaitState state = ref t_runtimeAsyncAwaitState; | ||
|
@@ -196,7 +196,7 @@ public static void AwaitAwaiterFromRuntimeAsync<TAwaiter>(TAwaiter awaiter) wher | |
// recognizes as an async2 call. | ||
[Intrinsic] | ||
[BypassReadyToRun] | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
[MethodImpl(MethodImplOptions.NoInlining | (MethodImplOptions)0x0400)] // NoInlining | Async | ||
public static void UnsafeAwaitAwaiterFromRuntimeAsync<TAwaiter>(TAwaiter awaiter) where TAwaiter : ICriticalNotifyCompletion | ||
{ | ||
ref RuntimeAsyncAwaitState state = ref t_runtimeAsyncAwaitState; | ||
|
@@ -208,6 +208,25 @@ public static void UnsafeAwaitAwaiterFromRuntimeAsync<TAwaiter>(TAwaiter awaiter | |
SuspendAsync2(sentinelContinuation); | ||
return; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The interesting part. The rest of code changes are mechanical - to make the helper known as special method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see a good reason to not write this function in terms of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Possibly. I started on that path, but was running into asserts (something about conditional BB not ending with conditional jump,...). I was not sure if that was something that I did wrong or issues with This way worked though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we will need an explicit |
||
// TODO: should this be called "AwaitFromRuntimeAsync" ? (i.e. same as above, but no "Awaiter") | ||
// | ||
// Marked intrinsic since this needs to be | ||
// recognizes as an async2 call. | ||
[Intrinsic] | ||
[BypassReadyToRun] | ||
[MethodImpl(MethodImplOptions.NoInlining | (MethodImplOptions)0x0400)] // NoInlining | Async | ||
public static T Await<T>(Task<T> task) | ||
{ | ||
TaskAwaiter<T> awaiter = task.GetAwaiter(); | ||
if (!awaiter.IsCompleted) | ||
{ | ||
UnsafeAwaitAwaiterFromRuntimeAsync(awaiter); | ||
} | ||
|
||
return awaiter.GetResult(); | ||
} | ||
|
||
#endif | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.IL"> | ||
<PropertyGroup> | ||
<Optimize>True</Optimize> | ||
<DefineConstants>AWAIT;$(DefineConstants)</DefineConstants> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="varying-yields.cs" /> | ||
</ItemGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
//#define ASYNC1_TASK | ||
//#define ASYNC1_VALUETASK | ||
|
||
#pragma warning disable 4014, 1998 | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
|
@@ -91,9 +93,19 @@ async2 Task<long> | |
double liveState3 = _yieldProbability; | ||
|
||
if (depth == 0) | ||
#if AWAIT | ||
return RuntimeHelpers.Await(Loop()); | ||
#else | ||
return await Loop(); | ||
#endif | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
long result = | ||
#if AWAIT | ||
RuntimeHelpers.Await(Run(depth - 1)); | ||
#else | ||
await Run(depth - 1); | ||
#endif | ||
|
||
long result = await Run(depth - 1); | ||
Sink = (int)liveState1 + (int)liveState2 + (int)(1 / liveState3) + depth; | ||
return result; | ||
} | ||
|
@@ -117,7 +129,12 @@ async2 Task<long> | |
{ | ||
for (int i = 0; i < 20; i++) | ||
{ | ||
numIters += await DoYields(); | ||
numIters += | ||
#if AWAIT | ||
RuntimeHelpers.Await(DoYields()); | ||
#else | ||
await DoYields(); | ||
#endif | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can probably be removed now that these are marked as runtime-async via MethodImpl
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, nothing special about the helpers names now.
I think the whole special-casing of async function in hijacking will be unnecessary as we stop caring about return kinds, except on x86