Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ManualResetValueTaskSourceCore.cs
Go to the documentation of this file.
4
6
7[StructLayout(LayoutKind.Auto)]
9{
10 private Action<object> _continuation;
11
12 private object _continuationState;
13
15
16 private object _capturedContext;
17
18 private bool _completed;
19
21
23
24 private short _version;
25
26 public bool RunContinuationsAsynchronously { get; set; }
27
28 public short Version => _version;
29
30 public void Reset()
31 {
32 _version++;
33 _completed = false;
34 _result = default(TResult);
35 _error = null;
36 _executionContext = null;
37 _capturedContext = null;
38 _continuation = null;
39 _continuationState = null;
40 }
41
42 public void SetResult(TResult result)
43 {
44 _result = result;
46 }
47
48 public void SetException(Exception error)
49 {
52 }
53
54 public ValueTaskSourceStatus GetStatus(short token)
55 {
56 ValidateToken(token);
57 if (_continuation != null && _completed)
58 {
59 if (_error != null)
60 {
62 {
63 return ValueTaskSourceStatus.Faulted;
64 }
65 return ValueTaskSourceStatus.Canceled;
66 }
67 return ValueTaskSourceStatus.Succeeded;
68 }
69 return ValueTaskSourceStatus.Pending;
70 }
71
72 [StackTraceHidden]
73 public TResult GetResult(short token)
74 {
75 if (token != _version || !_completed || _error != null)
76 {
78 }
79 return _result;
80 }
81
82 [StackTraceHidden]
83 private void ThrowForFailedGetResult(short token)
84 {
85 if (token != _version || !_completed)
86 {
88 }
89 _error?.Throw();
90 }
91
92 public void OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags)
93 {
94 if (continuation == null)
95 {
96 throw new ArgumentNullException("continuation");
97 }
98 ValidateToken(token);
99 if ((flags & ValueTaskSourceOnCompletedFlags.FlowExecutionContext) != 0)
100 {
102 }
103 if ((flags & ValueTaskSourceOnCompletedFlags.UseSchedulingContext) != 0)
104 {
106 if (current != null && current.GetType() != typeof(SynchronizationContext))
107 {
108 _capturedContext = current;
109 }
110 else
111 {
113 if (current2 != TaskScheduler.Default)
114 {
115 _capturedContext = current2;
116 }
117 }
118 }
119 object obj = _continuation;
120 if (obj == null)
121 {
123 obj = Interlocked.CompareExchange(ref _continuation, continuation, null);
124 }
125 if (obj == null)
126 {
127 return;
128 }
130 {
132 }
133 object capturedContext = _capturedContext;
134 if (capturedContext != null)
135 {
136 if (!(capturedContext is SynchronizationContext synchronizationContext))
137 {
138 if (capturedContext is TaskScheduler scheduler)
139 {
140 Task.Factory.StartNew(continuation, state, CancellationToken.None, TaskCreationOptions.DenyChildAttach, scheduler);
141 }
142 }
143 else
144 {
145 synchronizationContext.Post(delegate(object s)
146 {
147 TupleSlim<Action<object>, object> tupleSlim = (TupleSlim<Action<object>, object>)s;
148 tupleSlim.Item1(tupleSlim.Item2);
149 }, new TupleSlim<Action<object>, object>(continuation, state));
150 }
151 }
152 else if (_executionContext != null)
153 {
154 ThreadPool.QueueUserWorkItem<object>(continuation, state, preferLocal: true);
155 }
156 else
157 {
158 ThreadPool.UnsafeQueueUserWorkItem<object>(continuation, state, preferLocal: true);
159 }
160 }
161
162 private void ValidateToken(short token)
163 {
164 if (token != _version)
165 {
167 }
168 }
169
170 private void SignalCompletion()
171 {
172 if (_completed)
173 {
175 }
176 _completed = true;
178 {
179 return;
180 }
181 if (_executionContext == null)
182 {
183 if (_capturedContext == null)
184 {
186 {
188 }
189 else
190 {
192 }
193 }
194 else
195 {
197 }
198 }
199 else
200 {
202 }
203 }
204
206 {
209 if (_capturedContext == null)
210 {
212 {
213 try
214 {
216 return;
217 }
218 finally
219 {
220 ExecutionContext.RestoreInternal(executionContext);
221 }
222 }
223 ExceptionDispatchInfo exceptionDispatchInfo = null;
225 try
226 {
228 }
229 catch (Exception source)
230 {
231 exceptionDispatchInfo = ExceptionDispatchInfo.Capture(source);
232 }
233 finally
234 {
236 ExecutionContext.RestoreInternal(executionContext);
237 }
238 exceptionDispatchInfo?.Throw();
239 return;
240 }
241 try
242 {
244 }
245 finally
246 {
247 ExecutionContext.RestoreInternal(executionContext);
248 }
249 }
250
252 {
253 object capturedContext = _capturedContext;
254 if (!(capturedContext is SynchronizationContext synchronizationContext))
255 {
256 if (capturedContext is TaskScheduler scheduler)
257 {
259 }
260 }
261 else
262 {
263 synchronizationContext.Post(delegate(object s)
264 {
265 TupleSlim<Action<object>, object> tupleSlim = (TupleSlim<Action<object>, object>)s;
266 tupleSlim.Item1(tupleSlim.Item2);
268 }
269 }
270}
static ExceptionDispatchInfo Capture(Exception source)
static ExecutionContext CaptureForRestore()
static void RestoreInternal(ExecutionContext executionContext)
static void Restore(ExecutionContext executionContext)
static ? ExecutionContext Capture()
static int CompareExchange(ref int location1, int value, int comparand)
static ? SynchronizationContext Current
static void SetSynchronizationContext(SynchronizationContext? syncContext)
static new TaskFactory< TResult > Factory
Definition Task.cs:56
static bool QueueUserWorkItem(WaitCallback callBack)
static bool UnsafeQueueUserWorkItem(WaitCallback callBack, object? state)
static void ThrowInvalidOperationException()
readonly T2 Item2
Definition TupleSlim.cs:7
readonly T1 Item1
Definition TupleSlim.cs:5
void OnCompleted(Action< object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags)