Repository navigation
Expand file tree
/
Copy pathConcurrentBatchQueue.cs
More file actions
259 lines (201 loc) · 6.41 KB
/
Copy pathConcurrentBatchQueue.cs
File metadata and controls
259 lines (201 loc) · 6.41 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace SuperSocket.ClientEngine
{
/// <summary>
/// Concurrent BatchQueue
/// </summary>
/// <typeparam name="T"></typeparam>
public class ConcurrentBatchQueue<T> : IBatchQueue<T>
{
private Entity m_Entity;
private Entity m_BackEntity;
private static readonly T m_Null = default(T);
private Func<T, bool> m_NullValidator;
class Entity
{
public T[] Array { get; set; }
public int Count;
}
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentBatchQueue<T>"/> class.
/// </summary>
public ConcurrentBatchQueue()
: this(16)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentBatchQueue<T>"/> class.
/// </summary>
/// <param name="capacity">The capacity of the queue.</param>
public ConcurrentBatchQueue(int capacity)
: this(new T[capacity])
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentBatchQueue<T>"/> class.
/// </summary>
/// <param name="capacity">The capacity.</param>
/// <param name="nullValidator">The null validator.</param>
public ConcurrentBatchQueue(int capacity, Func<T, bool> nullValidator)
: this(new T[capacity], nullValidator)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentBatchQueue<T>"/> class.
/// </summary>
/// <param name="array">The array.</param>
public ConcurrentBatchQueue(T[] array)
: this(array, (t) => t == null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentBatchQueue<T>"/> class.
/// </summary>
/// <param name="array">The array.</param>
/// <param name="nullValidator">The null validator.</param>
public ConcurrentBatchQueue(T[] array, Func<T, bool> nullValidator)
{
m_Entity = new Entity();
m_Entity.Array = array;
m_BackEntity = new Entity();
m_BackEntity.Array = new T[array.Length];
m_NullValidator = nullValidator;
}
/// <summary>
/// Enqueues the specified item.
/// </summary>
/// <param name="item">The item.</param>
public bool Enqueue(T item)
{
bool full;
while (true)
{
if (TryEnqueue(item, out full) || full)
break;
}
return !full;
}
private bool TryEnqueue(T item, out bool full)
{
full = false;
var entity = m_Entity;
var array = entity.Array;
var count = entity.Count;
if (count >= array.Length)
{
full = true;
return false;
}
if(entity != m_Entity)
return false;
int oldCount = Interlocked.CompareExchange(ref entity.Count, count + 1, count);
if (oldCount != count)
return false;
array[count] = item;
return true;
}
/// <summary>
/// Enqueues the specified items.
/// </summary>
/// <param name="items">The items.</param>
public bool Enqueue(IList<T> items)
{
bool full;
while (true)
{
if (TryEnqueue(items, out full) || full)
break;
}
return !full;
}
private bool TryEnqueue(IList<T> items, out bool full)
{
full = false;
var entity = m_Entity;
var array = entity.Array;
var count = entity.Count;
int newItemCount = items.Count;
int expectedCount = count + newItemCount;
if (expectedCount > array.Length)
{
full = true;
return false;
}
if (entity != m_Entity)
return false;
int oldCount = Interlocked.CompareExchange(ref entity.Count, expectedCount, count);
if (oldCount != count)
return false;
foreach (var item in items)
{
array[count++] = item;
}
return true;
}
/// <summary>
/// Tries the dequeue.
/// </summary>
/// <param name="outputItems">The output items.</param>
/// <returns></returns>
public bool TryDequeue(IList<T> outputItems)
{
var entity = m_Entity;
int count = entity.Count;
if (count <= 0)
return false;
#if UNITY
var oldEntity = m_Entity;
if (m_Entity == entity)
{
m_Entity = m_BackEntity;
}
#else
var oldEntity = Interlocked.CompareExchange(ref m_Entity, m_BackEntity, entity);
#endif
if (!ReferenceEquals(oldEntity, entity))
return false;
var spinWait = new SpinWait();
spinWait.SpinOnce();
count = entity.Count;
var array = entity.Array;
var i = 0;
while (true)
{
var item = array[i];
while (m_NullValidator(item))
{
spinWait.SpinOnce();
item = array[i];
}
outputItems.Add(item);
array[i] = m_Null;
if (entity.Count <= (i + 1))
break;
i++;
}
entity.Count = 0;
m_BackEntity = entity;
return true;
}
/// <summary>
/// Gets a value indicating whether this instance is empty.
/// </summary>
/// <value>
/// <c>true</c> if this instance is empty; otherwise, <c>false</c>.
/// </value>
public bool IsEmpty
{
get { return m_Entity.Count <= 0; }
}
/// <summary>
/// Gets the count.
/// </summary>
public int Count
{
get { return m_Entity.Count; }
}
}
}