@@ -3207,13 +3207,24 @@ internal SetOnInvokeMres() : base(false, 0) { }
32073207 /// <returns>true if the task is completed; otherwise, false.</returns>
32083208 private bool SpinThenBlockingWait ( int millisecondsTimeout , CancellationToken cancellationToken )
32093209 {
3210+ if ( ! RuntimeFeature . IsMultithreadingSupported )
3211+ {
3212+ if ( IsCompleted )
3213+ {
3214+ return true ;
3215+ }
3216+ if ( millisecondsTimeout == 0 )
3217+ {
3218+ return false ;
3219+ }
3220+ RuntimeFeature . ThrowIfMultithreadingIsNotSupported ( ) ;
3221+ }
3222+
32103223 bool infiniteWait = millisecondsTimeout == Timeout . Infinite ;
32113224 uint startTimeTicks = infiniteWait ? 0 : ( uint ) Environment . TickCount ;
32123225 bool returnValue = SpinWait ( millisecondsTimeout ) ;
3213- if ( ! returnValue && ( millisecondsTimeout != 0 || RuntimeFeature . IsMultithreadingSupported ) )
3226+ if ( ! returnValue )
32143227 {
3215- RuntimeFeature . ThrowIfMultithreadingIsNotSupported ( ) ;
3216-
32173228 // We're about to block waiting for the task to complete, which is expensive, and if
32183229 // the task being waited on depends on some other work to run, this thread could end up
32193230 // waiting for some other thread to do work. If the two threads are part of the same scheduler,
@@ -3292,10 +3303,7 @@ private bool SpinWait(int millisecondsTimeout)
32923303 {
32933304 if ( IsCompleted ) return true ;
32943305
3295- if ( ! RuntimeFeature . IsMultithreadingSupported )
3296- {
3297- return false ;
3298- }
3306+ Debug . Assert ( RuntimeFeature . IsMultithreadingSupported ) ;
32993307
33003308 if ( millisecondsTimeout == 0 )
33013309 {
@@ -5133,8 +5141,6 @@ private static bool WaitAllCore(ReadOnlySpan<Task> tasks, int millisecondsTimeou
51335141 // If any of the waited-upon tasks end as Faulted or Canceled, set these to true.
51345142 bool exceptionSeen = false , cancellationSeen = false ;
51355143
5136- bool returnValue = true ;
5137-
51385144 // Collects incomplete tasks in "waitedOnTaskList"
51395145 for ( int i = tasks . Length - 1 ; i >= 0 ; i -- )
51405146 {
@@ -5173,36 +5179,35 @@ private static bool WaitAllCore(ReadOnlySpan<Task> tasks, int millisecondsTimeou
51735179
51745180 if ( waitedOnTaskList != null )
51755181 {
5176- if ( millisecondsTimeout != 0 || RuntimeFeature . IsMultithreadingSupported )
5182+ if ( ! RuntimeFeature . IsMultithreadingSupported )
51775183 {
5178- RuntimeFeature . ThrowIfMultithreadingIsNotSupported ( ) ;
5184+ if ( millisecondsTimeout != 0 )
5185+ RuntimeFeature . ThrowIfMultithreadingIsNotSupported ( ) ;
51795186
5180- // Block waiting for the tasks to complete.
5181- returnValue = WaitAllBlockingCore ( waitedOnTaskList , millisecondsTimeout , cancellationToken ) ;
5187+ return false ;
51825188 }
5183- else
5189+
5190+ // Block waiting for the tasks to complete.
5191+ if ( ! WaitAllBlockingCore ( waitedOnTaskList , millisecondsTimeout , cancellationToken ) )
51845192 {
5185- returnValue = false ;
5193+ return false ;
51865194 }
51875195
5188- // If the wait didn't time out, ensure exceptions are propagated, and if a debugger is
5196+ // The wait didn't time out, so ensure exceptions are propagated, and if a debugger is
51895197 // attached and one of these tasks requires it, that we notify the debugger of a wait completion.
5190- if ( returnValue )
5198+ // Add any exceptions for this task to the collection, and if it's wait
5199+ // notification bit is set, store it to operate on at the end.
5200+ foreach ( Task task in waitedOnTaskList )
51915201 {
5192- // Add any exceptions for this task to the collection, and if it's wait
5193- // notification bit is set, store it to operate on at the end.
5194- foreach ( Task task in waitedOnTaskList )
5195- {
5196- if ( task . IsFaulted ) exceptionSeen = true ;
5197- else if ( task . IsCanceled ) cancellationSeen = true ;
5198- if ( task . IsWaitNotificationEnabled ) AddToList ( task , ref notificationTasks , initSize : 1 ) ;
5199- }
5202+ if ( task . IsFaulted ) exceptionSeen = true ;
5203+ else if ( task . IsCanceled ) cancellationSeen = true ;
5204+ if ( task . IsWaitNotificationEnabled ) AddToList ( task , ref notificationTasks , initSize : 1 ) ;
52005205 }
52015206 }
52025207
5203- // Now that we're done and about to exit, if the wait completed and if we have
5208+ // Now that we're done and about to exit, if we have
52045209 // any tasks with a notification bit set, signal the debugger if any requires it.
5205- if ( returnValue && notificationTasks != null )
5210+ if ( notificationTasks != null )
52065211 {
52075212 // Loop through each task tha that had its bit set, and notify the debugger
52085213 // about the first one that requires it. The debugger will reset the bit
@@ -5215,7 +5220,7 @@ private static bool WaitAllCore(ReadOnlySpan<Task> tasks, int millisecondsTimeou
52155220 }
52165221
52175222 // If one or more threw exceptions, aggregate and throw them.
5218- if ( returnValue && ( exceptionSeen || cancellationSeen ) )
5223+ if ( exceptionSeen || cancellationSeen )
52195224 {
52205225 // If the WaitAll was canceled and tasks were canceled but not faulted,
52215226 // prioritize throwing an OCE for canceling the WaitAll over throwing an
@@ -5230,7 +5235,7 @@ private static bool WaitAllCore(ReadOnlySpan<Task> tasks, int millisecondsTimeou
52305235 ThrowHelper . ThrowAggregateException ( exceptions ) ;
52315236 }
52325237
5233- return returnValue ;
5238+ return true ;
52345239 }
52355240
52365241 /// <summary>Adds an element to the list, initializing the list if it's null.</summary>
@@ -5254,8 +5259,7 @@ private static bool WaitAllBlockingCore(List<Task> tasks, int millisecondsTimeou
52545259 {
52555260 Debug . Assert ( tasks != null , "Expected a non-null list of tasks" ) ;
52565261 Debug . Assert ( tasks . Count > 0 , "Expected at least one task" ) ;
5257-
5258- RuntimeFeature . ThrowIfMultithreadingIsNotSupported ( ) ;
5262+ Debug . Assert ( RuntimeFeature . IsMultithreadingSupported ) ;
52595263
52605264 bool waitCompleted = false ;
52615265 var mres = new SetOnCountdownMres ( tasks . Count ) ;
@@ -5520,11 +5524,15 @@ private static int WaitAnyCore(Task[] tasks, int millisecondsTimeout, Cancellati
55205524 }
55215525 }
55225526
5523- if ( signaledTaskIndex == - 1 &&
5524- tasks . Length != 0 &&
5525- ( millisecondsTimeout != 0 || RuntimeFeature . IsMultithreadingSupported ) )
5527+ if ( signaledTaskIndex == - 1 && tasks . Length != 0 )
55265528 {
5527- RuntimeFeature . ThrowIfMultithreadingIsNotSupported ( ) ;
5529+ if ( ! RuntimeFeature . IsMultithreadingSupported )
5530+ {
5531+ if ( millisecondsTimeout != 0 )
5532+ RuntimeFeature . ThrowIfMultithreadingIsNotSupported ( ) ;
5533+
5534+ return - 1 ;
5535+ }
55285536
55295537 Task < Task > firstCompleted = TaskFactory . CommonCWAnyLogic ( tasks , isSyncBlocking : true ) ;
55305538 bool waitCompleted = firstCompleted . Wait ( millisecondsTimeout , cancellationToken ) ;
0 commit comments