-
Notifications
You must be signed in to change notification settings - Fork 3
/
DropChannel.cs
52 lines (44 loc) · 880 Bytes
/
DropChannel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2017 Tim Oster. All rights reserved.
// Use of this source code is governed by the MIT license.
// More information can be found in the LICENSE file.
using System.Collections.Generic;
namespace rmnp
{
class DropChannel<T> where T : class
{
private readonly object mutex = new object();
private Queue<T> channel;
private int limit;
internal DropChannel(int limit)
{
this.channel = new Queue<T>();
this.limit = limit;
}
internal void Push(T value)
{
lock (this.mutex)
{
if (this.channel.Count == this.limit)
{
this.channel.Dequeue();
}
this.channel.Enqueue(value);
}
}
internal T Pop()
{
lock (this.mutex)
{
if (this.channel.Count == 0) return null;
return this.channel.Dequeue();
}
}
internal void Clear()
{
lock (this.mutex)
{
this.channel.Clear();
}
}
}
}