Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
AsyncMethodBuilderCore.cs
Go to the documentation of this file.
5using System.Text;
8
10
11internal static class AsyncMethodBuilderCore
12{
13 private sealed class ContinuationWrapper
14 {
15 private readonly Action<Action, Task> _invokeAction;
16
17 internal readonly Action _continuation;
18
19 internal readonly Task _innerTask;
20
21 internal ContinuationWrapper(Action continuation, Action<Action, Task> invokeAction, Task innerTask)
22 {
23 _invokeAction = invokeAction;
24 _continuation = continuation;
25 _innerTask = innerTask;
26 }
27
28 internal void Invoke()
29 {
31 }
32 }
33
34 internal static bool TrackAsyncMethodCompletion
35 {
36 [MethodImpl(MethodImplOptions.AggressiveInlining)]
37 get
38 {
39 return TplEventSource.Log.IsEnabled(EventLevel.Warning, (EventKeywords)256L);
40 }
41 }
42
43 [DebuggerStepThrough]
44 public static void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
45 {
46 if (stateMachine == null)
47 {
49 }
50 Thread currentThread = Thread.CurrentThread;
51 ExecutionContext executionContext = currentThread._executionContext;
52 SynchronizationContext synchronizationContext = currentThread._synchronizationContext;
53 try
54 {
55 stateMachine.MoveNext();
56 }
57 finally
58 {
59 if (synchronizationContext != currentThread._synchronizationContext)
60 {
61 currentThread._synchronizationContext = synchronizationContext;
62 }
63 ExecutionContext executionContext2 = currentThread._executionContext;
64 if (executionContext != executionContext2)
65 {
66 ExecutionContext.RestoreChangedContextToThread(currentThread, executionContext, executionContext2);
67 }
68 }
69 }
70
72 {
73 if (stateMachine == null)
74 {
76 }
77 if (task != null)
78 {
79 ThrowHelper.ThrowInvalidOperationException(ExceptionResource.AsyncMethodBuilder_InstanceNotInitialized);
80 }
81 }
82
83 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", Justification = "It's okay if unused fields disappear from debug views")]
85 {
87 FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
88 StringBuilder stringBuilder = new StringBuilder();
89 stringBuilder.AppendLine(type.FullName);
90 FieldInfo[] array = fields;
91 foreach (FieldInfo fieldInfo in array)
92 {
93 stringBuilder.Append(" ").Append(fieldInfo.Name).Append(": ")
94 .Append(fieldInfo.GetValue(stateMachine))
95 .AppendLine();
96 }
97 return stringBuilder.ToString();
98 }
99
100 internal static Action CreateContinuationWrapper(Action continuation, Action<Action, Task> invokeAction, Task innerTask)
101 {
102 return new ContinuationWrapper(continuation, invokeAction, innerTask).Invoke;
103 }
104
105 internal static Action TryGetStateMachineForDebugger(Action action)
106 {
107 object target = action.Target;
108 if (!(target is IAsyncStateMachineBox asyncStateMachineBox))
109 {
110 if (!(target is ContinuationWrapper continuationWrapper))
111 {
112 return action;
113 }
114 return TryGetStateMachineForDebugger(continuationWrapper._continuation);
115 }
116 return asyncStateMachineBox.GetStateMachineObject().MoveNext;
117 }
118
119 internal static Task TryGetContinuationTask(Action continuation)
120 {
121 if (!(continuation.Target is ContinuationWrapper continuationWrapper))
122 {
123 return continuation.Target as Task;
124 }
125 return continuationWrapper._innerTask;
126 }
127}
object? GetValue(object? obj)
ContinuationWrapper(Action continuation, Action< Action, Task > invokeAction, Task innerTask)
static void SetStateMachine(IAsyncStateMachine stateMachine, Task task)
static void Start< TStateMachine >(ref TStateMachine stateMachine)
static string GetAsyncStateMachineDescription(IAsyncStateMachine stateMachine)
static Action CreateContinuationWrapper(Action continuation, Action< Action, Task > invokeAction, Task innerTask)
static void RestoreChangedContextToThread(Thread currentThread, ExecutionContext contextToRestore, ExecutionContext currentContext)
static readonly TplEventSource Log
SynchronizationContext _synchronizationContext
Definition Thread.cs:160
static Thread CurrentThread
Definition Thread.cs:312
ExecutionContext _executionContext
Definition Thread.cs:158
static void ThrowInvalidOperationException()
static void ThrowArgumentNullException(string name)
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408