-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathWorkloadContinuerAndValueTaskSource.cs
More file actions
65 lines (50 loc) · 2.16 KB
/
WorkloadContinuerAndValueTaskSource.cs
File metadata and controls
65 lines (50 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using JetBrains.Annotations;
using Perfolizer.Horology;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks.Sources;
namespace BenchmarkDotNet.Engines;
// This is used to prevent allocating a new async state machine on every benchmark iteration.
[UsedImplicitly]
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class WorkloadContinuerAndValueTaskSource : ICriticalNotifyCompletion, IValueTaskSource<ClockSpan>
{
private static readonly Action s_completedSentinel = CompletedMethod;
private static void CompletedMethod() => throw new InvalidOperationException();
private Action? continuation;
private ManualResetValueTaskSourceCore<ClockSpan> valueTaskSourceCore;
public ValueTask<ClockSpan> Continue()
{
valueTaskSourceCore.Reset();
var callback = continuation;
continuation = null;
callback?.Invoke();
return new(this, valueTaskSourceCore.Version);
}
public void Complete()
{
var callback = continuation;
continuation = s_completedSentinel;
callback?.Invoke();
}
public void SetResult(ClockSpan result)
=> valueTaskSourceCore.SetResult(result);
public void SetException(Exception exception)
=> valueTaskSourceCore.SetException(exception);
// Await infrastructure
public WorkloadContinuerAndValueTaskSource GetAwaiter()
=> this;
public void OnCompleted(Action continuation)
=> UnsafeOnCompleted(continuation);
public void UnsafeOnCompleted(Action continuation)
=> this.continuation = continuation;
public bool IsCompleted
=> continuation == s_completedSentinel;
public void GetResult() { }
ClockSpan IValueTaskSource<ClockSpan>.GetResult(short token)
=> valueTaskSourceCore.GetResult(token);
ValueTaskSourceStatus IValueTaskSource<ClockSpan>.GetStatus(short token)
=> valueTaskSourceCore.GetStatus(token);
void IValueTaskSource<ClockSpan>.OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags)
=> valueTaskSourceCore.OnCompleted(continuation, state, token, flags);
}