Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SpinWait.cs
Go to the documentation of this file.
1namespace System.Threading;
2
3public struct SpinWait
4{
5 internal static readonly int SpinCountforSpinBeforeWait = (Environment.IsSingleProcessor ? 1 : 35);
6
7 private int _count;
8
9 public int Count
10 {
11 get
12 {
13 return _count;
14 }
15 internal set
16 {
17 _count = value;
18 }
19 }
20
22 {
23 get
24 {
25 if (_count < 10)
26 {
28 }
29 return true;
30 }
31 }
32
33 public void SpinOnce()
34 {
35 SpinOnceCore(20);
36 }
37
38 public void SpinOnce(int sleep1Threshold)
39 {
40 if (sleep1Threshold < -1)
41 {
42 throw new ArgumentOutOfRangeException("sleep1Threshold", sleep1Threshold, SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
43 }
44 if (sleep1Threshold >= 0 && sleep1Threshold < 10)
45 {
46 sleep1Threshold = 10;
47 }
48 SpinOnceCore(sleep1Threshold);
49 }
50
51 private void SpinOnceCore(int sleep1Threshold)
52 {
53 if ((_count >= 10 && ((_count >= sleep1Threshold && sleep1Threshold >= 0) || (_count - 10) % 2 == 0)) || Environment.IsSingleProcessor)
54 {
55 if (_count >= sleep1Threshold && sleep1Threshold >= 0)
56 {
57 Thread.Sleep(1);
58 }
59 else
60 {
61 int num = ((_count >= 10) ? ((_count - 10) / 2) : _count);
62 if (num % 5 == 4)
63 {
64 Thread.Sleep(0);
65 }
66 else
67 {
68 Thread.Yield();
69 }
70 }
71 }
72 else
73 {
75 if (_count <= 30 && 1 << _count < num2)
76 {
77 num2 = 1 << _count;
78 }
79 Thread.SpinWait(num2);
80 }
81 _count = ((_count == int.MaxValue) ? 10 : (_count + 1));
82 }
83
84 public void Reset()
85 {
86 _count = 0;
87 }
88
89 public static void SpinUntil(Func<bool> condition)
90 {
91 SpinUntil(condition, -1);
92 }
93
94 public static bool SpinUntil(Func<bool> condition, TimeSpan timeout)
95 {
96 long num = (long)timeout.TotalMilliseconds;
97 if (num < -1 || num > int.MaxValue)
98 {
100 }
101 return SpinUntil(condition, (int)num);
102 }
103
104 public static bool SpinUntil(Func<bool> condition, int millisecondsTimeout)
105 {
106 if (millisecondsTimeout < -1)
107 {
109 }
110 if (condition == null)
111 {
113 }
114 uint num = 0u;
116 {
117 num = TimeoutHelper.GetTime();
118 }
119 SpinWait spinWait = default(SpinWait);
120 while (!condition())
121 {
122 if (millisecondsTimeout == 0)
123 {
124 return false;
125 }
126 spinWait.SpinOnce();
128 {
129 return false;
130 }
131 }
132 return true;
133 }
134}
static bool IsSingleProcessor
static string ArgumentOutOfRange_NeedNonNegOrNegative1
Definition SR.cs:1072
static string SpinWait_SpinUntil_TimeoutWrong
Definition SR.cs:1922
static string SpinWait_SpinUntil_ArgumentNull
Definition SR.cs:1920
Definition SR.cs:7
static int OptimalMaxSpinWaitsPerSpinIteration
Definition Thread.cs:237
static bool Yield()
Definition Thread.cs:412
static void Sleep(int millisecondsTimeout)
Definition Thread.cs:658
static void SpinWait(int iterations)
Definition Thread.cs:404
static bool SpinUntil(Func< bool > condition, int millisecondsTimeout)
Definition SpinWait.cs:104
static void SpinUntil(Func< bool > condition)
Definition SpinWait.cs:89
void SpinOnceCore(int sleep1Threshold)
Definition SpinWait.cs:51
static readonly int SpinCountforSpinBeforeWait
Definition SpinWait.cs:5
void SpinOnce(int sleep1Threshold)
Definition SpinWait.cs:38
static bool SpinUntil(Func< bool > condition, TimeSpan timeout)
Definition SpinWait.cs:94