1+ using JetBrains . Annotations ;
2+ using Perfolizer . Horology ;
3+ using System . ComponentModel ;
4+ using System . Threading . Tasks . Sources ;
5+
6+ namespace BenchmarkDotNet . Engines ;
7+
8+ // This is used to prevent allocating a new async state machine on every benchmark iteration.
9+ [ UsedImplicitly ]
10+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
11+ public sealed class WorkloadValueTaskSource : IValueTaskSource < ClockSpan > , IValueTaskSource < bool >
12+ {
13+ private ManualResetValueTaskSourceCore < bool > continuerSource ;
14+ private ManualResetValueTaskSourceCore < ClockSpan > clockSpanSource ;
15+
16+ public ValueTask < ClockSpan > Continue ( )
17+ {
18+ clockSpanSource . Reset ( ) ;
19+ continuerSource . SetResult ( false ) ;
20+ return new ( this , clockSpanSource . Version ) ;
21+ }
22+
23+ public ValueTask < bool > GetIsComplete ( )
24+ => new ( this , continuerSource . Version ) ;
25+
26+ public void Complete ( )
27+ => continuerSource . SetResult ( true ) ;
28+
29+ public ValueTask < bool > SetResultAndGetIsComplete ( ClockSpan result )
30+ {
31+ continuerSource . Reset ( ) ;
32+ clockSpanSource . SetResult ( result ) ;
33+ return GetIsComplete ( ) ;
34+ }
35+
36+ public void SetException ( Exception exception )
37+ => clockSpanSource . SetException ( exception ) ;
38+
39+ ValueTaskSourceStatus IValueTaskSource < bool > . GetStatus ( short token )
40+ => continuerSource . GetStatus ( token ) ;
41+
42+ void IValueTaskSource < bool > . OnCompleted ( Action < object ? > continuation , object ? state , short token , ValueTaskSourceOnCompletedFlags flags )
43+ // Strip UseSchedulingContext to ensure Continue() and Complete() synchronously continue __WorkloadCore.
44+ => continuerSource . OnCompleted ( continuation , state , token , flags & ~ ValueTaskSourceOnCompletedFlags . UseSchedulingContext ) ;
45+
46+ bool IValueTaskSource < bool > . GetResult ( short token )
47+ => continuerSource . GetResult ( token ) ;
48+
49+ ClockSpan IValueTaskSource < ClockSpan > . GetResult ( short token )
50+ => clockSpanSource . GetResult ( token ) ;
51+
52+ ValueTaskSourceStatus IValueTaskSource < ClockSpan > . GetStatus ( short token )
53+ => clockSpanSource . GetStatus ( token ) ;
54+
55+ void IValueTaskSource < ClockSpan > . OnCompleted ( Action < object ? > continuation , object ? state , short token , ValueTaskSourceOnCompletedFlags flags )
56+ => clockSpanSource . OnCompleted ( continuation , state , token , flags ) ;
57+ }
0 commit comments