Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TimerQueueTimer.cs
Go to the documentation of this file.
4
5namespace System.Threading;
6
7[DebuggerDisplay("{DisplayString,nq}")]
8[DebuggerTypeProxy(typeof(TimerDebuggerTypeProxy))]
9internal sealed class TimerQueueTimer : IThreadPoolWorkItem
10{
11 internal sealed class TimerDebuggerTypeProxy
12 {
13 private readonly TimerQueueTimer _timer;
14
16 {
17 get
18 {
19 if (_timer._dueTime != uint.MaxValue)
20 {
21 long num = _timer._startTicks - TimerQueue.s_tickCountToTimeMap.TickCount + _timer._dueTime;
22 return TimerQueue.s_tickCountToTimeMap.Time + TimeSpan.FromMilliseconds(num);
23 }
24 return null;
25 }
26 }
27
29 {
30 get
31 {
32 if (_timer._dueTime != uint.MaxValue)
33 {
35 }
36 return null;
37 }
38 }
39
41 {
42 get
43 {
44 if (_timer._period != uint.MaxValue)
45 {
47 }
48 return null;
49 }
50 }
51
53
54 public object State => _timer._state;
55
57 {
58 _timer = timer._timer._timer;
59 }
60
62 {
63 _timer = timer;
64 }
65 }
66
68
70
72
73 internal bool _short;
74
75 internal long _startTicks;
76
77 internal uint _dueTime;
78
79 internal uint _period;
80
81 private readonly TimerCallback _timerCallback;
82
83 private readonly object _state;
84
86
87 private int _callbacksRunning;
88
89 private bool _canceled;
90
91 internal bool _everQueued;
92
94
95 private static readonly ContextCallback s_callCallbackInContext = delegate(object state)
96 {
97 TimerQueueTimer timerQueueTimer = (TimerQueueTimer)state;
98 timerQueueTimer._timerCallback(timerQueueTimer._state);
99 };
100
101 internal string DisplayString
102 {
103 get
104 {
105 string text = _timerCallback.Method.DeclaringType?.FullName;
106 if (text != null)
107 {
108 text += ".";
109 }
110 return "DueTime = " + ((_dueTime == uint.MaxValue) ? "(not set)" : ((object)TimeSpan.FromMilliseconds(_dueTime)))?.ToString() + ", Period = " + ((_period == uint.MaxValue) ? "(not set)" : ((object)TimeSpan.FromMilliseconds(_period)))?.ToString() + ", " + text + _timerCallback.Method.Name + "(" + (_state?.ToString() ?? "null") + ")";
111 }
112 }
113
114 internal TimerQueueTimer(TimerCallback timerCallback, object state, uint dueTime, uint period, bool flowExecutionContext)
115 {
116 _timerCallback = timerCallback;
117 _state = state;
118 _dueTime = uint.MaxValue;
119 _period = uint.MaxValue;
120 if (flowExecutionContext)
121 {
123 }
125 if (dueTime != uint.MaxValue)
126 {
127 Change(dueTime, period);
128 }
129 }
130
131 internal bool Change(uint dueTime, uint period)
132 {
134 {
135 if (_canceled)
136 {
138 }
139 _period = period;
140 if (dueTime == uint.MaxValue)
141 {
143 return true;
144 }
145 if (FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, (EventKeywords)16L))
146 {
147 FrameworkEventSource.Log.ThreadTransferSendObj(this, 1, string.Empty, multiDequeues: true, (int)dueTime, (int)period);
148 }
149 return _associatedTimerQueue.UpdateTimer(this, dueTime, period);
150 }
151 }
152
153 public void Close()
154 {
156 {
157 if (!_canceled)
158 {
159 _canceled = true;
161 }
162 }
163 }
164
165 public bool Close(WaitHandle toSignal)
166 {
167 bool flag = false;
168 bool result;
170 {
171 if (_canceled)
172 {
173 result = false;
174 }
175 else
176 {
177 _canceled = true;
180 flag = _callbacksRunning == 0;
181 result = true;
182 }
183 }
184 if (flag)
185 {
187 }
188 return result;
189 }
190
192 {
194 {
195 object notifyWhenNoCallbacksRunning = _notifyWhenNoCallbacksRunning;
196 if (_canceled)
197 {
198 if (notifyWhenNoCallbacksRunning is WaitHandle)
199 {
202 return ValueTask.FromException(ex);
203 }
204 }
205 else
206 {
207 _canceled = true;
209 }
210 if (_callbacksRunning == 0)
211 {
212 return default(ValueTask);
213 }
214 if (notifyWhenNoCallbacksRunning == null)
215 {
216 return new ValueTask((Task)(_notifyWhenNoCallbacksRunning = new Task(null, TaskCreationOptions.RunContinuationsAsynchronously, promiseStyle: true)));
217 }
218 return new ValueTask((Task)notifyWhenNoCallbacksRunning);
219 }
220 }
221
223 {
224 Fire(isThreadPool: true);
225 }
226
227 internal void Fire(bool isThreadPool = false)
228 {
229 bool flag = false;
231 {
232 flag = _canceled;
233 if (!flag)
234 {
236 }
237 }
238 if (!flag)
239 {
240 CallCallback(isThreadPool);
241 bool flag2;
243 {
246 }
247 if (flag2)
248 {
250 }
251 }
252 }
253
255 {
256 object notifyWhenNoCallbacksRunning = _notifyWhenNoCallbacksRunning;
257 if (notifyWhenNoCallbacksRunning is WaitHandle waitHandle)
258 {
259 EventWaitHandle.Set(waitHandle.SafeWaitHandle);
260 }
261 else
262 {
263 ((Task)notifyWhenNoCallbacksRunning).TrySetResult();
264 }
265 }
266
267 internal void CallCallback(bool isThreadPool)
268 {
269 if (FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, (EventKeywords)16L))
270 {
271 FrameworkEventSource.Log.ThreadTransferReceiveObj(this, 1, string.Empty);
272 }
273 ExecutionContext executionContext = _executionContext;
274 if (executionContext == null)
275 {
277 }
278 else if (isThreadPool)
279 {
281 }
282 else
283 {
285 }
286 }
287}
void SetCurrentStackTrace()
Definition Exception.cs:458
static string InvalidOperation_TimerAlreadyClosed
Definition SR.cs:1522
static string ObjectDisposed_Generic
Definition SR.cs:1742
Definition SR.cs:7
static void RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
static ? ExecutionContext Capture()
static void RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, object state)
static Thread CurrentThread
Definition Thread.cs:312
static int GetCurrentProcessorId()
Definition Thread.cs:501
readonly TimerQueueTimer _timer
Definition TimerHolder.cs:7
TimerQueueTimer(TimerCallback timerCallback, object state, uint dueTime, uint period, bool flowExecutionContext)
bool Change(uint dueTime, uint period)
void CallCallback(bool isThreadPool)
readonly ExecutionContext _executionContext
readonly TimerCallback _timerCallback
static readonly ContextCallback s_callCallbackInContext
readonly TimerQueue _associatedTimerQueue
void Fire(bool isThreadPool=false)
bool Close(WaitHandle toSignal)
static TimerQueue[] Instances
Definition TimerQueue.cs:57
bool UpdateTimer(TimerQueueTimer timer, uint dueTime, uint period)
void DeleteTimer(TimerQueueTimer timer)
TimerHolder _timer
Definition Timer.cs:12
delegate void TimerCallback(object? state)
delegate void ContextCallback(object? state)
static ValueTask FromException(Exception exception)
Definition ValueTask.cs:190
static TimeSpan FromMilliseconds(double value)
Definition TimeSpan.cs:228