-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTSQueue.cs
157 lines (122 loc) · 3.85 KB
/
TSQueue.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Threading;
namespace uWAVE_VLBL
{
/// <summary>
/// Thread-safe queue
/// </summary>
/// <typeparam name="T">Type of items</typeparam>
public class TSQueue<T> where T : class
{
#region Properties
Queue<T> queue;
public const int DefaultMaxQueueSize = 512;
public const int DefaultCapacity = 512;
public int MaxQueueSize { get; private set; }
public int Count
{
get
{
return queue.Count;
}
}
int synLock;
#endregion
#region Constructor
public TSQueue()
: this(DefaultCapacity, DefaultMaxQueueSize)
{
}
public TSQueue(int capacity)
: this(capacity, DefaultMaxQueueSize)
{
}
public TSQueue(int capacity, int maxSize)
{
queue = new Queue<T>(capacity);
MaxQueueSize = maxSize;
}
#endregion
#region Methods
/// <summary>
/// Clears all items in queue, thread-safe
/// </summary>
public void Clear()
{
while (Interlocked.CompareExchange(ref synLock, 1, 0) != 0)
Thread.SpinWait(1);
queue.Clear();
Interlocked.Decrement(ref synLock);
}
/// <summary>
/// Enqueues item, thread-safe
/// </summary>
/// <param name="item">item to enqueue</param>
public void Enqueue(T item)
{
while (Interlocked.CompareExchange(ref synLock, 1, 0) != 0)
Thread.SpinWait(1);
queue.Enqueue(item);
if (queue.Count >= MaxQueueSize)
{
queue.Dequeue();
if (QueueOverflow != null)
QueueOverflow(this, new EventArgs());
}
Interlocked.Decrement(ref synLock);
if (ItemEnqueued != null)
{
ItemEnqueued.BeginInvoke(this, new EventArgs(), null, null);
}
}
/// <summary>
/// Dequeues item, thread-safe, avoid use this method on empty queue
/// </summary>
/// <returns>dequequed item</returns>
public T Dequeue()
{
while (Interlocked.CompareExchange(ref synLock, 1, 0) != 0)
Thread.SpinWait(1);
T result = null;
bool ok = false;
if (queue.Count > 0)
{
result = queue.Dequeue();
ok = true;
}
Interlocked.Decrement(ref synLock);
if (ok)
return result;
else
throw new InvalidOperationException("Queue empty");
}
/// <summary>
/// Dequeues all items, thread-safe, avoid use this method on empty queue
/// </summary>
/// <returns>array of dequeued items</returns>
public T[] Dump()
{
while (Interlocked.CompareExchange(ref synLock, 1, 0) != 0)
Thread.SpinWait(1);
List<T> result = new List<T>();
bool ok = false;
if (queue.Count > 0)
{
result.AddRange(queue.ToArray());
queue.Clear();
ok = true;
}
Interlocked.Decrement(ref synLock);
if (ok)
return result.ToArray();
else
throw new InvalidOperationException("Queue empty");
}
#endregion
#region Events
public EventHandler ItemEnqueued;
public EventHandler QueueOverflow;
#endregion
}
}