Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
FastParallel.cs
Go to the documentation of this file.
1using System;
3
4namespace ReLogic.Threading;
5
6public static class FastParallel
7{
8 private class RangeTask
9 {
10 private readonly ParallelForAction _action;
11
12 private readonly int _fromInclusive;
13
14 private readonly int _toExclusive;
15
16 private readonly object _context;
17
18 private readonly CountdownEvent _countdown;
19
20 public RangeTask(ParallelForAction action, int fromInclusive, int toExclusive, object context, CountdownEvent countdown)
21 {
23 _fromInclusive = fromInclusive;
24 _toExclusive = toExclusive;
25 _context = context;
26 _countdown = countdown;
27 }
28
29 public void Invoke()
30 {
31 try
32 {
34 {
36 }
37 }
38 finally
39 {
41 }
42 }
43 }
44
45 public static bool ForceTasksOnCallingThread { get; set; }
46
47 static FastParallel()
48 {
50 }
51
52 public static void For(int fromInclusive, int toExclusive, ParallelForAction callback, object context = null)
53 {
54 int num = toExclusive - fromInclusive;
55 if (num == 0)
56 {
57 return;
58 }
59 int num2 = Math.Min(Math.Max(1, Environment.ProcessorCount + 1 - 1 - 1), num);
61 {
62 num2 = 1;
63 }
64 int num3 = num / num2;
65 int num4 = num % num2;
66 CountdownEvent countdownEvent = new CountdownEvent(num2);
67 int num5 = toExclusive;
68 for (int num6 = num2 - 1; num6 >= 0; num6--)
69 {
70 int num7 = num3;
71 if (num6 < num4)
72 {
73 num7++;
74 }
75 num5 -= num7;
76 int num8 = num5;
77 int toExclusive2 = num8 + num7;
78 RangeTask rangeTask = new RangeTask(callback, num8, toExclusive2, context, countdownEvent);
79 if (num6 < 1)
80 {
81 InvokeTask(rangeTask);
82 }
83 else
84 {
86 }
87 }
88 if (countdownEvent.Wait(10000))
89 {
90 return;
91 }
92 ThreadPool.GetAvailableThreads(out var workerThreads, out var _);
93 throw new Exception($"Fatal Deadlock in FastParallelFor. pending: {ThreadPool.PendingWorkItemCount}. avail: {workerThreads}");
94 }
95
96 private static void InvokeTask(object context)
97 {
98 ((RangeTask)context).Invoke();
99 }
100}
RangeTask(ParallelForAction action, int fromInclusive, int toExclusive, object context, CountdownEvent countdown)
readonly ParallelForAction _action
static void For(int fromInclusive, int toExclusive, ParallelForAction callback, object context=null)
static void InvokeTask(object context)
static int ProcessorCount
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static bool QueueUserWorkItem(WaitCallback callBack)
static void GetAvailableThreads(out int workerThreads, out int completionPortThreads)
delegate void ParallelForAction(int fromInclusive, int toExclusive, object context)