Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TimerQueue.cs
Go to the documentation of this file.
6
7namespace System.Threading;
8
9[DebuggerDisplay("Count = {CountForDebugger}")]
10[DebuggerTypeProxy(typeof(TimerQueueDebuggerTypeProxy))]
11internal sealed class TimerQueue
12{
14 {
17 {
18 }
19
20 protected override bool ReleaseHandle()
21 {
23 }
24 }
25
26 private sealed class TimerQueueDebuggerTypeProxy
27 {
29
32
34 {
35 _queue = queue ?? throw new ArgumentNullException("queue");
36 }
37 }
38
39 private readonly int _id;
40
42
43 internal static readonly (long TickCount, DateTime Time) s_tickCountToTimeMap = (TickCount: TickCount64, Time: DateTime.UtcNow);
44
45 private bool _isTimerScheduled;
46
48
50
52
54
56
57 public static TimerQueue[] Instances { get; } = CreateTimerQueues();
58
59
60 private int CountForDebugger
61 {
62 get
63 {
64 int num = 0;
66 {
67 num++;
68 }
69 return num;
70 }
71 }
72
73 public long ActiveCount { get; private set; }
74
75 private static long TickCount64
76 {
77 get
78 {
80 {
81 ulong UnbiasedTime;
83 return (long)(UnbiasedTime / 10000);
84 }
86 }
87 }
88
89 private TimerQueue(int id)
90 {
91 _id = id;
92 }
93
94 [MethodImpl(MethodImplOptions.AggressiveInlining)]
104
105 internal static void AppDomainTimerCallback(int id)
106 {
107 Instances[id].FireNextTimers();
108 }
109
110 [DllImport("QCall", CharSet = CharSet.Unicode)]
111 private static extern AppDomainTimerSafeHandle CreateAppDomainTimer(uint dueTime, int id);
112
113 [DllImport("QCall", CharSet = CharSet.Unicode)]
115
116 [DllImport("QCall", CharSet = CharSet.Unicode)]
117 private static extern bool DeleteAppDomainTimer(IntPtr handle);
118
119 private static TimerQueue[] CreateTimerQueues()
120 {
122 for (int i = 0; i < array.Length; i++)
123 {
124 array[i] = new TimerQueue(i);
125 }
126 return array;
127 }
128
130 {
131 for (TimerQueueTimer timer2 = _shortTimers; timer2 != null; timer2 = timer2._next)
132 {
133 yield return timer2;
134 }
135 for (TimerQueueTimer timer2 = _longTimers; timer2 != null; timer2 = timer2._next)
136 {
137 yield return timer2;
138 }
139 }
140
142 {
143 uint num = Math.Min(requestedDuration, 268435455u);
145 {
148 {
149 return true;
150 }
151 uint num3 = _currentTimerDuration - (uint)(int)num2;
152 if (num >= num3)
153 {
154 return true;
155 }
156 }
157 if (SetTimer(num))
158 {
159 _isTimerScheduled = true;
162 return true;
163 }
164 return false;
165 }
166
167 private void FireNextTimers()
168 {
170 lock (this)
171 {
172 _isTimerScheduled = false;
173 bool flag = false;
174 uint num = uint.MaxValue;
175 long tickCount = TickCount64;
177 for (int i = 0; i < 2; i++)
178 {
179 while (timerQueueTimer2 != null)
180 {
182 long num2 = tickCount - timerQueueTimer2._startTicks;
183 long num3 = timerQueueTimer2._dueTime - num2;
184 if (num3 <= 0)
185 {
186 timerQueueTimer2._everQueued = true;
187 if (timerQueueTimer2._period != uint.MaxValue)
188 {
189 timerQueueTimer2._startTicks = tickCount;
190 long num4 = num2 - timerQueueTimer2._dueTime;
191 timerQueueTimer2._dueTime = ((num4 >= timerQueueTimer2._period) ? 1u : (timerQueueTimer2._period - (uint)(int)num4));
192 if (timerQueueTimer2._dueTime < num)
193 {
194 flag = true;
195 num = timerQueueTimer2._dueTime;
196 }
197 bool flag2 = tickCount + timerQueueTimer2._dueTime - _currentAbsoluteThreshold <= 0;
198 if (timerQueueTimer2._short != flag2)
199 {
201 }
202 }
203 else
204 {
206 }
207 if (timerQueueTimer == null)
208 {
210 }
211 else
212 {
214 }
215 }
216 else
217 {
218 if (num3 < num)
219 {
220 flag = true;
221 num = (uint)num3;
222 }
223 if (!timerQueueTimer2._short && num3 <= 333)
224 {
226 }
227 }
228 timerQueueTimer2 = next;
229 }
230 if (i != 0)
231 {
232 continue;
233 }
234 long num5 = _currentAbsoluteThreshold - tickCount;
235 if (num5 > 0)
236 {
237 if (_shortTimers == null && _longTimers != null)
238 {
239 num = (uint)((int)num5 + 1);
240 flag = true;
241 }
242 break;
243 }
245 _currentAbsoluteThreshold = tickCount + 333;
246 }
247 if (flag)
248 {
250 }
251 }
252 timerQueueTimer?.Fire();
253 }
254
256 {
257 long tickCount = TickCount64;
258 long num = tickCount + dueTime;
259 bool flag = _currentAbsoluteThreshold - num >= 0;
260 if (timer._dueTime == uint.MaxValue)
261 {
262 timer._short = flag;
264 long activeCount = ActiveCount + 1;
266 }
267 else if (timer._short != flag)
268 {
270 timer._short = flag;
272 }
273 timer._dueTime = dueTime;
274 timer._period = ((period == 0) ? uint.MaxValue : period);
275 timer._startTicks = tickCount;
277 }
278
280 {
282 timer._short = shortList;
284 }
285
287 {
289 timer._next = reference;
290 if (timer._next != null)
291 {
292 timer._next._prev = timer;
293 }
294 timer._prev = null;
296 }
297
299 {
300 TimerQueueTimer next = timer._next;
301 if (next != null)
302 {
303 next._prev = timer._prev;
304 }
305 if (_shortTimers == timer)
306 {
307 _shortTimers = next;
308 }
309 else if (_longTimers == timer)
310 {
311 _longTimers = next;
312 }
313 next = timer._prev;
314 if (next != null)
315 {
316 next._next = timer._next;
317 }
318 }
319
321 {
322 if (timer._dueTime != uint.MaxValue)
323 {
324 long activeCount = ActiveCount - 1;
327 timer._prev = null;
328 timer._next = null;
329 timer._dueTime = uint.MaxValue;
330 timer._period = uint.MaxValue;
331 timer._startTicks = 0L;
332 timer._short = false;
333 }
334 }
335}
static bool QueryUnbiasedInterruptTime(out ulong UnbiasedTime)
static long TickCount64
static int ProcessorCount
static bool IsWindows8OrAbove
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static void UnsafeQueueUserWorkItemInternal(object callBack, bool preferLocal)
void MoveTimerToCorrectList(TimerQueueTimer timer, bool shortList)
void LinkTimer(TimerQueueTimer timer)
AppDomainTimerSafeHandle m_appDomainTimer
Definition TimerQueue.cs:41
static void AppDomainTimerCallback(int id)
TimerQueueTimer _shortTimers
Definition TimerQueue.cs:51
IEnumerable< TimerQueueTimer > GetTimersForDebugger()
static readonly(long TickCount, DateTime Time) s_tickCountToTimeMap
static bool ChangeAppDomainTimer(AppDomainTimerSafeHandle handle, uint dueTime)
TimerQueueTimer _longTimers
Definition TimerQueue.cs:53
bool SetTimer(uint actualDuration)
Definition TimerQueue.cs:95
static TimerQueue[] CreateTimerQueues()
static bool DeleteAppDomainTimer(IntPtr handle)
bool EnsureTimerFiresBy(uint requestedDuration)
static TimerQueue[] Instances
Definition TimerQueue.cs:57
bool UpdateTimer(TimerQueueTimer timer, uint dueTime, uint period)
static AppDomainTimerSafeHandle CreateAppDomainTimer(uint dueTime, int id)
void DeleteTimer(TimerQueueTimer timer)
void UnlinkTimer(TimerQueueTimer timer)
static unsafe DateTime UtcNow
Definition DateTime.cs:142