|
1 | | -using System; |
2 | | -using System.Collections.Concurrent; |
3 | | -using System.Collections.Generic; |
4 | | -using System.Linq; |
5 | | -using System.Text; |
6 | | -using System.Threading.Tasks; |
| 1 | +using System.Collections.Concurrent; |
7 | 2 |
|
8 | 3 | namespace RB4InstrumentMapper |
9 | 4 | { |
10 | 5 | /// <summary> |
11 | | - /// Implementation of a fixed-size concurrent queue. |
| 6 | + /// A concurrent queue with a size limit. |
12 | 7 | /// </summary> |
13 | | - /// <typeparam name="T"></typeparam> |
14 | 8 | public class FixedSizeConcurrentQueue<T> : ConcurrentQueue<T> |
15 | 9 | { |
16 | | - /// <summary> |
17 | | - /// Sync root of the queue for locking. |
18 | | - /// </summary> |
19 | | - private readonly object privateLockObject = new object(); |
| 10 | + private readonly object queueLock = new object(); |
20 | 11 |
|
21 | 12 | /// <summary> |
22 | 13 | /// Size of the queue. |
23 | 14 | /// </summary> |
24 | | - public int Size { get; private set; } |
| 15 | + public int MaxSize { get; private set; } |
25 | 16 |
|
26 | 17 | /// <summary> |
27 | | - /// Create a new instance of the FixedSizeConcurrentQueue. |
| 18 | + /// Creates a new queue with the given max size. |
28 | 19 | /// </summary> |
29 | | - /// <param name="size">Size of the queue.</param> |
| 20 | + |
30 | 21 | public FixedSizeConcurrentQueue(int size) |
31 | 22 | { |
32 | | - Size = size; |
| 23 | + MaxSize = size; |
33 | 24 | } |
34 | 25 |
|
35 | 26 | /// <summary> |
36 | | - /// Enqueue an object into the queue. Maintains size limit of queue. |
| 27 | + /// Enqueues an object into the queue, discarding any items at the end of the queue which exceed the max count. |
37 | 28 | /// </summary> |
38 | | - /// <param name="obj">Object to enqueue</param> |
39 | 29 | public new void Enqueue(T obj) |
40 | 30 | { |
41 | 31 | base.Enqueue(obj); |
42 | | - lock (privateLockObject) |
| 32 | + lock (queueLock) |
43 | 33 | { |
44 | | - while (base.Count > Size) |
| 34 | + while (Count > MaxSize) |
45 | 35 | { |
46 | | - T outObj; |
47 | | - base.TryDequeue(out outObj); |
| 36 | + TryDequeue(out _); |
48 | 37 | } |
49 | 38 | } |
50 | 39 | } |
|
0 commit comments