Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
WriteOnceBlock.cs
Go to the documentation of this file.
5
7
8[DebuggerDisplay("{DebuggerDisplayContent,nq}")]
11{
12 private sealed class DebugView
13 {
15
16 public bool IsCompleted => _writeOnceBlock.Completion.IsCompleted;
17
19
20 public bool HasValue => _writeOnceBlock.HasValue;
21
22 public T Value => _writeOnceBlock.Value;
23
25
27
32 }
33
35
36 private readonly Func<T, T> _cloningFunction;
37
39
41
43
44 private bool _completionReserved;
45
47
48 private T _value;
49
50 private object ValueLock => _targetRegistry;
51
53
65
66 private bool HasValue => _header.IsValid;
67
68 private T? Value
69 {
70 get
71 {
72 if (!_header.IsValid)
73 {
74 return default(T);
75 }
76 return _value;
77 }
78 }
79
80 private object DebuggerDisplayContent => $"{Common.GetNameForDebugger(this, _dataflowBlockOptions)}, HasValue={HasValue}, Value={Value}";
81
82 object IDebuggerDisplay.Content => DebuggerDisplayContent;
83
88
90 {
91 if (dataflowBlockOptions == null)
92 {
93 throw new ArgumentNullException("dataflowBlockOptions");
94 }
98 if (dataflowBlockOptions.CancellationToken.CanBeCanceled)
99 {
101 if (dataflowBlockOptions.CancellationToken.IsCancellationRequested)
102 {
104 _lazyCompletionTaskSource.SetCanceled();
105 }
106 else
107 {
109 {
110 ((WriteOnceBlock<T>)state).Complete();
111 }, this);
112 }
113 }
115 if (log.IsEnabled())
116 {
117 log.DataflowBlockCreated(this, dataflowBlockOptions);
118 }
119 }
120
122 {
123 if (exceptions == null)
124 {
125 Task task = new Task(delegate(object state)
126 {
127 ((WriteOnceBlock<T>)state).OfferToTargetsAndCompleteBlock();
130 if (log.IsEnabled())
131 {
132 log.TaskLaunchedForMessageHandling(this, task, DataflowEtwProvider.TaskLaunchedReason.OfferingOutputMessages, _header.IsValid ? 1 : 0);
133 }
135 if (ex != null)
136 {
138 }
139 }
140 else
141 {
142 Task.Factory.StartNew(delegate(object state)
143 {
145 tuple.Item1.CompleteBlock(tuple.Item2);
147 }
148 }
149
155
157 {
158 TargetRegistry<T>.LinkedTargetInfo firstTarget = _targetRegistry.ClearEntryPoints();
159 if (exceptions != null && exceptions.Count > 0)
160 {
161 CompletionTaskSource.TrySetException(exceptions);
162 }
164 {
165 CompletionTaskSource.TrySetCanceled();
166 }
168 {
169 _lazyCompletionTaskSource.TrySetResult(default(VoidResult));
170 }
171 _targetRegistry.PropagateCompletion(firstTarget);
173 if (log.IsEnabled())
174 {
175 log.DataflowBlockCompleted(this);
176 }
177 }
178
180 {
181 if (exception == null)
182 {
183 throw new ArgumentNullException("exception");
184 }
186 }
187
188 public void Complete()
189 {
191 }
192
194 {
195 bool flag = false;
197 {
199 {
200 return;
201 }
204 {
205 flag = (_completionReserved = true);
206 }
207 }
208 if (flag)
209 {
210 List<Exception> list = null;
211 if (exception != null)
212 {
213 list = new List<Exception>();
214 list.Add(exception);
215 }
217 }
218 }
219
221 {
222 if (_header.IsValid && (filter == null || filter(_value)))
223 {
225 return true;
226 }
227 item = default(T);
228 return false;
229 }
230
231 bool IReceivableSourceBlock<T>.TryReceiveAll([NotNullWhen(true)] out IList<T> items)
232 {
233 if (TryReceive(null, out var item))
234 {
235 items = new T[1] { item };
236 return true;
237 }
238 items = null;
239 return false;
240 }
241
243 {
244 if (target == null)
245 {
246 throw new ArgumentNullException("target");
247 }
248 if (linkOptions == null)
249 {
250 throw new ArgumentNullException("linkOptions");
251 }
252 bool hasValue;
254 {
255 hasValue = HasValue;
257 if (!hasValue && !completionReserved)
258 {
260 return Common.CreateUnlinker<T>(ValueLock, _targetRegistry, target);
261 }
262 }
263 if (hasValue)
264 {
265 bool consumeToAccept = _cloningFunction != null;
266 target.OfferMessage(_header, _value, this, consumeToAccept);
267 }
268 if (linkOptions.PropagateCompletion)
269 {
271 }
272 return Disposables.Nop;
273 }
274
276 {
277 if (!messageHeader.IsValid)
278 {
279 throw new ArgumentException(System.SR.Argument_InvalidMessageHeader, "messageHeader");
280 }
281 if (source == null && consumeToAccept)
282 {
284 }
285 bool flag = false;
287 {
289 {
290 return DataflowMessageStatus.DecliningPermanently;
291 }
292 if (consumeToAccept)
293 {
294 messageValue = source.ConsumeMessage(messageHeader, this, out var messageConsumed);
295 if (!messageConsumed)
296 {
297 return DataflowMessageStatus.NotAvailable;
298 }
299 }
304 {
305 flag = (_completionReserved = true);
306 }
307 }
308 if (flag)
309 {
310 CompleteBlockAsync(null);
311 }
312 return DataflowMessageStatus.Accepted;
313 }
314
316 {
317 if (!messageHeader.IsValid)
318 {
319 throw new ArgumentException(System.SR.Argument_InvalidMessageHeader, "messageHeader");
320 }
321 if (target == null)
322 {
323 throw new ArgumentNullException("target");
324 }
325 if (_header.Id == messageHeader.Id)
326 {
327 messageConsumed = true;
328 return CloneItem(_value);
329 }
330 messageConsumed = false;
331 return default(T);
332 }
333
335 {
336 if (!messageHeader.IsValid)
337 {
338 throw new ArgumentException(System.SR.Argument_InvalidMessageHeader, "messageHeader");
339 }
340 if (target == null)
341 {
342 throw new ArgumentNullException("target");
343 }
344 return _header.Id == messageHeader.Id;
345 }
346
348 {
349 if (!messageHeader.IsValid)
350 {
351 throw new ArgumentException(System.SR.Argument_InvalidMessageHeader, "messageHeader");
352 }
353 if (target == null)
354 {
355 throw new ArgumentNullException("target");
356 }
357 if (_header.Id != messageHeader.Id)
358 {
360 }
361 bool consumeToAccept = _cloningFunction != null;
362 target.OfferMessage(_header, _value, this, consumeToAccept);
363 }
364
365 private T CloneItem(T item)
366 {
367 if (_cloningFunction == null)
368 {
369 return item;
370 }
371 return _cloningFunction(item);
372 }
373
375 {
376 List<Exception> list = null;
377 if (HasValue)
378 {
379 TargetRegistry<T>.LinkedTargetInfo linkedTargetInfo = _targetRegistry.FirstTargetNode;
380 while (linkedTargetInfo != null)
381 {
382 TargetRegistry<T>.LinkedTargetInfo next = linkedTargetInfo.Next;
383 ITargetBlock<T> target = linkedTargetInfo.Target;
384 try
385 {
386 bool consumeToAccept = _cloningFunction != null;
387 target.OfferMessage(_header, _value, this, consumeToAccept);
388 }
389 catch (Exception ex)
390 {
391 Common.StoreDataflowMessageValueIntoExceptionData(ex, _value);
393 }
394 linkedTargetInfo = next;
395 }
396 }
397 return list;
398 }
399
400 public override string ToString()
401 {
403 }
404}
void Add(TKey key, TValue value)
static string Argument_InvalidMessageHeader
Definition SR.cs:24
static string InvalidOperation_MessageNotReservedByTarget
Definition SR.cs:34
static string Argument_CantConsumeFromANullSource
Definition SR.cs:22
Definition SR.cs:7
static int CompareExchange(ref int location1, int value, int comparand)
static readonly TaskCompletionSource< VoidResult > CompletedVoidResultTaskCompletionSource
Definition Common.cs:32
static Exception StartTaskSafe(Task task, TaskScheduler scheduler)
Definition Common.cs:277
static void PropagateCompletionOnceCompleted(Task sourceCompletionTask, IDataflowBlock target)
Definition Common.cs:357
static TaskCreationOptions GetCreationOptionsForTask(bool isReplacementReplica=false)
Definition Common.cs:267
static string GetNameForDebugger(IDataflowBlock block, DataflowBlockOptions options=null)
Definition Common.cs:66
static void AddException([NotNull] ref List< Exception > list, Exception exception, bool unwrapInnerExceptions=false)
Definition Common.cs:183
static readonly DataflowMessageHeader SingleMessageHeader
Definition Common.cs:26
static void WireCancellationToComplete(CancellationToken cancellationToken, Task completionTask, Action< object > completeAction, object completeState)
Definition Common.cs:93
static int GetBlockId(IDataflowBlock block)
Definition Common.cs:61
void CompleteBlockAsync(IList< Exception > exceptions)
void CompleteBlock(IList< Exception > exceptions)
WriteOnceBlock(Func< T, T >? cloningFunction)
TaskCompletionSource< VoidResult > _lazyCompletionTaskSource
TaskCompletionSource< VoidResult > CompletionTaskSource
IDisposable LinkTo(ITargetBlock< T > target, DataflowLinkOptions linkOptions)
void CompleteCore(Exception exception, bool storeExceptionEvenIfAlreadyCompleting)
WriteOnceBlock(Func< T, T >? cloningFunction, DataflowBlockOptions dataflowBlockOptions)
readonly DataflowBlockOptions _dataflowBlockOptions
bool TryReceive(Predicate< T >? filter, [MaybeNullWhen(false)] out T item)
readonly TargetRegistry< T > _targetRegistry
static new TaskFactory< TResult > Factory
Definition Task.cs:56