Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
AsyncTaskMethodBuilder.cs
Go to the documentation of this file.
6
8
9public struct AsyncTaskMethodBuilder
10{
12
13 public Task Task
14 {
15 [MethodImpl(MethodImplOptions.AggressiveInlining)]
16 get
17 {
19 }
20 }
21
23
25 {
26 return default(AsyncTaskMethodBuilder);
27 }
28
29 [MethodImpl(MethodImplOptions.AggressiveInlining)]
30 [DebuggerStepThrough]
31 public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
32 {
34 }
35
40
41 public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
42 {
43 AsyncTaskMethodBuilder<VoidTaskResult>.AwaitOnCompleted(ref awaiter, ref stateMachine, ref m_task);
44 }
45
46 [MethodImpl(MethodImplOptions.AggressiveInlining)]
47 public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
48 {
49 AsyncTaskMethodBuilder<VoidTaskResult>.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine, ref m_task);
50 }
51
52 [MethodImpl(MethodImplOptions.NoInlining)]
57
58 public void SetResult()
59 {
60 if (m_task == null)
61 {
63 }
64 else
65 {
67 }
68 }
69
74
79}
81{
82 private sealed class DebugFinalizableAsyncStateMachineBox<TStateMachine> : AsyncStateMachineBox<TStateMachine> where TStateMachine : IAsyncStateMachine
83 {
85 {
86 if (!base.IsCompleted)
87 {
88 TplEventSource.Log.IncompleteAsyncMethod(this);
89 }
90 }
91 }
92
93 private class AsyncStateMachineBox<TStateMachine> : Task<TResult>, IAsyncStateMachineBox where TStateMachine : IAsyncStateMachine
94 {
96
97 private Action _moveNextAction;
98
99 public TStateMachine StateMachine;
100
102
104
105 private static void ExecutionContextCallback(object s)
106 {
108 }
109
110 internal sealed override void ExecuteFromThreadPool(Thread threadPoolThread)
111 {
112 MoveNext(threadPoolThread);
113 }
114
115 public void MoveNext()
116 {
117 MoveNext(null);
118 }
119
120 private void MoveNext(Thread threadPoolThread)
121 {
122 bool flag = TplEventSource.Log.IsEnabled();
123 if (flag)
124 {
125 TplEventSource.Log.TraceSynchronousWorkBegin(base.Id, CausalitySynchronousWork.Execution);
126 }
127 ExecutionContext context = Context;
128 if (context == null)
129 {
130 StateMachine.MoveNext();
131 }
132 else if (threadPoolThread == null)
133 {
135 }
136 else
137 {
138 ExecutionContext.RunFromThreadPoolDispatchLoop(threadPoolThread, context, s_callback, this);
139 }
140 if (base.IsCompleted)
141 {
143 }
144 if (flag)
145 {
146 TplEventSource.Log.TraceSynchronousWorkEnd(CausalitySynchronousWork.Execution);
147 }
148 }
149
150 [MethodImpl(MethodImplOptions.AggressiveInlining)]
152 {
154 {
156 }
157 StateMachine = default(TStateMachine);
158 Context = null;
160 {
161 GC.SuppressFinalize(this);
162 }
163 }
164
169 }
170
172
174 {
175 [MethodImpl(MethodImplOptions.AggressiveInlining)]
176 get
177 {
179 }
180 }
181
183
185 {
186 return default(AsyncTaskMethodBuilder<TResult>);
187 }
188
189 [MethodImpl(MethodImplOptions.AggressiveInlining)]
190 [DebuggerStepThrough]
191 public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
192 {
194 }
195
200
201 public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
202 {
203 AwaitOnCompleted(ref awaiter, ref stateMachine, ref m_task);
204 }
205
206 internal static void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine, ref Task<TResult> taskField) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
207 {
208 try
209 {
210 awaiter.OnCompleted(GetStateMachineBox(ref stateMachine, ref taskField).MoveNextAction);
211 }
212 catch (Exception exception)
213 {
215 }
216 }
217
218 [MethodImpl(MethodImplOptions.AggressiveInlining)]
219 public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
220 {
221 AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine, ref m_task);
222 }
223
224 [MethodImpl(MethodImplOptions.AggressiveInlining)]
225 internal static void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine, [NotNull] ref Task<TResult> taskField) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
226 {
227 IAsyncStateMachineBox stateMachineBox = GetStateMachineBox(ref stateMachine, ref taskField);
228 AwaitUnsafeOnCompleted(ref awaiter, stateMachineBox);
229 }
230
231 [MethodImpl(MethodImplOptions.AggressiveOptimization)]
232 internal static void AwaitUnsafeOnCompleted<TAwaiter>(ref TAwaiter awaiter, IAsyncStateMachineBox box) where TAwaiter : ICriticalNotifyCompletion
233 {
234 if (default(TAwaiter) != null && awaiter is ITaskAwaiter)
235 {
236 TaskAwaiter.UnsafeOnCompletedInternal(Unsafe.As<TAwaiter, TaskAwaiter>(ref awaiter).m_task, box, continueOnCapturedContext: true);
237 return;
238 }
239 if (default(TAwaiter) != null && awaiter is IConfiguredTaskAwaiter)
240 {
242 TaskAwaiter.UnsafeOnCompletedInternal(reference.m_task, box, reference.m_continueOnCapturedContext);
243 return;
244 }
245 if (default(TAwaiter) != null && awaiter is IStateMachineBoxAwareAwaiter)
246 {
247 try
248 {
249 ((IStateMachineBoxAwareAwaiter)(object)awaiter).AwaitUnsafeOnCompleted(box);
250 return;
251 }
252 catch (Exception exception)
253 {
255 return;
256 }
257 }
258 try
259 {
260 awaiter.UnsafeOnCompleted(box.MoveNextAction);
261 }
262 catch (Exception exception2)
263 {
264 System.Threading.Tasks.Task.ThrowAsync(exception2, null);
265 }
266 }
267
268 private static IAsyncStateMachineBox GetStateMachineBox<TStateMachine>(ref TStateMachine stateMachine, [NotNull] ref Task<TResult> taskField) where TStateMachine : IAsyncStateMachine
269 {
270 ExecutionContext executionContext = ExecutionContext.Capture();
271 if (taskField is AsyncStateMachineBox<TStateMachine> asyncStateMachineBox)
272 {
273 if (asyncStateMachineBox.Context != executionContext)
274 {
275 asyncStateMachineBox.Context = executionContext;
276 }
277 return asyncStateMachineBox;
278 }
279 if (taskField is AsyncStateMachineBox<IAsyncStateMachine> asyncStateMachineBox2)
280 {
281 if (asyncStateMachineBox2.StateMachine == null)
282 {
284 asyncStateMachineBox2.StateMachine = stateMachine;
285 }
286 asyncStateMachineBox2.Context = executionContext;
287 return asyncStateMachineBox2;
288 }
291 asyncStateMachineBox3.StateMachine = stateMachine;
292 asyncStateMachineBox3.Context = executionContext;
293 if (TplEventSource.Log.IsEnabled())
294 {
295 TplEventSource.Log.TraceOperationBegin(asyncStateMachineBox3.Id, "Async: " + stateMachine.GetType().Name, 0L);
296 }
298 {
299 System.Threading.Tasks.Task.AddToActiveTasks(asyncStateMachineBox3);
300 }
301 return asyncStateMachineBox3;
302 }
303
304 [MethodImpl(MethodImplOptions.NoInlining)]
309
310 [MethodImpl(MethodImplOptions.NoInlining)]
312 {
313 return m_task = new Task<TResult>();
314 }
315
317 {
319 {
321 }
322 return CreateDebugFinalizableAsyncStateMachineBox<IAsyncStateMachine>();
323 }
324
325 public void SetResult(TResult result)
326 {
327 if (m_task == null)
328 {
329 m_task = System.Threading.Tasks.Task.FromResult(result);
330 }
331 else
332 {
334 }
335 }
336
337 internal static void SetExistingTaskResult(Task<TResult> task, TResult result)
338 {
339 if (TplEventSource.Log.IsEnabled())
340 {
341 TplEventSource.Log.TraceOperationEnd(task.Id, AsyncCausalityStatus.Completed);
342 }
343 if (!task.TrySetResult(result))
344 {
345 ThrowHelper.ThrowInvalidOperationException(ExceptionResource.TaskT_TransitionToFinal_AlreadyCompleted);
346 }
347 }
348
350 {
352 }
353
354 internal static void SetException(Exception exception, ref Task<TResult> taskField)
355 {
356 if (exception == null)
357 {
359 }
360 Task<TResult> task = taskField ?? (taskField = new Task<TResult>());
361 if (!((exception is OperationCanceledException ex) ? task.TrySetCanceled(ex.CancellationToken, ex) : task.TrySetException(exception)))
362 {
363 ThrowHelper.ThrowInvalidOperationException(ExceptionResource.TaskT_TransitionToFinal_AlreadyCompleted);
364 }
365 }
366
367 internal void SetNotificationForWaitCompletion(bool enabled)
368 {
370 }
371
372 internal static void SetNotificationForWaitCompletion(bool enabled, [NotNull] ref Task<TResult> taskField)
373 {
374 (taskField ?? (taskField = CreateWeaklyTypedStateMachineBox())).SetNotificationForWaitCompletion(enabled);
375 }
376}
static void NotifyOfCrossThreadDependency()
Definition Debugger.cs:44
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
static void SetStateMachine(IAsyncStateMachine stateMachine, Task task)
static void RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
static ? ExecutionContext Capture()
static void RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, object state)
static bool s_asyncDebuggingEnabled
Definition Task.cs:960
static readonly Task< VoidTaskResult > s_cachedCompleted
Definition Task.cs:966
static void ThrowAsync(Exception exception, SynchronizationContext targetContext)
Definition Task.cs:1806
static bool AddToActiveTasks(Task task)
Definition Task.cs:1186
static void RemoveFromActiveTasks(Task task)
Definition Task.cs:1197
static readonly TplEventSource Log
static void ThrowInvalidOperationException()
static void ThrowArgumentNullException(string name)
delegate void ContextCallback(object? state)
static AsyncTaskMethodBuilder< TResult > Create()
static IAsyncStateMachineBox GetStateMachineBox< TStateMachine >(ref TStateMachine stateMachine, [NotNull] ref Task< TResult > taskField)
void Start< TStateMachine >(ref TStateMachine stateMachine)
static void SetNotificationForWaitCompletion(bool enabled, [NotNull] ref Task< TResult > taskField)
static void SetException(Exception exception, ref Task< TResult > taskField)
static void AwaitUnsafeOnCompleted< TAwaiter >(ref TAwaiter awaiter, IAsyncStateMachineBox box)
void AwaitUnsafeOnCompleted< TAwaiter, TStateMachine >(ref TAwaiter awaiter, ref TStateMachine stateMachine)
static AsyncStateMachineBox< TStateMachine > CreateDebugFinalizableAsyncStateMachineBox< TStateMachine >()
void AwaitOnCompleted< TAwaiter, TStateMachine >(ref TAwaiter awaiter, ref TStateMachine stateMachine)
static void SetExistingTaskResult(Task< TResult > task, TResult result)
static void UnsafeOnCompletedInternal(Task task, IAsyncStateMachineBox stateMachineBox, bool continueOnCapturedContext)