@@ -25,15 +25,13 @@ private enum State : byte
25
25
private State _state ;
26
26
private ManualResetValueTaskSourceCore < bool > _valueTaskSource ;
27
27
private CancellationTokenRegistration _cancellationRegistration ;
28
- private Exception ? _exception ;
29
28
private GCHandle _keepAlive ;
30
29
31
30
public ValueTaskSource ( )
32
31
{
33
32
_state = State . None ;
34
33
_valueTaskSource = new ManualResetValueTaskSourceCore < bool > ( ) { RunContinuationsAsynchronously = true } ;
35
34
_cancellationRegistration = default ;
36
- _exception = default ;
37
35
_keepAlive = default ;
38
36
}
39
37
@@ -77,7 +75,7 @@ public bool TryInitialize(out ValueTask valueTask, object? keepAlive = null, Can
77
75
State state = _state ;
78
76
79
77
// If we're the first here, we will return true.
80
- if ( state == State . None && _valueTaskSource . GetStatus ( _valueTaskSource . Version ) == ValueTaskSourceStatus . Pending )
78
+ if ( state == State . None )
81
79
{
82
80
// Keep alive the caller object until the result is read from the task.
83
81
// Used for keeping caller alive during async interop calls.
@@ -106,42 +104,30 @@ private bool TryComplete(Exception? exception)
106
104
{
107
105
State state = _state ;
108
106
109
- // Completed: nothing to do.
110
- if ( state == State . Completed )
107
+ if ( state != State . Completed )
111
108
{
112
- return false ;
113
- }
114
-
115
- // With cancellation, keep the state as-is so it can be restored after the OCE is consumed.
116
- _state = exception is OperationCanceledException ? state : State . Completed ;
109
+ _state = State . Completed ;
117
110
118
- // Swap the cancellation registration so the one that's been registered gets eventually Disposed.
119
- // Ideally, we would dispose it here, but if the callbacks kicks in, it tries to take the lock held by this thread leading to deadlock.
120
- cancellationRegistration = _cancellationRegistration ;
121
- _cancellationRegistration = default ;
111
+ // Swap the cancellation registration so the one that's been registered gets eventually Disposed.
112
+ // Ideally, we would dispose it here, but if the callbacks kicks in, it tries to take the lock held by this thread leading to deadlock.
113
+ cancellationRegistration = _cancellationRegistration ;
114
+ _cancellationRegistration = default ;
122
115
123
- if ( exception is not null )
124
- {
125
- // Set up the exception stack trace for the caller.
126
- exception = exception . StackTrace is null ? ExceptionDispatchInfo . SetCurrentStackTrace ( exception ) : exception ;
127
- if ( _valueTaskSource . GetStatus ( _valueTaskSource . Version ) == ValueTaskSourceStatus . Pending )
116
+ if ( exception is not null )
128
117
{
118
+ // Set up the exception stack trace for the caller.
119
+ exception = exception . StackTrace is null ? ExceptionDispatchInfo . SetCurrentStackTrace ( exception ) : exception ;
129
120
_valueTaskSource . SetException ( exception ) ;
130
121
}
131
- else if ( exception is not OperationCanceledException )
132
- {
133
- _exception = exception ;
134
- }
135
- }
136
- else
137
- {
138
- if ( _valueTaskSource . GetStatus ( _valueTaskSource . Version ) == ValueTaskSourceStatus . Pending )
122
+ else
139
123
{
140
124
_valueTaskSource . SetResult ( true ) ;
141
125
}
126
+
127
+ return true ;
142
128
}
143
129
144
- return true ;
130
+ return false ;
145
131
}
146
132
finally
147
133
{
@@ -187,34 +173,5 @@ void IValueTaskSource.OnCompleted(Action<object?> continuation, object? state, s
187
173
=> _valueTaskSource . OnCompleted ( continuation , state , token , flags ) ;
188
174
189
175
void IValueTaskSource . GetResult ( short token )
190
- {
191
- try
192
- {
193
- _valueTaskSource . GetResult ( token ) ;
194
- }
195
- finally
196
- {
197
- lock ( this )
198
- {
199
- State state = _state ;
200
-
201
- // In case of a cancellation, reset the task and set the stored results if necessary.
202
- if ( _valueTaskSource . GetStatus ( _valueTaskSource . Version ) == ValueTaskSourceStatus . Canceled )
203
- {
204
- _valueTaskSource . Reset ( ) ;
205
- if ( state == State . Completed )
206
- {
207
- if ( _exception is not null )
208
- {
209
- _valueTaskSource . SetException ( _exception ) ;
210
- }
211
- else
212
- {
213
- _valueTaskSource . SetResult ( true ) ;
214
- }
215
- }
216
- }
217
- }
218
- }
219
- }
176
+ => _valueTaskSource . GetResult ( token ) ;
220
177
}
0 commit comments