public override Task <bool> WaitToReadAsync(CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return(Task.FromCanceled <bool>(cancellationToken)); } BoundedChannel <T> parent = _parent; lock (parent.SyncObj) { parent.AssertInvariants(); // If there are any items available, a read is possible. if (!parent._items.IsEmpty) { return(ChannelUtilities.s_trueTask); } // There were no items available, so if we're done writing, a read will never be possible. if (parent._doneWriting != null) { return(parent._doneWriting != ChannelUtilities.s_doneWritingSentinel ? Task.FromException <bool>(parent._doneWriting) : ChannelUtilities.s_falseTask); } // There were no items available, but there could be in the future, so ensure // there's a blocked reader task and return it. return(ChannelUtilities.GetOrCreateWaiter(ref parent._waitingReaders, parent._runContinuationsAsynchronously, cancellationToken)); } }
public override bool TryRead(out T item) { BoundedChannel <T> parent = _parent; lock (parent.SyncObj) { parent.AssertInvariants(); // Get an item if there is one. if (!parent._items.IsEmpty) { item = DequeueItemAndPostProcess(); return(true); } } item = default(T); return(false); }