Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ ForWorker< TLocal >()

static ParallelLoopResult System.Threading.Tasks.Parallel.ForWorker< TLocal > ( int fromInclusive,
int toExclusive,
ParallelOptions parallelOptions,
Action< int > body,
Action< int, ParallelLoopState > bodyWithState,
Func< int, ParallelLoopState, TLocal, TLocal > bodyWithLocal,
Func< TLocal > localInit,
Action< TLocal > localFinally )
inlinestaticprivate

Definition at line 448 of file Parallel.cs.

449 {
450 ParallelLoopResult result = default(ParallelLoopResult);
452 {
453 result._completed = true;
454 return result;
455 }
456 ParallelLoopStateFlags32 sharedPStateFlags = new ParallelLoopStateFlags32();
457 parallelOptions.CancellationToken.ThrowIfCancellationRequested();
458 int nNumExpectedWorkers = ((parallelOptions.EffectiveMaxConcurrencyLevel == -1) ? Environment.ProcessorCount : parallelOptions.EffectiveMaxConcurrencyLevel);
459 RangeManager rangeManager = new RangeManager(fromInclusive, toExclusive, 1L, nNumExpectedWorkers);
460 OperationCanceledException oce = null;
461 CancellationTokenRegistration cancellationTokenRegistration = ((!parallelOptions.CancellationToken.CanBeCanceled) ? default(CancellationTokenRegistration) : parallelOptions.CancellationToken.UnsafeRegister((Action<object?>)delegate
462 {
463 oce = new OperationCanceledException(parallelOptions.CancellationToken);
464 sharedPStateFlags.Cancel();
465 }, (object?)null));
466 int forkJoinContextID = 0;
467 if (ParallelEtwProvider.Log.IsEnabled())
468 {
469 forkJoinContextID = Interlocked.Increment(ref s_forkJoinContextID);
470 ParallelEtwProvider.Log.ParallelLoopBegin(TaskScheduler.Current.Id, Task.CurrentId.GetValueOrDefault(), forkJoinContextID, ParallelEtwProvider.ForkJoinOperationType.ParallelFor, fromInclusive, toExclusive);
471 }
472 try
473 {
474 try
475 {
476 TaskReplicator.Run(delegate(ref RangeWorker currentWorker, int timeout, out bool replicationDelegateYieldedBeforeCompletion)
477 {
478 if (!currentWorker.IsInitialized)
479 {
480 currentWorker = rangeManager.RegisterNewWorker();
481 }
484 {
485 return;
486 }
487 if (ParallelEtwProvider.Log.IsEnabled())
488 {
489 ParallelEtwProvider.Log.ParallelFork(TaskScheduler.Current.Id, Task.CurrentId.GetValueOrDefault(), forkJoinContextID);
490 }
491 TLocal val = default(TLocal);
492 bool flag = false;
493 try
494 {
495 ParallelLoopState32 parallelLoopState = null;
496 if (bodyWithState != null)
497 {
498 parallelLoopState = new ParallelLoopState32(sharedPStateFlags);
499 }
500 else if (bodyWithLocal != null)
501 {
502 parallelLoopState = new ParallelLoopState32(sharedPStateFlags);
503 if (localInit != null)
504 {
505 val = localInit();
506 flag = true;
507 }
508 }
510 do
511 {
512 if (body != null)
513 {
514 for (int i = nFromInclusiveLocal; i < nToExclusiveLocal; i++)
515 {
516 if (sharedPStateFlags.LoopStateFlags != 0 && sharedPStateFlags.ShouldExitLoop())
517 {
518 break;
519 }
520 body(i);
521 }
522 }
523 else if (bodyWithState != null)
524 {
525 for (int j = nFromInclusiveLocal; j < nToExclusiveLocal && (sharedPStateFlags.LoopStateFlags == 0 || !sharedPStateFlags.ShouldExitLoop(j)); j++)
526 {
527 parallelLoopState.CurrentIteration = j;
529 }
530 }
531 else
532 {
533 for (int k = nFromInclusiveLocal; k < nToExclusiveLocal && (sharedPStateFlags.LoopStateFlags == 0 || !sharedPStateFlags.ShouldExitLoop(k)); k++)
534 {
535 parallelLoopState.CurrentIteration = k;
536 val = bodyWithLocal(k, parallelLoopState, val);
537 }
538 }
540 {
542 break;
543 }
544 }
545 while (currentWorker.FindNewWork32(out nFromInclusiveLocal, out nToExclusiveLocal) && (sharedPStateFlags.LoopStateFlags == 0 || !sharedPStateFlags.ShouldExitLoop(nFromInclusiveLocal)));
546 }
547 catch (Exception source)
548 {
549 sharedPStateFlags.SetExceptional();
551 }
552 finally
553 {
554 if (localFinally != null && flag)
555 {
556 localFinally(val);
557 }
558 if (ParallelEtwProvider.Log.IsEnabled())
559 {
560 ParallelEtwProvider.Log.ParallelJoin(TaskScheduler.Current.Id, Task.CurrentId.GetValueOrDefault(), forkJoinContextID);
561 }
562 }
564 }
565 finally
566 {
567 if (parallelOptions.CancellationToken.CanBeCanceled)
568 {
570 }
571 }
572 if (oce != null)
573 {
574 throw oce;
575 }
576 }
577 catch (AggregateException ex)
578 {
579 ThrowSingleCancellationExceptionOrOtherException(ex.InnerExceptions, parallelOptions.CancellationToken, ex);
580 }
581 finally
582 {
583 int loopStateFlags = sharedPStateFlags.LoopStateFlags;
584 result._completed = loopStateFlags == 0;
585 if (((uint)loopStateFlags & 2u) != 0)
586 {
587 result._lowestBreakIteration = sharedPStateFlags.LowestBreakIteration;
588 }
589 if (ParallelEtwProvider.Log.IsEnabled())
590 {
591 int num = 0;
592 num = ((loopStateFlags == 0) ? (toExclusive - fromInclusive) : (((loopStateFlags & 2) == 0) ? (-1) : (sharedPStateFlags.LowestBreakIteration - fromInclusive)));
593 ParallelEtwProvider.Log.ParallelLoopEnd(TaskScheduler.Current.Id, Task.CurrentId.GetValueOrDefault(), forkJoinContextID, num);
594 }
595 }
596 return result;
597 }
static int ComputeTimeoutPoint(int timeoutLength)
Definition Parallel.cs:443
static void ThrowSingleCancellationExceptionOrOtherException(ICollection exceptions, CancellationToken cancelToken, Exception otherException)
Definition Parallel.cs:1447
static bool CheckTimeoutReached(int timeoutOccursAt)
Definition Parallel.cs:429

References System.Threading.Tasks.Parallel.CheckTimeoutReached(), System.Threading.Tasks.Parallel.ComputeTimeoutPoint(), System.Threading.Tasks.TaskScheduler.Current, System.Threading.Tasks.Task< TResult >.CurrentId, System.Threading.Interlocked.Increment(), System.L, System.Threading.Tasks.ParallelEtwProvider.Log, System.Environment.ProcessorCount, System.Threading.Tasks.Parallel.s_forkJoinContextID, System.source, System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(), System.Threading.Tasks.Parallel.ThrowSingleCancellationExceptionOrOtherException(), and System.timeout.