Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ExecutionContext.cs
Go to the documentation of this file.
6
7namespace System.Threading;
8
10{
11 internal static readonly ExecutionContext Default = new ExecutionContext();
12
14
16
18
19 private readonly bool m_isFlowSuppressed;
20
21 private readonly bool m_isDefault;
22
24
25 internal bool IsDefault => m_isDefault;
26
28 {
29 m_isDefault = true;
30 }
31
38
43
44 public static ExecutionContext? Capture()
45 {
47 if (executionContext == null)
48 {
50 }
51 else if (executionContext.m_isFlowSuppressed)
52 {
53 executionContext = null;
54 }
55 return executionContext;
56 }
57
59 {
60 return Thread.CurrentThread._executionContext;
61 }
62
75
90
91 public static void RestoreFlow()
92 {
93 Thread currentThread = Thread.CurrentThread;
95 if (executionContext == null || !executionContext.m_isFlowSuppressed)
96 {
98 }
100 }
101
102 public static bool IsFlowSuppressed()
103 {
104 return Thread.CurrentThread._executionContext?.m_isFlowSuppressed ?? false;
105 }
106
107 public static void Run(ExecutionContext executionContext, ContextCallback callback, object? state)
108 {
109 if (executionContext == null)
110 {
112 }
114 }
115
154
156 {
157 if (executionContext == null)
158 {
160 }
162 }
163
165 {
166 Thread currentThread = Thread.CurrentThread;
168 if (executionContext2 != null && executionContext2.m_isDefault)
169 {
170 executionContext2 = null;
171 }
172 if (executionContext != null && executionContext.m_isDefault)
173 {
174 executionContext = null;
175 }
177 {
179 }
180 }
181
205
206 internal static void RunForThreadPoolUnsafe<TState>(ExecutionContext executionContext, Action<TState> callback, in TState state)
207 {
209 if (executionContext.HasChangeNotifications)
210 {
212 }
213 callback(state);
214 }
215
217 {
219 if ((currentContext != null && currentContext.HasChangeNotifications) || (contextToRestore != null && contextToRestore.HasChangeNotifications))
220 {
222 }
223 }
224
225 [MethodImpl(MethodImplOptions.AggressiveInlining)]
226 internal static void ResetThreadPoolThread(Thread currentThread)
227 {
231 if (executionContext != null && executionContext.HasChangeNotifications)
232 {
236 }
237 }
238
240 {
241 IAsyncLocal[] array = previousExecutionCtx?.m_localChangeNotifications;
242 IAsyncLocal[] array2 = nextExecutionCtx?.m_localChangeNotifications;
243 try
244 {
245 if (array != null && array2 != null)
246 {
248 foreach (IAsyncLocal asyncLocal in array3)
249 {
250 previousExecutionCtx.m_localValues.TryGetValue(asyncLocal, out var value);
251 nextExecutionCtx.m_localValues.TryGetValue(asyncLocal, out var value2);
252 if (value != value2)
253 {
255 }
256 }
257 if (array2 == array)
258 {
259 return;
260 }
262 foreach (IAsyncLocal asyncLocal2 in array4)
263 {
264 if (!previousExecutionCtx.m_localValues.TryGetValue(asyncLocal2, out var value3))
265 {
266 nextExecutionCtx.m_localValues.TryGetValue(asyncLocal2, out var value4);
267 if (value3 != value4)
268 {
270 }
271 }
272 }
273 return;
274 }
275 if (array != null)
276 {
278 foreach (IAsyncLocal asyncLocal3 in array5)
279 {
280 previousExecutionCtx.m_localValues.TryGetValue(asyncLocal3, out var value5);
281 if (value5 != null)
282 {
284 }
285 }
286 return;
287 }
289 foreach (IAsyncLocal asyncLocal4 in array6)
290 {
291 nextExecutionCtx.m_localValues.TryGetValue(asyncLocal4, out var value6);
292 if (value6 != null)
293 {
295 }
296 }
297 }
298 catch (Exception exception)
299 {
301 }
302 }
303
306 private static void ThrowNullContext()
307 {
309 }
310
311 internal static object GetLocalValue(IAsyncLocal local)
312 {
314 if (executionContext == null)
315 {
316 return null;
317 }
318 executionContext.m_localValues.TryGetValue(local, out var value);
319 return value;
320 }
321
323 {
325 object value = null;
326 bool flag = false;
327 if (executionContext != null)
328 {
329 flag = executionContext.m_localValues.TryGetValue(local, out value);
330 }
331 if (value == newValue)
332 {
333 return;
334 }
335 IAsyncLocal[] array = null;
336 bool flag2 = false;
338 if (executionContext != null)
339 {
340 flag2 = executionContext.m_isFlowSuppressed;
342 array = executionContext.m_localChangeNotifications;
343 }
344 else
345 {
347 }
348 if (needChangeNotifications && !flag)
349 {
350 if (array == null)
351 {
352 array = new IAsyncLocal[1] { local };
353 }
354 else
355 {
356 int num = array.Length;
357 Array.Resize(ref array, num + 1);
358 array[num] = local;
359 }
360 }
363 {
364 local.OnValueChanged(value, newValue, contextChanged: false);
365 }
366 }
367
369 {
370 return this;
371 }
372
373 public void Dispose()
374 {
375 }
376}
static void FailFast(string? message)
static ExceptionDispatchInfo Capture(Exception source)
static string InvalidOperation_CannotSupressFlowMultipleTimes
Definition SR.cs:1420
static string ExecutionContext_ExceptionInAsyncLocalNotification
Definition SR.cs:1312
static string InvalidOperation_NullContext
Definition SR.cs:1494
static string InvalidOperation_CannotRestoreUnsupressedFlow
Definition SR.cs:1418
Definition SR.cs:7
static bool IsEmpty(IAsyncLocalValueMap asyncLocalValueMap)
static IAsyncLocalValueMap Create(IAsyncLocal key, object value, bool treatNullValueAsNonexistent)
static volatile ExecutionContext s_defaultFlowSuppressed
static AsyncFlowControl SuppressFlow()
static void RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
static ExecutionContext CaptureForRestore()
static void RestoreInternal(ExecutionContext executionContext)
static readonly ExecutionContext Default
static void Restore(ExecutionContext executionContext)
static void RunForThreadPoolUnsafe< TState >(ExecutionContext executionContext, Action< TState > callback, in TState state)
static ? ExecutionContext Capture()
static void ResetThreadPoolThread(Thread currentThread)
static void SetLocalValue(IAsyncLocal local, object newValue, bool needChangeNotifications)
ExecutionContext ShallowClone(bool isFlowSuppressed)
static void RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, object state)
static void RestoreChangedContextToThread(Thread currentThread, ExecutionContext contextToRestore, ExecutionContext currentContext)
static object GetLocalValue(IAsyncLocal local)
readonly IAsyncLocalValueMap m_localValues
readonly IAsyncLocal[] m_localChangeNotifications
static void OnValuesChanged(ExecutionContext previousExecutionCtx, ExecutionContext nextExecutionCtx)
ExecutionContext(IAsyncLocalValueMap localValues, IAsyncLocal[] localChangeNotifications, bool isFlowSuppressed)
static void Run(ExecutionContext executionContext, ContextCallback callback, object? state)
void GetObjectData(SerializationInfo info, StreamingContext context)
SynchronizationContext _synchronizationContext
Definition Thread.cs:160
static Thread CurrentThread
Definition Thread.cs:312
ExecutionContext _executionContext
Definition Thread.cs:158
IAsyncLocalValueMap Set(IAsyncLocal key, object value, bool treatNullValueAsNonexistent)
void OnValueChanged(object previousValue, object currentValue, bool contextChanged)
delegate void ContextCallback(object? state)
void Initialize(Thread currentThread)