Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CountdownEvent.cs
Go to the documentation of this file.
3
4namespace System.Threading;
5
6[DebuggerDisplay("Initial Count={InitialCount}, Current Count={CurrentCount}")]
8{
9 private int _initialCount;
10
11 private volatile int _currentCount;
12
13 private readonly ManualResetEventSlim _event;
14
15 private volatile bool _disposed;
16
17 public int CurrentCount
18 {
19 get
20 {
21 int currentCount = _currentCount;
22 if (currentCount >= 0)
23 {
24 return currentCount;
25 }
26 return 0;
27 }
28 }
29
31
32 public bool IsSet => _currentCount <= 0;
33
35 {
36 get
37 {
39 return _event.WaitHandle;
40 }
41 }
42
43 public CountdownEvent(int initialCount)
44 {
45 if (initialCount < 0)
46 {
47 throw new ArgumentOutOfRangeException("initialCount");
48 }
49 _initialCount = initialCount;
50 _currentCount = initialCount;
52 if (initialCount == 0)
53 {
54 _event.Set();
55 }
56 }
57
58 public void Dispose()
59 {
60 Dispose(disposing: true);
61 GC.SuppressFinalize(this);
62 }
63
64 protected virtual void Dispose(bool disposing)
65 {
66 if (disposing)
67 {
69 _disposed = true;
70 }
71 }
72
73 public bool Signal()
74 {
76 if (_currentCount <= 0)
77 {
79 }
80 int num = Interlocked.Decrement(ref _currentCount);
81 if (num == 0)
82 {
83 _event.Set();
84 return true;
85 }
86 if (num < 0)
87 {
89 }
90 return false;
91 }
92
93 public bool Signal(int signalCount)
94 {
95 if (signalCount <= 0)
96 {
97 throw new ArgumentOutOfRangeException("signalCount");
98 }
100 SpinWait spinWait = default(SpinWait);
101 int currentCount;
102 while (true)
103 {
104 currentCount = _currentCount;
105 if (currentCount < signalCount)
106 {
108 }
109 if (Interlocked.CompareExchange(ref _currentCount, currentCount - signalCount, currentCount) == currentCount)
110 {
111 break;
112 }
113 spinWait.SpinOnce(-1);
114 }
115 if (currentCount == signalCount)
116 {
117 _event.Set();
118 return true;
119 }
120 return false;
121 }
122
123 public void AddCount()
124 {
125 AddCount(1);
126 }
127
128 public bool TryAddCount()
129 {
130 return TryAddCount(1);
131 }
132
133 public void AddCount(int signalCount)
134 {
135 if (!TryAddCount(signalCount))
136 {
138 }
139 }
140
141 public bool TryAddCount(int signalCount)
142 {
143 if (signalCount <= 0)
144 {
145 throw new ArgumentOutOfRangeException("signalCount");
146 }
148 SpinWait spinWait = default(SpinWait);
149 while (true)
150 {
151 int currentCount = _currentCount;
152 if (currentCount <= 0)
153 {
154 return false;
155 }
156 if (currentCount > int.MaxValue - signalCount)
157 {
159 }
160 if (Interlocked.CompareExchange(ref _currentCount, currentCount + signalCount, currentCount) == currentCount)
161 {
162 break;
163 }
164 spinWait.SpinOnce(-1);
165 }
166 return true;
167 }
168
169 public void Reset()
170 {
172 }
173
174 public void Reset(int count)
175 {
177 if (count < 0)
178 {
179 throw new ArgumentOutOfRangeException("count");
180 }
183 if (count == 0)
184 {
185 _event.Set();
186 }
187 else
188 {
189 _event.Reset();
190 }
191 }
192
193 [UnsupportedOSPlatform("browser")]
194 public void Wait()
195 {
197 }
198
199 [UnsupportedOSPlatform("browser")]
204
205 [UnsupportedOSPlatform("browser")]
206 public bool Wait(TimeSpan timeout)
207 {
208 long num = (long)timeout.TotalMilliseconds;
209 if (num < -1 || num > int.MaxValue)
210 {
211 throw new ArgumentOutOfRangeException("timeout");
212 }
213 return Wait((int)num, CancellationToken.None);
214 }
215
216 [UnsupportedOSPlatform("browser")]
218 {
219 long num = (long)timeout.TotalMilliseconds;
220 if (num < -1 || num > int.MaxValue)
221 {
222 throw new ArgumentOutOfRangeException("timeout");
223 }
224 return Wait((int)num, cancellationToken);
225 }
226
227 [UnsupportedOSPlatform("browser")]
228 public bool Wait(int millisecondsTimeout)
229 {
231 }
232
233 [UnsupportedOSPlatform("browser")]
235 {
236 if (millisecondsTimeout < -1)
237 {
238 throw new ArgumentOutOfRangeException("millisecondsTimeout");
239 }
241 cancellationToken.ThrowIfCancellationRequested();
242 bool flag = _event.IsSet;
243 if (!flag)
244 {
246 }
247 return flag;
248 }
249
250 private void ThrowIfDisposed()
251 {
252 if (_disposed)
253 {
254 throw new ObjectDisposedException("CountdownEvent");
255 }
256 }
257}
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
static string CountdownEvent_Decrement_BelowZero
Definition SR.cs:18
static string CountdownEvent_Increment_AlreadyZero
Definition SR.cs:14
static string CountdownEvent_Increment_AlreadyMax
Definition SR.cs:16
Definition SR.cs:7
bool Wait(int millisecondsTimeout)
bool TryAddCount(int signalCount)
void Wait(CancellationToken cancellationToken)
virtual void Dispose(bool disposing)
bool Signal(int signalCount)
void AddCount(int signalCount)
bool Wait(TimeSpan timeout)
bool Wait(TimeSpan timeout, CancellationToken cancellationToken)
readonly ManualResetEventSlim _event
bool Wait(int millisecondsTimeout, CancellationToken cancellationToken)
static int CompareExchange(ref int location1, int value, int comparand)
static int Decrement(ref int location)