Skip to content

Commit

Permalink
Code documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pglombardo committed May 22, 2024
1 parent fbe51dd commit da5dd88
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Source/HiveMQtt/Client/internal/AwaitableQueueX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,45 @@ namespace HiveMQtt.Client.Internal;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
/// A queue that can be awaited for items to be enqueued.
/// </summary>
/// <typeparam name="T">The type of items to queue.</typeparam>
public class AwaitableQueueX<T> : IDisposable
{
/// <summary>
/// The semaphore used to signal when items are enqueued.
/// </summary>
private readonly SemaphoreSlim semaphore;

/// <summary>
/// The internal queue of items.
/// </summary>
private readonly ConcurrentQueue<T> queue;

/// <summary>
/// Initializes a new instance of the <see cref="AwaitableQueueX{T}"/> class.
/// </summary>
public AwaitableQueueX()
{
this.semaphore = new SemaphoreSlim(0);
this.queue = new ConcurrentQueue<T>();
}

/// <summary>
/// Enqueues an item.
/// </summary>
/// <param name="item">The item to enqueue.</param>
public void Enqueue(T item)
{
this.queue.Enqueue(item);
this.semaphore.Release();
}

/// <summary>
/// Enqueues a range of items.
/// </summary>
/// <param name="source">The items to enqueue.</param>
public void EnqueueRange(IEnumerable<T> source)
{
var n = 0;
Expand All @@ -34,6 +56,11 @@ public void EnqueueRange(IEnumerable<T> source)
this.semaphore.Release(n);
}

/// <summary>
/// Dequeues an item.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The dequeued item.</returns>
public async Task<T> DequeueAsync(CancellationToken cancellationToken)
{
while (true)
Expand All @@ -47,6 +74,9 @@ public async Task<T> DequeueAsync(CancellationToken cancellationToken)
}
}

/// <summary>
/// Clears the queue.
/// </summary>
public void Clear()
{
while (this.queue.TryDequeue(out _))
Expand All @@ -55,10 +85,18 @@ public void Clear()
}
}

/// <summary>
/// Gets the number of items in the queue.
/// </summary>
/// <value>The number of items in the queue.</value>
public int Count => this.queue.Count;

/// <summary>
/// Gets a value indicating whether the queue is empty.
/// </summary>
public bool IsEmpty => this.queue.IsEmpty;

/// <inheritdoc />
public void Dispose()
{
this.semaphore.Dispose();
Expand Down

0 comments on commit da5dd88

Please sign in to comment.