Add queue with async support to free up threads.

This commit is contained in:
Christian Kratky
2019-03-22 23:12:37 +01:00
parent 7ed5580352
commit 11a5b5a080
5 changed files with 111 additions and 99 deletions
@@ -0,0 +1,39 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
namespace MQTTnet.Internal
{
public sealed class AsyncQueue<TItem> : IDisposable
{
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);
private readonly ConcurrentQueue<TItem> _queue = new ConcurrentQueue<TItem>();
public void Enqueue(TItem item)
{
_queue.Enqueue(item);
_semaphore.Release();
}
public async Task<TItem> DequeueAsync(CancellationToken cancellationToken)
{
while (true)
{
await _semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
if (_queue.TryDequeue(out var item))
{
return item;
}
}
}
public void Dispose()
{
_semaphore?.Dispose();
}
}
}