@@ -47,8 +47,7 @@ private void CancellationRequested(object? expectedVersion, CancellationToken to
4747 if ( versionAndStatus . Status is not ManualResetCompletionSourceStatus . Activated )
4848 goto exit ;
4949
50- EnterLock ( ) ;
51- try
50+ lock ( SyncRoot )
5251 {
5352 if ( versionAndStatus . Status is not ManualResetCompletionSourceStatus . Activated
5453 || versionAndStatus . Version != ( short ) expectedVersion
@@ -58,14 +57,10 @@ private void CancellationRequested(object? expectedVersion, CancellationToken to
5857 // ensure that timeout or cancellation handler sets the status correctly
5958 Debug . Assert ( versionAndStatus . Status is ManualResetCompletionSourceStatus . WaitForConsumption ) ;
6059 }
61- finally
62- {
63- ExitLock ( ) ;
64- }
6560
6661 Resume ( ) ;
6762
68- exit :
63+ exit :
6964 return ;
7065 }
7166
@@ -100,9 +95,9 @@ private CancellationState ResetCore(out short token)
10095 /// <returns>The version of the uncompleted task.</returns>
10196 public short Reset ( )
10297 {
103- EnterLock ( ) ;
98+ Monitor . Enter ( SyncRoot ) ;
10499 var stateCopy = ResetCore ( out var token ) ;
105- ExitLock ( ) ;
100+ Monitor . Exit ( SyncRoot ) ;
106101
107102 stateCopy . Dispose ( ) ;
108103 CleanUp ( ) ;
@@ -119,10 +114,10 @@ public bool TryReset(out short token)
119114 {
120115 bool result ;
121116
122- if ( result = TryEnterLock ( ) )
117+ if ( result = Monitor . TryEnter ( SyncRoot ) )
123118 {
124119 var stateCopy = ResetCore ( out token ) ;
125- ExitLock ( ) ;
120+ Monitor . Exit ( SyncRoot ) ;
126121
127122 stateCopy . Dispose ( ) ;
128123 CleanUp ( ) ;
@@ -190,26 +185,26 @@ private void OnCompleted(in Continuation continuation, short token)
190185
191186 // code block doesn't have any calls leading to exceptions
192187 // so replace try-finally with manually cloned code
193- EnterLock ( ) ;
188+ Monitor . Enter ( SyncRoot ) ;
194189 if ( token != versionAndStatus . Version )
195190 {
196191 errorMessage = ExceptionMessages . InvalidSourceToken ;
197- ExitLock ( ) ;
192+ Monitor . Exit ( SyncRoot ) ;
198193 goto invalid_state ;
199194 }
200195
201196 switch ( versionAndStatus . Status )
202197 {
203198 default :
204199 errorMessage = ExceptionMessages . InvalidSourceState ;
205- ExitLock ( ) ;
200+ Monitor . Exit ( SyncRoot ) ;
206201 goto invalid_state ;
207202 case ManualResetCompletionSourceStatus . WaitForConsumption :
208- ExitLock ( ) ;
203+ Monitor . Exit ( SyncRoot ) ;
209204 break ;
210205 case ManualResetCompletionSourceStatus . Activated :
211206 this . continuation = continuation ;
212- ExitLock ( ) ;
207+ Monitor . Exit ( SyncRoot ) ;
213208 goto exit ;
214209 }
215210
@@ -294,27 +289,21 @@ public void OnCompleted(Action<object?> continuation, object? state, short token
294289 {
295290 Timeout . Validate ( timeout ) ;
296291
297- // The source can be completed before the activation. Moreover, someone could try
298- // to complete the task during the activation concurrently. To avoid lock contention,
299- // use TryEnterLock(). If we can't acquire the lock, there is concurrent completion.
300- // In that case, do not activate the source and skip fast.
301- return TryEnterLock ( ) ? ActivateSlow ( timeout , token ) : versionAndStatus . Version ;
302- }
303-
304- private short ? ActivateSlow ( TimeSpan timeout , CancellationToken token )
305- {
306292 short ? result ;
307- try
293+ lock ( SyncRoot )
308294 {
309295 switch ( versionAndStatus . Status )
310296 {
311- case ManualResetCompletionSourceStatus . WaitForActivation when timeout == default :
312- CompleteAsTimedOut ( ) ;
313- goto case ManualResetCompletionSourceStatus . WaitForConsumption ;
314297 case ManualResetCompletionSourceStatus . WaitForActivation :
315- if ( ! state . Initialize ( ref versionAndStatus , cancellationCallback , timeout , token ) )
298+ if ( timeout == TimeSpan . Zero )
299+ {
300+ CompleteAsTimedOut ( ) ;
301+ }
302+ else if ( ! state . Initialize ( ref versionAndStatus , cancellationCallback , timeout , token ) )
303+ {
316304 CompleteAsCanceled ( token ) ;
317-
305+ }
306+
318307 goto case ManualResetCompletionSourceStatus . WaitForConsumption ;
319308 case ManualResetCompletionSourceStatus . WaitForConsumption :
320309 result = versionAndStatus . Version ;
@@ -324,10 +313,6 @@ public void OnCompleted(Action<object?> continuation, object? state, short token
324313 break ;
325314 }
326315 }
327- finally
328- {
329- ExitLock ( ) ;
330- }
331316
332317 return result ;
333318 }
0 commit comments