2021-11-24 22:22:14 +01:00
|
|
|
using System;
|
2019-04-21 19:31:36 +02:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2019-04-23 23:38:35 +02:00
|
|
|
namespace Samples.Terminal
|
2019-04-21 19:31:36 +02:00
|
|
|
{
|
|
|
|
/// <summary>
|
2019-04-23 23:38:35 +02:00
|
|
|
/// Implements a bounded queue that won't block on overflow; instead the oldest item is discarded.
|
2019-04-21 19:31:36 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
2019-04-23 23:38:35 +02:00
|
|
|
public class ConcurrentBoundedQueue<T> : ConcurrentQueue<T>
|
2019-04-21 19:31:36 +02:00
|
|
|
{
|
2019-04-23 23:38:35 +02:00
|
|
|
public ConcurrentBoundedQueue(int capacity)
|
2019-04-21 19:31:36 +02:00
|
|
|
{
|
|
|
|
Capacity = GetAlignedCapacity(capacity);
|
|
|
|
}
|
|
|
|
|
2019-04-23 23:38:35 +02:00
|
|
|
public ConcurrentBoundedQueue(IEnumerable<T> collection, int capacity) : base(collection)
|
2019-04-21 19:31:36 +02:00
|
|
|
{
|
|
|
|
Capacity = GetAlignedCapacity(capacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int GetAlignedCapacity(int n)
|
|
|
|
{
|
|
|
|
if (n < 2)
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Capacity must be at least 2");
|
|
|
|
}
|
|
|
|
|
|
|
|
var f = Math.Log(n, 2);
|
|
|
|
var p = Math.Ceiling(f);
|
|
|
|
|
|
|
|
return (int) Math.Pow(2, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
public new void Enqueue(T item)
|
|
|
|
{
|
2019-04-23 23:38:35 +02:00
|
|
|
// if we're about to overflow, dump oldest item
|
2019-04-21 19:31:36 +02:00
|
|
|
if (Count >= Capacity)
|
|
|
|
{
|
|
|
|
lock (this)
|
|
|
|
{
|
|
|
|
while (Count >= Capacity)
|
|
|
|
{
|
|
|
|
TryDequeue(out _);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
base.Enqueue(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int Capacity
|
|
|
|
{
|
|
|
|
get; private set;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|