mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-17 01:33:03 +08:00
111 lines
2.5 KiB
C#
111 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
|
|
namespace MQTTnet.Internal
|
|
{
|
|
public class BlockingQueue<TItem>
|
|
{
|
|
private readonly object _syncRoot = new object();
|
|
private readonly LinkedList<TItem> _items = new LinkedList<TItem>();
|
|
private readonly ManualResetEvent _gate = new ManualResetEvent(false);
|
|
|
|
public int Count
|
|
{
|
|
get
|
|
{
|
|
lock (_syncRoot)
|
|
{
|
|
return _items.Count;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Enqueue(TItem item)
|
|
{
|
|
if (item == null) throw new ArgumentNullException(nameof(item));
|
|
|
|
lock (_syncRoot)
|
|
{
|
|
_items.AddLast(item);
|
|
_gate.Set();
|
|
}
|
|
}
|
|
|
|
public TItem Dequeue()
|
|
{
|
|
while (true)
|
|
{
|
|
lock (_syncRoot)
|
|
{
|
|
if (_items.Count > 0)
|
|
{
|
|
var item = _items.First.Value;
|
|
_items.RemoveFirst();
|
|
|
|
return item;
|
|
}
|
|
|
|
if (_items.Count == 0)
|
|
{
|
|
_gate.Reset();
|
|
}
|
|
}
|
|
|
|
_gate.WaitOne();
|
|
}
|
|
}
|
|
|
|
public TItem PeekAndWait()
|
|
{
|
|
while (true)
|
|
{
|
|
lock (_syncRoot)
|
|
{
|
|
if (_items.Count > 0)
|
|
{
|
|
return _items.First.Value;
|
|
}
|
|
|
|
if (_items.Count == 0)
|
|
{
|
|
_gate.Reset();
|
|
}
|
|
}
|
|
|
|
_gate.WaitOne();
|
|
}
|
|
}
|
|
|
|
public void RemoveFirstIfEqual(TItem item)
|
|
{
|
|
lock (_syncRoot)
|
|
{
|
|
if (_items.Count > 0 && EqualityComparer<TItem>.Default.Equals(_items.First.Value, item))
|
|
{
|
|
_items.RemoveFirst();
|
|
}
|
|
}
|
|
}
|
|
|
|
public TItem RemoveFirst()
|
|
{
|
|
lock (_syncRoot)
|
|
{
|
|
var item = _items.First;
|
|
_items.RemoveFirst();
|
|
|
|
return item.Value;
|
|
}
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
lock (_syncRoot)
|
|
{
|
|
_items.Clear();
|
|
}
|
|
}
|
|
}
|
|
}
|