Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ WriteAsync()

override ValueTask System.Threading.Channels.BoundedChannel< T >.BoundedChannelWriter.WriteAsync ( T item,
CancellationToken cancellationToken )
inlinevirtual

Reimplemented from System.Threading.Channels.ChannelWriter< T >.

Definition at line 330 of file BoundedChannel.cs.

331 {
332 if (cancellationToken.IsCancellationRequested)
333 {
335 }
338 BoundedChannel<T> parent = _parent;
339 bool lockTaken = false;
340 try
341 {
342 Monitor.Enter(parent.SyncObj, ref lockTaken);
343 if (parent._doneWriting != null)
344 {
345 return new ValueTask(Task.FromException(ChannelUtilities.CreateInvalidCompletionException(parent._doneWriting)));
346 }
347 int count = parent._items.Count;
348 if (count != 0)
349 {
350 if (count < parent._bufferedCapacity)
351 {
352 parent._items.EnqueueTail(item);
353 return default(ValueTask);
354 }
355 if (parent._mode == BoundedChannelFullMode.Wait)
356 {
357 if (!cancellationToken.CanBeCanceled)
358 {
360 if (writerSingleton.TryOwnAndReset())
361 {
362 writerSingleton.Item = item;
363 parent._blockedWriters.EnqueueTail(writerSingleton);
364 return writerSingleton.ValueTask;
365 }
366 }
368 voidAsyncOperationWithData.Item = item;
369 parent._blockedWriters.EnqueueTail(voidAsyncOperationWithData);
370 return voidAsyncOperationWithData.ValueTask;
371 }
372 if (parent._mode == BoundedChannelFullMode.DropWrite)
373 {
374 Monitor.Exit(parent.SyncObj);
375 lockTaken = false;
376 parent._itemDropped?.Invoke(item);
377 return default(ValueTask);
378 }
379 T obj = ((parent._mode == BoundedChannelFullMode.DropNewest) ? parent._items.DequeueTail() : parent._items.DequeueHead());
380 parent._items.EnqueueTail(item);
381 Monitor.Exit(parent.SyncObj);
382 lockTaken = false;
383 parent._itemDropped?.Invoke(obj);
384 return default(ValueTask);
385 }
386 while (!parent._blockedReaders.IsEmpty)
387 {
388 AsyncOperation<T> asyncOperation2 = parent._blockedReaders.DequeueHead();
389 if (asyncOperation2.UnregisterCancellation())
390 {
392 break;
393 }
394 }
395 if (asyncOperation == null)
396 {
397 parent._items.EnqueueTail(item);
398 listTail = parent._waitingReadersTail;
399 if (listTail == null)
400 {
401 return default(ValueTask);
402 }
403 parent._waitingReadersTail = null;
404 }
405 }
406 finally
407 {
408 if (lockTaken)
409 {
410 Monitor.Exit(parent.SyncObj);
411 }
412 }
413 if (asyncOperation != null)
414 {
415 bool flag = asyncOperation.TrySetResult(item);
416 }
417 else
418 {
419 ChannelUtilities.WakeUpWaiters(ref listTail, result: true);
420 }
421 return default(ValueTask);
422 }
readonly VoidAsyncOperationWithData< T > _writerSingleton
static Task FromException(Exception exception)
Definition Task.cs:3341
static Task FromCanceled(CancellationToken cancellationToken)
Definition Task.cs:3363

References System.Threading.Channels.BoundedChannel< T >.BoundedChannelWriter._parent, System.Threading.Channels.BoundedChannel< T >.BoundedChannelWriter._writerSingleton, System.cancellationToken, System.count, System.Collections.Generic.Dictionary< TKey, TValue >.Count, System.Threading.Channels.ChannelUtilities.CreateInvalidCompletionException(), System.Threading.Monitor.Enter(), System.Threading.Monitor.Exit(), System.Threading.Tasks.Task< TResult >.FromCanceled(), System.Threading.Tasks.Task< TResult >.FromException(), System.item, System.obj, and System.Threading.Channels.ChannelUtilities.WakeUpWaiters().