Skip to content

Commit 2a5de17

Browse files
committed
Make ValueTaskWaiter.Wait generic.
1 parent 2806c79 commit 2a5de17

File tree

1 file changed

+3
-21
lines changed

1 file changed

+3
-21
lines changed

src/BenchmarkDotNet/Helpers/AwaitHelper.cs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,7 @@ private ValueTaskWaiter()
2626
awaiterCallback = resetEvent.Set;
2727
}
2828

29-
// Hook up a callback instead of converting to Task to prevent extra allocations on each benchmark run.
30-
internal void Wait(ConfiguredValueTaskAwaitable.ConfiguredValueTaskAwaiter awaiter)
31-
{
32-
resetEvent.Reset();
33-
awaiter.UnsafeOnCompleted(awaiterCallback);
34-
35-
// The fastest way to wait for completion is to spin a bit before waiting on the event. This is the same logic that Task.GetAwaiter().GetResult() uses.
36-
var spinner = new SpinWait();
37-
while (!resetEvent.IsSet)
38-
{
39-
if (spinner.NextSpinWillYield)
40-
{
41-
resetEvent.Wait();
42-
return;
43-
}
44-
spinner.SpinOnce();
45-
}
46-
}
47-
48-
internal void Wait<T>(ConfiguredValueTaskAwaitable<T>.ConfiguredValueTaskAwaiter awaiter)
29+
internal void Wait<TAwaiter>(TAwaiter awaiter) where TAwaiter : ICriticalNotifyCompletion
4930
{
5031
resetEvent.Reset();
5132
awaiter.UnsafeOnCompleted(awaiterCallback);
@@ -70,7 +51,8 @@ internal void Wait<T>(ConfiguredValueTaskAwaitable<T>.ConfiguredValueTaskAwaiter
7051

7152
public static T GetResult<T>(Task<T> task) => task.GetAwaiter().GetResult();
7253

73-
// ValueTask can be backed by an IValueTaskSource that only supports asynchronous awaits, so we have to hook up a callback instead of calling .GetAwaiter().GetResult() like we do for Task.
54+
// ValueTask can be backed by an IValueTaskSource that only supports asynchronous awaits,
55+
// so we have to hook up a callback instead of calling .GetAwaiter().GetResult() like we do for Task.
7456
// The alternative is to convert it to Task using .AsTask(), but that causes allocations which we must avoid for memory diagnoser.
7557
public static void GetResult(ValueTask task)
7658
{

0 commit comments

Comments
 (0)