Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
AsyncMethodBuilderCore.cs
Go to the documentation of this file.
1
using
System.Diagnostics
;
2
using
System.Diagnostics.CodeAnalysis
;
3
using
System.Diagnostics.Tracing
;
4
using
System.Reflection
;
5
using
System.Text
;
6
using
System.Threading
;
7
using
System.Threading.Tasks
;
8
9
namespace
System.Runtime.CompilerServices
;
10
11
internal
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
{
30
_invokeAction
(
_continuation
,
_innerTask
);
31
}
32
}
33
34
internal
static
bool
TrackAsyncMethodCompletion
35
{
36
[MethodImpl(
MethodImplOptions
.AggressiveInlining)]
37
get
38
{
39
return
TplEventSource
.
Log
.IsEnabled(
EventLevel
.Warning, (
EventKeywords
)256
L
);
40
}
41
}
42
43
[DebuggerStepThrough]
44
public
static
void
Start<TStateMachine>
(ref TStateMachine
stateMachine
) where TStateMachine :
IAsyncStateMachine
45
{
46
if
(
stateMachine
==
null
)
47
{
48
ThrowHelper
.
ThrowArgumentNullException
(
ExceptionArgument
.stateMachine);
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
71
public
static
void
SetStateMachine
(
IAsyncStateMachine
stateMachine
,
Task
task
)
72
{
73
if
(
stateMachine
==
null
)
74
{
75
ThrowHelper
.
ThrowArgumentNullException
(
ExceptionArgument
.stateMachine);
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"
)]
84
internal
static
string
GetAsyncStateMachineDescription
(
IAsyncStateMachine
stateMachine
)
85
{
86
Type
type
=
stateMachine
.
GetType
();
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
}
System.Reflection.FieldInfo.GetValue
object? GetValue(object? obj)
System.Reflection.FieldInfo
Definition
FieldInfo.cs:8
System.Reflection.MemberInfo.Name
string Name
Definition
MemberInfo.cs:10
System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper._innerTask
readonly Task _innerTask
Definition
AsyncMethodBuilderCore.cs:19
System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.ContinuationWrapper
ContinuationWrapper(Action continuation, Action< Action, Task > invokeAction, Task innerTask)
Definition
AsyncMethodBuilderCore.cs:21
System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper._continuation
readonly Action _continuation
Definition
AsyncMethodBuilderCore.cs:17
System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper._invokeAction
readonly Action< Action, Task > _invokeAction
Definition
AsyncMethodBuilderCore.cs:15
System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke
void Invoke()
Definition
AsyncMethodBuilderCore.cs:28
System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper
Definition
AsyncMethodBuilderCore.cs:14
System.Runtime.CompilerServices.AsyncMethodBuilderCore.TryGetContinuationTask
static Task TryGetContinuationTask(Action continuation)
Definition
AsyncMethodBuilderCore.cs:119
System.Runtime.CompilerServices.AsyncMethodBuilderCore.SetStateMachine
static void SetStateMachine(IAsyncStateMachine stateMachine, Task task)
Definition
AsyncMethodBuilderCore.cs:71
System.Runtime.CompilerServices.AsyncMethodBuilderCore.TrackAsyncMethodCompletion
static bool TrackAsyncMethodCompletion
Definition
AsyncMethodBuilderCore.cs:35
System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start< TStateMachine >
static void Start< TStateMachine >(ref TStateMachine stateMachine)
Definition
AsyncMethodBuilderCore.cs:44
System.Runtime.CompilerServices.AsyncMethodBuilderCore.GetAsyncStateMachineDescription
static string GetAsyncStateMachineDescription(IAsyncStateMachine stateMachine)
Definition
AsyncMethodBuilderCore.cs:84
System.Runtime.CompilerServices.AsyncMethodBuilderCore.TryGetStateMachineForDebugger
static Action TryGetStateMachineForDebugger(Action action)
Definition
AsyncMethodBuilderCore.cs:105
System.Runtime.CompilerServices.AsyncMethodBuilderCore.CreateContinuationWrapper
static Action CreateContinuationWrapper(Action continuation, Action< Action, Task > invokeAction, Task innerTask)
Definition
AsyncMethodBuilderCore.cs:100
System.Runtime.CompilerServices.AsyncMethodBuilderCore
Definition
AsyncMethodBuilderCore.cs:12
System.Text.StringBuilder.AppendLine
StringBuilder AppendLine()
Definition
StringBuilder.cs:951
System.Text.StringBuilder
Definition
StringBuilder.cs:14
System.Threading.ExecutionContext.RestoreChangedContextToThread
static void RestoreChangedContextToThread(Thread currentThread, ExecutionContext contextToRestore, ExecutionContext currentContext)
Definition
ExecutionContext.cs:216
System.Threading.ExecutionContext
Definition
ExecutionContext.cs:10
System.Threading.SynchronizationContext
Definition
SynchronizationContext.cs:4
System.Threading.Tasks.Task
Definition
Task.cs:14
System.Threading.Tasks.TplEventSource.Log
static readonly TplEventSource Log
Definition
TplEventSource.cs:75
System.Threading.Tasks.TplEventSource
Definition
TplEventSource.cs:10
System.Threading.Thread._synchronizationContext
SynchronizationContext _synchronizationContext
Definition
Thread.cs:160
System.Threading.Thread.CurrentThread
static Thread CurrentThread
Definition
Thread.cs:312
System.Threading.Thread._executionContext
ExecutionContext _executionContext
Definition
Thread.cs:158
System.Threading.Thread
Definition
Thread.cs:13
System.ThrowHelper.ThrowInvalidOperationException
static void ThrowInvalidOperationException()
Definition
ThrowHelper.cs:34
System.ThrowHelper.ThrowArgumentNullException
static void ThrowArgumentNullException(string name)
Definition
ThrowHelper.cs:14
System.ThrowHelper
Definition
ThrowHelper.cs:6
System.Type.GetType
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition
Type.cs:408
System.Type
Definition
Type.cs:14
System.Runtime.CompilerServices.IAsyncStateMachineBox
Definition
IAsyncStateMachineBox.cs:4
System.Runtime.CompilerServices.IAsyncStateMachine
Definition
IAsyncStateMachine.cs:4
System.Diagnostics.CodeAnalysis
Definition
AllowNullAttribute.cs:1
System.Diagnostics.Tracing.EventLevel
EventLevel
Definition
EventLevel.cs:4
System.Diagnostics.Tracing.EventKeywords
EventKeywords
Definition
EventKeywords.cs:5
System.Diagnostics.Tracing
Definition
ActivityTracker.cs:4
System.Diagnostics
Definition
AggregationManager.cs:6
System.Reflection.BindingFlags
BindingFlags
Definition
BindingFlags.cs:5
System.Reflection
Definition
ICustomTypeProvider.cs:1
System.Runtime.CompilerServices.MethodImplOptions
MethodImplOptions
Definition
MethodImplOptions.cs:5
System.Runtime.CompilerServices
Definition
NullablePublicOnlyAttribute.cs:3
System.Text
Definition
ConsoleEncoding.cs:1
System.Threading.Tasks
Definition
TaskToApm.cs:3
System.Threading
Definition
TaskToApm.cs:3
System.ExceptionResource
ExceptionResource
Definition
ExceptionResource.cs:4
System.ExceptionArgument
ExceptionArgument
Definition
ExceptionArgument.cs:4
System.ExceptionArgument.stateMachine
@ stateMachine
System.ExceptionArgument.action
@ action
System.ExceptionArgument.task
@ task
System.ExceptionArgument.type
@ type
System.ExceptionArgument.array
@ array
System.ConsoleKey.L
@ L
source
System.Private.CoreLib
System.Runtime.CompilerServices
AsyncMethodBuilderCore.cs
Generated by
1.10.0