Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
AwaitTaskContinuation.cs
Go to the documentation of this file.
3
5
7{
9
10 protected readonly Action m_action;
11
12 protected int m_continuationId;
13
14 private static readonly ContextCallback s_invokeContextCallback = delegate(object state)
15 {
16 ((Action)state)();
17 };
18
19 private static readonly Action<Action> s_invokeAction = delegate(Action action)
20 {
21 action();
22 };
23
24 internal static bool IsValidLocationForInlining
25 {
26 get
27 {
29 if (current != null && current.GetType() != typeof(SynchronizationContext))
30 {
31 return false;
32 }
34 if (internalCurrent != null)
35 {
36 return internalCurrent == TaskScheduler.Default;
37 }
38 return true;
39 }
40 }
41
42 internal AwaitTaskContinuation(Action action, bool flowExecutionContext)
43 {
45 if (flowExecutionContext)
46 {
48 }
49 }
50
51 protected Task CreateTask(Action<object> action, object state, TaskScheduler scheduler)
52 {
53 return new Task(action, state, null, default(CancellationToken), TaskCreationOptions.None, InternalTaskOptions.QueuedByRuntime, scheduler)
54 {
55 CapturedContext = m_capturedContext
56 };
57 }
58
59 internal override void Run(Task task, bool canInlineContinuationTask)
60 {
61 if (canInlineContinuationTask && IsValidLocationForInlining)
62 {
64 return;
65 }
67 if (log.IsEnabled())
68 {
70 log.AwaitTaskContinuationScheduled((task.ExecutingTaskScheduler ?? TaskScheduler.Default).Id, task.Id, m_continuationId);
71 }
72 ThreadPool.UnsafeQueueUserWorkItemInternal(this, preferLocal: true);
73 }
74
76 {
78 ExecutionContext capturedContext = m_capturedContext;
79 if (!log.IsEnabled() && capturedContext == null)
80 {
81 m_action();
82 return;
83 }
84 Guid oldActivityThatWillContinue = default(Guid);
85 if (log.IsEnabled() && log.TasksSetActivityIds && m_continuationId != 0)
86 {
88 EventSource.SetCurrentThreadActivityId(activityId, out oldActivityThatWillContinue);
89 }
90 try
91 {
92 if (capturedContext == null || capturedContext.IsDefault)
93 {
94 m_action();
95 }
96 else
97 {
98 ExecutionContext.RunForThreadPoolUnsafe(capturedContext, s_invokeAction, in m_action);
99 }
100 }
101 finally
102 {
103 if (log.IsEnabled() && log.TasksSetActivityIds && m_continuationId != 0)
104 {
105 EventSource.SetCurrentThreadActivityId(oldActivityThatWillContinue);
106 }
107 }
108 }
109
110 [MethodImpl(MethodImplOptions.AggressiveInlining)]
112 {
114 }
115
116 protected void RunCallback(ContextCallback callback, object state, ref Task currentTask)
117 {
118 Task task = currentTask;
119 try
120 {
121 if (task != null)
122 {
123 currentTask = null;
124 }
125 ExecutionContext capturedContext = m_capturedContext;
126 if (capturedContext == null)
127 {
128 callback(state);
129 }
130 else
131 {
132 ExecutionContext.RunInternal(capturedContext, callback, state);
133 }
134 }
135 catch (Exception exception)
136 {
138 }
139 finally
140 {
141 if (task != null)
142 {
143 currentTask = task;
144 }
145 }
146 }
147
148 internal static void RunOrScheduleAction(Action action, bool allowInlining)
149 {
150 Task t_currentTask = Task.t_currentTask;
151 if (!allowInlining || !IsValidLocationForInlining)
152 {
153 UnsafeScheduleAction(action, t_currentTask);
154 return;
155 }
156 try
157 {
158 if (t_currentTask != null)
159 {
160 Task.t_currentTask = null;
161 }
162 action();
163 }
164 catch (Exception exception)
165 {
167 }
168 finally
169 {
170 if (t_currentTask != null)
171 {
172 Task.t_currentTask = t_currentTask;
173 }
174 }
175 }
176
177 internal static void RunOrScheduleAction(IAsyncStateMachineBox box, bool allowInlining)
178 {
179 Task t_currentTask = Task.t_currentTask;
180 if (!allowInlining || !IsValidLocationForInlining)
181 {
182 if (TplEventSource.Log.IsEnabled())
183 {
184 UnsafeScheduleAction(box.MoveNextAction, t_currentTask);
185 }
186 else
187 {
188 ThreadPool.UnsafeQueueUserWorkItemInternal(box, preferLocal: true);
189 }
190 return;
191 }
192 try
193 {
194 if (t_currentTask != null)
195 {
196 Task.t_currentTask = null;
197 }
198 box.MoveNext();
199 }
200 catch (Exception exception)
201 {
203 }
204 finally
205 {
206 if (t_currentTask != null)
207 {
208 Task.t_currentTask = t_currentTask;
209 }
210 }
211 }
212
213 internal static void UnsafeScheduleAction(Action action, Task task)
214 {
215 AwaitTaskContinuation awaitTaskContinuation = new AwaitTaskContinuation(action, flowExecutionContext: false);
217 if (log.IsEnabled() && task != null)
218 {
219 awaitTaskContinuation.m_continuationId = Task.NewId();
220 log.AwaitTaskContinuationScheduled((task.ExecutingTaskScheduler ?? TaskScheduler.Default).Id, task.Id, awaitTaskContinuation.m_continuationId);
221 }
222 ThreadPool.UnsafeQueueUserWorkItemInternal(awaitTaskContinuation, preferLocal: true);
223 }
224
229}
static void SetCurrentThreadActivityId(Guid activityId)
static void RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
static ? ExecutionContext Capture()
static ? SynchronizationContext Current
static void RunOrScheduleAction(Action action, bool allowInlining)
override void Run(Task task, bool canInlineContinuationTask)
static readonly ContextCallback s_invokeContextCallback
static readonly Action< Action > s_invokeAction
void RunCallback(ContextCallback callback, object state, ref Task currentTask)
Task CreateTask(Action< object > action, object state, TaskScheduler scheduler)
static void UnsafeScheduleAction(Action action, Task task)
AwaitTaskContinuation(Action action, bool flowExecutionContext)
static void RunOrScheduleAction(IAsyncStateMachineBox box, bool allowInlining)
static ? TaskScheduler InternalCurrent
static Task t_currentTask
Definition Task.cs:942
static void ThrowAsync(Exception exception, SynchronizationContext targetContext)
Definition Task.cs:1806
static readonly TplEventSource Log
static Guid CreateGuidForTaskID(int taskID)
unsafe void AwaitTaskContinuationScheduled(int OriginatingTaskSchedulerID, int OriginatingTaskID, int ContinueWithTaskId)
static void UnsafeQueueUserWorkItemInternal(object callBack, bool preferLocal)
delegate void ContextCallback(object? state)
delegate void Action()