/// Synchronously marks the producer/consumer queue as complete for adding.
/// </summary>
publicvoidCompleteAdding()
{
using(_mutex.Lock())
{
if(_completed.IsCancellationRequested)
return;
_completed.Cancel();
_completedOrNotEmpty.NotifyAll();
}
}
/// <summary>
/// Attempts to enqueue an item.
/// </summary>
/// <param name="item">The item to enqueue.</param>
/// <param name="cancellationToken">A cancellation token that can be used to abort the enqueue operation. If <paramref name="abort"/> is not <c>null</c>, then this token must include signals from the <paramref name="abort"/> object.</param>
/// <param name="abort">A synchronization object used to cancel related enqueue operations. May be <c>null</c> if this is the only enqueue operation.</param>
// Explicitly check whether the queue has been marked complete to prevent a race condition where notFull is signalled at the same time the queue is marked complete.
if(_completed.IsCancellationRequested)
returnnull;
// Set the abort signal. If another queue has already set the abort signal, then abort.
if(abort!=null&&!abort.TrySetCanceled())
returnnull;
_queue.Enqueue(item);
_completedOrNotEmpty.Notify();
returnthis;
}
}
catch(OperationCanceledException)
{
returnnull;
}
}
/// <summary>
/// Attempts to enqueue an item. This method may block the calling thread.
/// </summary>
/// <param name="item">The item to enqueue.</param>
/// <param name="cancellationToken">A cancellation token that can be used to abort the enqueue operation.</param>
// Explicitly check whether the queue has been marked complete to prevent a race condition where notFull is signalled at the same time the queue is marked complete.
if(_completed.IsCancellationRequested)
returnnull;
_queue.Enqueue(item);
_completedOrNotEmpty.Notify();
returnthis;
}
}
catch(OperationCanceledException)
{
returnnull;
}
}
/// <summary>
/// Attempts to enqueue an item to the producer/consumer queue. Returns <c>false</c> if the producer/consumer queue has completed adding.
/// </summary>
/// <param name="item">The item to enqueue.</param>
/// <param name="cancellationToken">A cancellation token that can be used to abort the enqueue operation.</param>
/// Attempts to enqueue an item to the producer/consumer queue. Returns <c>false</c> if the producer/consumer queue has completed adding. This method may block the calling thread.
/// </summary>
/// <param name="item">The item to enqueue.</param>
/// <param name="cancellationToken">A cancellation token that can be used to abort the enqueue operation.</param>
/// Attempts to enqueue an item to the producer/consumer queue. Returns <c>false</c> if the producer/consumer queue has completed adding. This method may block the calling thread.
/// </summary>
/// <param name="item">The item to enqueue.</param>
publicboolTryEnqueue(Titem)
{
returnTryEnqueue(item,CancellationToken.None);
}
/// <summary>
/// Enqueues an item to the producer/consumer queue. Throws <see cref="InvalidOperationException"/> if the producer/consumer queue has completed adding.
/// </summary>
/// <param name="item">The item to enqueue.</param>
/// <param name="cancellationToken">A cancellation token that can be used to abort the enqueue operation.</param>
thrownewInvalidOperationException("Enqueue failed; the producer/consumer queue has completed adding.");
}
/// <summary>
/// Enqueues an item to the producer/consumer queue. Throws <see cref="InvalidOperationException"/> if the producer/consumer queue has completed adding. This method may block the calling thread.
/// </summary>
/// <param name="item">The item to enqueue.</param>
/// <param name="cancellationToken">A cancellation token that can be used to abort the enqueue operation.</param>
thrownewInvalidOperationException("Enqueue failed; the producer/consumer queue has completed adding.");
}
/// <summary>
/// Enqueues an item to the producer/consumer queue. Throws <see cref="InvalidOperationException"/> if the producer/consumer queue has completed adding.
/// </summary>
/// <param name="item">The item to enqueue.</param>
publicTaskEnqueueAsync(Titem)
{
returnEnqueueAsync(item,CancellationToken.None);
}
/// <summary>
/// Enqueues an item to the producer/consumer queue. This method may block the calling thread. Throws <see cref="InvalidOperationException"/> if the producer/consumer queue has completed adding.
/// </summary>
/// <param name="item">The item to enqueue.</param>
publicvoidEnqueue(Titem)
{
Enqueue(item,CancellationToken.None);
}
/// <summary>
/// Asynchronously waits until an item is available to dequeue. Returns <c>false</c> if the producer/consumer queue has completed adding and there are no more items.
/// </summary>
/// <param name="cancellationToken">A cancellation token that can be used to abort the asynchronous wait.</param>
/// Asynchronously waits until an item is available to dequeue. Returns <c>false</c> if the producer/consumer queue has completed adding and there are no more items.
/// <param name="cancellationToken">A cancellation token that can be used to abort the dequeue operation. If <paramref name="abort"/> is not <c>null</c>, then this token must include signals from the <paramref name="abort"/> object.</param>
/// <param name="abort">A synchronization object used to cancel related dequeue operations. May be <c>null</c> if this is the only dequeue operation.</param>
/// Attempts to dequeue an item from the producer/consumer queue.
/// </summary>
publicTask<DequeueResult>TryDequeueAsync()
{
returnTryDequeueAsync(CancellationToken.None);
}
/// <summary>
/// Attempts to dequeue an item from the producer/consumer queue. This method may block the calling thread.
/// </summary>
publicDequeueResultTryDequeue()
{
returnTryDequeue(CancellationToken.None);
}
/// <summary>
/// Dequeues an item from the producer/consumer queue. Returns the dequeued item. Throws <see cref="InvalidOperationException"/> if the producer/consumer queue has completed adding and is empty.
/// </summary>
/// <param name="cancellationToken">A cancellation token that can be used to abort the dequeue operation.</param>
thrownewInvalidOperationException("Dequeue failed; the producer/consumer queue has completed adding and is empty.");
returnret.Item;
}
/// <summary>
/// Dequeues an item from the producer/consumer queue. Returns the dequeued item. This method may block the calling thread. Throws <see cref="InvalidOperationException"/> if the producer/consumer queue has completed adding and is empty.
/// </summary>
/// <param name="cancellationToken">A cancellation token that can be used to abort the dequeue operation.</param>
thrownewInvalidOperationException("Dequeue failed; the producer/consumer queue has completed adding and is empty.");
returnret.Item;
}
/// <summary>
/// Dequeues an item from the producer/consumer queue. Returns the dequeued item. Throws <see cref="InvalidOperationException"/> if the producer/consumer queue has completed adding and is empty.
/// </summary>
/// <returns>The dequeued item.</returns>
publicTask<T>DequeueAsync()
{
returnDequeueAsync(CancellationToken.None);
}
/// <summary>
/// Dequeues an item from the producer/consumer queue. Returns the dequeued item. This method may block the calling thread. Throws <see cref="InvalidOperationException"/> if the producer/consumer queue has completed adding and is empty.
/// </summary>
/// <returns>The dequeued item.</returns>
publicTDequeue()
{
returnDequeue(CancellationToken.None);
}
/// <summary>
/// The result of a <c>TryDequeue</c>, <c>DequeueFromAny</c>, or <c>TryDequeueFromAny</c> operation.
/// Attempts to enqueue an item to any of a number of producer/consumer queues. Returns the producer/consumer queue that received the item. Returns <c>null</c> if all producer/consumer queues have completed adding.
/// Attempts to enqueue an item to any of a number of producer/consumer queues. Returns the producer/consumer queue that received the item. Returns <c>null</c> if all producer/consumer queues have completed adding. This method may block the calling thread.
/// Attempts to enqueue an item to any of a number of producer/consumer queues. Returns the producer/consumer queue that received the item. Returns <c>null</c> if all producer/consumer queues have completed adding.
/// Attempts to enqueue an item to any of a number of producer/consumer queues. Returns the producer/consumer queue that received the item. Returns <c>null</c> if all producer/consumer queues have completed adding. This method may block the calling thread.
/// Enqueues an item to any of a number of producer/consumer queues. Returns the producer/consumer queue that received the item. Throws <see cref="InvalidOperationException"/> if all producer/consumer queues have completed adding.
thrownewInvalidOperationException("Enqueue failed; all producer/consumer queues have completed adding.");
returnret;
}
/// <summary>
/// Enqueues an item to any of a number of producer/consumer queues. Returns the producer/consumer queue that received the item. Throws <see cref="InvalidOperationException"/> if all producer/consumer queues have completed adding. This method may block the calling thread.
thrownewInvalidOperationException("Enqueue failed; all producer/consumer queues have completed adding.");
returnret;
}
/// <summary>
/// Enqueues an item to any of a number of producer/consumer queues. Returns the producer/consumer queue that received the item. Throws <see cref="InvalidOperationException"/> if all producer/consumer queues have completed adding.
/// Enqueues an item to any of a number of producer/consumer queues. Returns the producer/consumer queue that received the item. Throws <see cref="InvalidOperationException"/> if all producer/consumer queues have completed adding. This method may block the calling thread.
/// Attempts to dequeue an item from any of a number of producer/consumer queues. The operation "fails" if all the producer/consumer queues have completed adding and are empty.
/// Attempts to dequeue an item from any of a number of producer/consumer queues. The operation "fails" if all the producer/consumer queues have completed adding and are empty. This method may block the calling thread.
/// Attempts to dequeue an item from any of a number of producer/consumer queues. The operation "fails" if all the producer/consumer queues have completed adding and are empty.
/// Attempts to dequeue an item from any of a number of producer/consumer queues. The operation "fails" if all the producer/consumer queues have completed adding and are empty. This method may block the calling thread.
/// Dequeues an item from any of a number of producer/consumer queues. Throws <see cref="InvalidOperationException"/> if all the producer/consumer queues have completed adding and are empty.
thrownewInvalidOperationException("Dequeue failed; all producer/consumer queues have completed adding and are empty.");
returnret;
}
/// <summary>
/// Dequeues an item from any of a number of producer/consumer queues. Throws <see cref="InvalidOperationException"/> if all the producer/consumer queues have completed adding and are empty. This method may block the calling thread.
thrownewInvalidOperationException("Dequeue failed; all producer/consumer queues have completed adding and are empty.");
returnret;
}
/// <summary>
/// Dequeues an item from any of a number of producer/consumer queues. Throws <see cref="InvalidOperationException"/> if all the producer/consumer queues have completed adding and are empty.
/// Dequeues an item from any of a number of producer/consumer queues. Throws <see cref="InvalidOperationException"/> if all the producer/consumer queues have completed adding and are empty. This method may block the calling thread.