Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
PoolingAsyncValueTaskMethodBuilder.cs
Go to the documentation of this file.
7using Internal;
9
11
12[StructLayout(LayoutKind.Auto)]
13public struct PoolingAsyncValueTaskMethodBuilder
14{
16
18
20 {
21 get
22 {
24 {
25 return default(ValueTask);
26 }
28 return new ValueTask(stateMachineBox, stateMachineBox.Version);
29 }
30 }
31
36
37 [MethodImpl(MethodImplOptions.AggressiveInlining)]
38 public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
39 {
41 }
42
47
48 public void SetResult()
49 {
50 if (m_task == null)
51 {
53 }
54 else
55 {
57 }
58 }
59
64
65 public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
66 {
67 PoolingAsyncValueTaskMethodBuilder<VoidTaskResult>.AwaitOnCompleted(ref awaiter, ref stateMachine, ref m_task);
68 }
69
70 [MethodImpl(MethodImplOptions.AggressiveInlining)]
71 public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
72 {
73 PoolingAsyncValueTaskMethodBuilder<VoidTaskResult>.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine, ref m_task);
74 }
75}
76[StructLayout(LayoutKind.Auto)]
78{
79 internal abstract class StateMachineBox : IValueTaskSource<TResult>, IValueTaskSource
80 {
81 protected Action _moveNextAction;
82
84
86
87 public short Version => _valueTaskSource.Version;
88
89 public void SetResult(TResult result)
90 {
91 _valueTaskSource.SetResult(result);
92 }
93
94 public void SetException(Exception error)
95 {
96 _valueTaskSource.SetException(error);
97 }
98
99 public ValueTaskSourceStatus GetStatus(short token)
100 {
101 return _valueTaskSource.GetStatus(token);
102 }
103
104 public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
105 {
106 _valueTaskSource.OnCompleted(continuation, state, token, flags);
107 }
108
113
114 void IValueTaskSource.GetResult(short token)
115 {
117 }
118 }
119
121 {
123 {
124 SetResult(default(TResult));
125 }
126 }
127
129 {
131
133
134 [ThreadStatic]
136
137 public TStateMachine StateMachine;
138
140 {
141 [MethodImpl(MethodImplOptions.AggressiveInlining)]
142 get
143 {
144 int num = (int)((uint)Thread.GetCurrentProcessorId() % (uint)Environment.ProcessorCount);
145 return ref Unsafe.As<object, StateMachineBox<TStateMachine>>(ref s_perCoreCache[num].Object);
146 }
147 }
148
150
151 [MethodImpl(MethodImplOptions.AggressiveInlining)]
153 {
155 if (stateMachineBox != null)
156 {
157 t_tlsCache = null;
158 }
159 else
160 {
161 ref StateMachineBox<TStateMachine> perCoreCacheSlot = ref PerCoreCacheSlot;
162 if (perCoreCacheSlot == null || (stateMachineBox = Interlocked.Exchange(ref perCoreCacheSlot, null)) == null)
163 {
164 stateMachineBox = new StateMachineBox<TStateMachine>();
165 }
166 }
167 return stateMachineBox;
168 }
169
170 [MethodImpl(MethodImplOptions.AggressiveInlining)]
171 private void ReturnToCache()
172 {
174 _valueTaskSource.Reset();
175 if (t_tlsCache == null)
176 {
177 t_tlsCache = this;
178 return;
179 }
180 ref StateMachineBox<TStateMachine> perCoreCacheSlot = ref PerCoreCacheSlot;
181 if (perCoreCacheSlot == null)
182 {
183 Volatile.Write(ref perCoreCacheSlot, this);
184 }
185 }
186
187 [MethodImpl(MethodImplOptions.AggressiveInlining)]
189 {
190 StateMachine = default(TStateMachine);
191 Context = null;
192 }
193
194 private static void ExecutionContextCallback(object s)
195 {
197 }
198
200 {
201 MoveNext();
202 }
203
204 public void MoveNext()
205 {
206 ExecutionContext context = Context;
207 if (context == null)
208 {
209 StateMachine.MoveNext();
210 }
211 else
212 {
214 }
215 }
216
218 {
219 try
220 {
221 return _valueTaskSource.GetResult(token);
222 }
223 finally
224 {
226 }
227 }
228
229 void IValueTaskSource.GetResult(short token)
230 {
231 try
232 {
233 _valueTaskSource.GetResult(token);
234 }
235 finally
236 {
238 }
239 }
240
245 }
246
248
250
252
254 {
255 get
256 {
258 {
259 return new ValueTask<TResult>(_result);
260 }
262 return new ValueTask<TResult>(stateMachineBox, stateMachineBox.Version);
263 }
264 }
265
270
271 [MethodImpl(MethodImplOptions.AggressiveInlining)]
272 public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
273 {
275 }
276
281
282 public void SetResult(TResult result)
283 {
284 if (m_task == null)
285 {
286 _result = result;
288 }
289 else
290 {
291 m_task.SetResult(result);
292 }
293 }
294
296 {
298 }
299
300 internal static void SetException(Exception exception, [NotNull] ref StateMachineBox boxFieldRef)
301 {
302 if (exception == null)
303 {
305 }
306 (boxFieldRef ?? (boxFieldRef = CreateWeaklyTypedStateMachineBox())).SetException(exception);
307 }
308
309 public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
310 {
311 AwaitOnCompleted(ref awaiter, ref stateMachine, ref m_task);
312 }
313
314 internal static void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine, ref StateMachineBox box) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
315 {
316 try
317 {
318 awaiter.OnCompleted(GetStateMachineBox(ref stateMachine, ref box).MoveNextAction);
319 }
320 catch (Exception exception)
321 {
323 }
324 }
325
326 [MethodImpl(MethodImplOptions.AggressiveInlining)]
327 public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
328 {
329 AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine, ref m_task);
330 }
331
332 [MethodImpl(MethodImplOptions.AggressiveInlining)]
333 internal static void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine, [NotNull] ref StateMachineBox boxRef) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
334 {
335 IAsyncStateMachineBox stateMachineBox = GetStateMachineBox(ref stateMachine, ref boxRef);
336 AsyncTaskMethodBuilder<VoidTaskResult>.AwaitUnsafeOnCompleted(ref awaiter, stateMachineBox);
337 }
338
339 private static IAsyncStateMachineBox GetStateMachineBox<TStateMachine>(ref TStateMachine stateMachine, [NotNull] ref StateMachineBox boxFieldRef) where TStateMachine : IAsyncStateMachine
340 {
341 ExecutionContext executionContext = ExecutionContext.Capture();
342 if (boxFieldRef is StateMachineBox<TStateMachine> stateMachineBox)
343 {
344 if (stateMachineBox.Context != executionContext)
345 {
346 stateMachineBox.Context = executionContext;
347 }
348 return stateMachineBox;
349 }
350 if (boxFieldRef is StateMachineBox<IAsyncStateMachine> stateMachineBox2)
351 {
352 if (stateMachineBox2.StateMachine == null)
353 {
355 stateMachineBox2.StateMachine = stateMachine;
356 }
357 stateMachineBox2.Context = executionContext;
358 return stateMachineBox2;
359 }
361 StateMachineBox<TStateMachine> stateMachineBox3 = (StateMachineBox<TStateMachine>)(boxFieldRef = StateMachineBox<TStateMachine>.RentFromCache());
362 stateMachineBox3.StateMachine = stateMachine;
363 stateMachineBox3.Context = executionContext;
364 return stateMachineBox3;
365 }
366
371}
static void NotifyOfCrossThreadDependency()
Definition Debugger.cs:44
static int ProcessorCount
static Exception ByDesign
static void SetStateMachine(IAsyncStateMachine stateMachine, Task task)
void OnCompleted(Action< object > continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
static void RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
static ? ExecutionContext Capture()
static int Exchange(ref int location1, int value)
static void ThrowAsync(Exception exception, SynchronizationContext targetContext)
Definition Task.cs:1806
static int GetCurrentProcessorId()
Definition Thread.cs:501
static void Write(ref bool location, bool value)
Definition Volatile.cs:74
static void ThrowArgumentNullException(string name)
delegate void ContextCallback(object? state)
static readonly PoolingAsyncValueTaskMethodBuilder< VoidTaskResult >.StateMachineBox s_syncSuccessSentinel
void AwaitUnsafeOnCompleted< TAwaiter, TStateMachine >(ref TAwaiter awaiter, ref TStateMachine stateMachine)
static IAsyncStateMachineBox GetStateMachineBox< TStateMachine >(ref TStateMachine stateMachine, [NotNull] ref StateMachineBox boxFieldRef)
static void SetException(Exception exception, [NotNull] ref StateMachineBox boxFieldRef)
PoolingAsyncValueTaskMethodBuilder< VoidTaskResult >.StateMachineBox m_task
void AwaitOnCompleted< TAwaiter, TStateMachine >(ref TAwaiter awaiter, ref TStateMachine stateMachine)