Mercurial > pub > ImplabNet
annotate Implab/Parallels/AsyncQueue.cs @ 124:a336cb13c6a9 v2
major update, added Drain mathod to AsyncQueue class
author | cin |
---|---|
date | Thu, 15 Jan 2015 02:43:14 +0300 |
parents | f4d6ea6969cc |
children | f803565868a4 |
rev | line source |
---|---|
119 | 1 using System.Threading; |
2 using System.Collections.Generic; | |
3 using System; | |
4 using System.Collections; | |
124 | 5 using System.Diagnostics; |
119 | 6 |
7 namespace Implab.Parallels { | |
8 public class AsyncQueue<T> : IEnumerable<T> { | |
9 class Chunk { | |
10 public Chunk next; | |
11 | |
12 int m_low; | |
13 int m_hi; | |
14 int m_alloc; | |
15 readonly int m_size; | |
16 readonly T[] m_data; | |
17 | |
18 public Chunk(int size) { | |
19 m_size = size; | |
20 m_data = new T[size]; | |
21 } | |
22 | |
23 public Chunk(int size, T value) { | |
24 m_size = size; | |
25 m_hi = 1; | |
26 m_alloc = 1; | |
27 m_data = new T[size]; | |
28 m_data[0] = value; | |
29 } | |
30 | |
121 | 31 public Chunk(int size, T[] data, int offset, int length, int alloc) { |
32 m_size = size; | |
33 m_hi = length; | |
34 m_alloc = alloc; | |
35 m_data = new T[size]; | |
36 Array.Copy(data, offset, m_data, 0, length); | |
37 } | |
38 | |
119 | 39 public int Low { |
40 get { return m_low; } | |
41 } | |
42 | |
43 public int Hi { | |
44 get { return m_hi; } | |
45 } | |
46 | |
121 | 47 public bool TryEnqueue(T value, out bool extend) { |
120 | 48 var alloc = Interlocked.Increment(ref m_alloc) - 1; |
119 | 49 |
120 | 50 if (alloc >= m_size) { |
51 extend = alloc == m_size; | |
119 | 52 return false; |
53 } | |
120 | 54 |
55 extend = false; | |
119 | 56 m_data[alloc] = value; |
57 | |
58 while (alloc != Interlocked.CompareExchange(ref m_hi, alloc + 1, alloc)) { | |
59 // spin wait for commit | |
60 } | |
61 return true; | |
62 } | |
63 | |
124 | 64 /// <summary> |
65 /// Prevents from allocating new space in the chunk and waits for all write operations to complete | |
66 /// </summary> | |
67 public void Commit() { | |
68 var actual = Math.Min(Interlocked.Exchange(ref m_alloc, m_size + 1), m_size); | |
69 | |
70 while (m_hi != actual) | |
71 Thread.MemoryBarrier(); | |
72 } | |
73 | |
121 | 74 public bool TryDequeue(out T value, out bool recycle) { |
119 | 75 int low; |
76 do { | |
77 low = m_low; | |
78 if (low >= m_hi) { | |
79 value = default(T); | |
80 recycle = (low == m_size); | |
81 return false; | |
82 } | |
83 } while(low != Interlocked.CompareExchange(ref m_low, low + 1, low)); | |
84 | |
85 recycle = (low == m_size - 1); | |
86 value = m_data[low]; | |
87 | |
88 return true; | |
89 } | |
90 | |
120 | 91 public bool TryEnqueueBatch(T[] batch, int offset, int length, out int enqueued, out bool extend) { |
122
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
92 //int alloc; |
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
93 //int allocSize; |
120 | 94 |
122
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
95 var alloc = Interlocked.Add(ref m_alloc, length) - length; |
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
96 if (alloc > m_size) { |
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
97 // the chunk is full and someone already |
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
98 // creating the new one |
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
99 enqueued = 0; // nothing was added |
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
100 extend = false; // the caller shouldn't try to extend the queue |
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
101 return false; // nothing was added |
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
102 } |
120 | 103 |
122
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
104 enqueued = Math.Min(m_size - alloc, length); |
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
105 extend = length > enqueued; |
120 | 106 |
122
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
107 if (enqueued == 0) |
120 | 108 return false; |
109 | |
110 | |
122
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
111 Array.Copy(batch, offset, m_data, alloc, enqueued); |
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
112 |
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
113 while (alloc != Interlocked.CompareExchange(ref m_hi, alloc + enqueued, alloc)) { |
120 | 114 // spin wait for commit |
115 } | |
122
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
116 |
120 | 117 return true; |
118 } | |
119 | |
121 | 120 public bool TryDequeueBatch(T[] buffer, int offset, int length,out int dequeued, out bool recycle) { |
121 int low, hi, batchSize; | |
122 | |
123 do { | |
124 low = m_low; | |
125 hi = m_hi; | |
126 if (low >= hi) { | |
127 dequeued = 0; | |
128 recycle = (low == m_size); // recycling could be restarted and we need to signal again | |
129 return false; | |
130 } | |
131 batchSize = Math.Min(hi - low, length); | |
132 } while(low != Interlocked.CompareExchange(ref m_low, low + batchSize, low)); | |
133 | |
134 recycle = (low == m_size - batchSize); | |
135 dequeued = batchSize; | |
136 | |
137 Array.Copy(m_data, low, buffer, offset, batchSize); | |
138 | |
139 return true; | |
140 } | |
141 | |
119 | 142 public T GetAt(int pos) { |
143 return m_data[pos]; | |
144 } | |
145 } | |
146 | |
147 public const int DEFAULT_CHUNK_SIZE = 32; | |
121 | 148 public const int MAX_CHUNK_SIZE = 262144; |
119 | 149 |
150 readonly int m_chunkSize = DEFAULT_CHUNK_SIZE; | |
151 | |
152 Chunk m_first; | |
153 Chunk m_last; | |
154 | |
155 public AsyncQueue() { | |
156 m_last = m_first = new Chunk(m_chunkSize); | |
157 } | |
158 | |
121 | 159 /// <summary> |
160 /// Adds the specified value to the queue. | |
161 /// </summary> | |
162 /// <param name="value">Tha value which will be added to the queue.</param> | |
119 | 163 public void Enqueue(T value) { |
164 var last = m_last; | |
165 // spin wait to the new chunk | |
166 bool extend = true; | |
121 | 167 while (last == null || !last.TryEnqueue(value, out extend)) { |
119 | 168 // try to extend queue |
169 if (extend || last == null) { | |
170 var chunk = new Chunk(m_chunkSize, value); | |
171 if (EnqueueChunk(last, chunk)) | |
122
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
172 break; // success! exit! |
119 | 173 last = m_last; |
174 } else { | |
121 | 175 while (last == m_last) { |
119 | 176 Thread.MemoryBarrier(); |
177 } | |
121 | 178 last = m_last; |
119 | 179 } |
180 } | |
181 } | |
182 | |
121 | 183 /// <summary> |
184 /// Adds the specified data to the queue. | |
185 /// </summary> | |
186 /// <param name="data">The buffer which contains the data to be enqueued.</param> | |
187 /// <param name="offset">The offset of the data in the buffer.</param> | |
188 /// <param name="length">The size of the data to read from the buffer.</param> | |
189 public void EnqueueRange(T[] data, int offset, int length) { | |
190 if (data == null) | |
191 throw new ArgumentNullException("data"); | |
192 if (offset < 0) | |
193 throw new ArgumentOutOfRangeException("offset"); | |
194 if (length < 1 || offset + length > data.Length) | |
195 throw new ArgumentOutOfRangeException("length"); | |
196 | |
197 var last = m_last; | |
198 | |
199 bool extend; | |
200 int enqueued; | |
201 | |
202 while (length > 0) { | |
203 extend = true; | |
204 if (last != null && last.TryEnqueueBatch(data, offset, length, out enqueued, out extend)) { | |
205 length -= enqueued; | |
206 offset += enqueued; | |
207 } | |
208 | |
209 if (extend) { | |
210 // there was no enough space in the chunk | |
211 // or there was no chunks in the queue | |
212 | |
213 while (length > 0) { | |
214 | |
215 var size = Math.Min(length, MAX_CHUNK_SIZE); | |
216 | |
217 var chunk = new Chunk( | |
218 Math.Max(size, m_chunkSize), | |
219 data, | |
220 offset, | |
221 size, | |
222 length // length >= size | |
223 ); | |
224 | |
225 if (!EnqueueChunk(last, chunk)) { | |
226 // looks like the queue has been updated then proceed from the beginning | |
227 last = m_last; | |
228 break; | |
229 } | |
230 | |
231 // we have successfully added the new chunk | |
232 last = chunk; | |
233 length -= size; | |
234 offset += size; | |
235 } | |
236 } else { | |
237 // we don't need to extend the queue, if we successfully enqueued data | |
238 if (length == 0) | |
239 break; | |
240 | |
241 // if we need to wait while someone is extending the queue | |
242 // spinwait | |
243 while (last == m_last) { | |
244 Thread.MemoryBarrier(); | |
245 } | |
246 | |
247 last = m_last; | |
248 } | |
249 } | |
250 } | |
251 | |
252 /// <summary> | |
253 /// Tries to retrieve the first element from the queue. | |
254 /// </summary> | |
255 /// <returns><c>true</c>, if element is dequeued, <c>false</c> otherwise.</returns> | |
256 /// <param name="value">The value of the dequeued element.</param> | |
119 | 257 public bool TryDequeue(out T value) { |
258 var chunk = m_first; | |
259 bool recycle; | |
260 while (chunk != null) { | |
261 | |
262 var result = chunk.TryDequeue(out value, out recycle); | |
263 | |
264 if (recycle) // this chunk is waste | |
265 RecycleFirstChunk(chunk); | |
266 else | |
267 return result; // this chunk is usable and returned actual result | |
268 | |
269 if (result) // this chunk is waste but the true result is always actual | |
270 return true; | |
271 | |
272 // try again | |
273 chunk = m_first; | |
274 } | |
275 | |
276 // the queue is empty | |
277 value = default(T); | |
278 return false; | |
279 } | |
280 | |
121 | 281 /// <summary> |
282 /// Tries to dequeue the specified amount of data from the queue. | |
283 /// </summary> | |
284 /// <returns><c>true</c>, if data was deuqueued, <c>false</c> otherwise.</returns> | |
285 /// <param name="buffer">The buffer to which the data will be written.</param> | |
286 /// <param name="offset">The offset in the buffer at which the data will be written.</param> | |
287 /// <param name="length">The maximum amount of data to be retrieved.</param> | |
288 /// <param name="dequeued">The actual amout of the retrieved data.</param> | |
289 public bool TryDequeueRange(T[] buffer, int offset, int length, out int dequeued) { | |
290 if (buffer == null) | |
291 throw new ArgumentNullException("buffer"); | |
292 if (offset < 0) | |
293 throw new ArgumentOutOfRangeException("offset"); | |
294 if (length < 1 || offset + length > buffer.Length) | |
295 throw new ArgumentOutOfRangeException("length"); | |
296 | |
297 var chunk = m_first; | |
298 bool recycle; | |
299 dequeued = 0; | |
300 while (chunk != null) { | |
301 | |
302 int actual; | |
303 if (chunk.TryDequeueBatch(buffer, offset, length, out actual, out recycle)) { | |
304 offset += actual; | |
305 length -= actual; | |
306 dequeued += actual; | |
307 } | |
308 | |
309 if (recycle) // this chunk is waste | |
310 RecycleFirstChunk(chunk); | |
311 else if (actual == 0) | |
312 break; // the chunk is usable, but doesn't contain any data (it's the last chunk in the queue) | |
313 | |
314 if (length == 0) | |
315 return true; | |
316 | |
317 // we still may dequeue something | |
318 // try again | |
319 chunk = m_first; | |
320 } | |
321 | |
322 return dequeued != 0; | |
323 } | |
324 | |
325 /// <summary> | |
326 /// Tries to dequeue all remaining data in the first chunk. | |
327 /// </summary> | |
328 /// <returns><c>true</c>, if data was dequeued, <c>false</c> otherwise.</returns> | |
122
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
329 /// <param name="buffer">The buffer to which the data will be written.</param> |
121 | 330 /// <param name="offset">The offset in the buffer at which the data will be written.</param> |
331 /// <param name="length">Tha maximum amount of the data to be dequeued.</param> | |
332 /// <param name="dequeued">The actual amount of the dequeued data.</param> | |
333 public bool TryDequeueChunk(T[] buffer, int offset, int length, out int dequeued) { | |
334 if (buffer == null) | |
335 throw new ArgumentNullException("buffer"); | |
336 if (offset < 0) | |
337 throw new ArgumentOutOfRangeException("offset"); | |
338 if (length < 1 || offset + length > buffer.Length) | |
339 throw new ArgumentOutOfRangeException("length"); | |
340 | |
341 var chunk = m_first; | |
342 bool recycle; | |
343 dequeued = 0; | |
344 | |
345 while (chunk != null) { | |
346 | |
347 int actual; | |
348 if (chunk.TryDequeueBatch(buffer, offset, length, out actual, out recycle)) { | |
349 dequeued = actual; | |
350 } | |
351 | |
352 if (recycle) // this chunk is waste | |
353 RecycleFirstChunk(chunk); | |
354 | |
355 // if we have dequeued any data, then return | |
356 if (dequeued != 0) | |
357 return true; | |
358 | |
359 // we still may dequeue something | |
360 // try again | |
361 chunk = m_first; | |
362 } | |
363 | |
364 return false; | |
365 } | |
366 | |
119 | 367 bool EnqueueChunk(Chunk last, Chunk chunk) { |
368 if (Interlocked.CompareExchange(ref m_last, chunk, last) != last) | |
369 return false; | |
370 | |
371 if (last != null) | |
372 last.next = chunk; | |
124 | 373 else { |
119 | 374 m_first = chunk; |
124 | 375 } |
119 | 376 return true; |
377 } | |
378 | |
379 void RecycleFirstChunk(Chunk first) { | |
380 var next = first.next; | |
381 | |
124 | 382 if (first != Interlocked.CompareExchange(ref m_first, next, first)) |
383 return; | |
384 | |
119 | 385 if (next == null) { |
124 | 386 |
119 | 387 if (first != Interlocked.CompareExchange(ref m_last, null, first)) { |
124 | 388 /*while (first.next == null) |
389 Thread.MemoryBarrier();*/ | |
390 | |
119 | 391 // race |
124 | 392 // someone already updated the tail, restore the pointer to the queue head |
393 m_first = first; | |
119 | 394 } |
395 // the tail is updated | |
396 } | |
397 | |
398 // we need to update the head | |
124 | 399 //Interlocked.CompareExchange(ref m_first, next, first); |
119 | 400 // if the head is already updated then give up |
124 | 401 //return; |
119 | 402 |
403 } | |
404 | |
123 | 405 public void Clear() { |
406 // start the new queue | |
124 | 407 var chunk = new Chunk(m_chunkSize); |
408 | |
409 do { | |
410 Thread.MemoryBarrier(); | |
411 var first = m_first; | |
412 var last = m_last; | |
413 | |
414 if (last == null) // nothing to clear | |
415 return; | |
123 | 416 |
124 | 417 if (first == null || (first.next == null && first != last)) // inconcistency |
418 continue; | |
419 | |
420 // here we will create inconsistency which will force others to spin | |
421 // and prevent from fetching. chunk.next = null | |
422 if (first != Interlocked.CompareExchange(ref m_first, chunk, first)) | |
423 continue;// inconsistent | |
424 | |
425 m_last = chunk; | |
426 | |
427 return; | |
428 | |
429 } while(true); | |
123 | 430 } |
431 | |
432 public T[] Drain() { | |
433 // start the new queue | |
124 | 434 var chunk = new Chunk(m_chunkSize); |
123 | 435 |
436 do { | |
124 | 437 Thread.MemoryBarrier(); |
438 var first = m_first; | |
439 var last = m_last; | |
440 | |
441 if (last == null) | |
442 return new T[0]; | |
443 | |
444 if (first == null || (first.next == null && first != last)) | |
445 continue; | |
123 | 446 |
124 | 447 // here we will create inconsistency which will force others to spin |
448 // and prevent from fetching. chunk.next = null | |
449 if (first != Interlocked.CompareExchange(ref m_first, chunk, first)) | |
450 continue;// inconsistent | |
123 | 451 |
124 | 452 last = Interlocked.Exchange(ref m_last, chunk); |
453 | |
454 return ReadChunks(first, last); | |
455 | |
456 } while(true); | |
123 | 457 } |
458 | |
124 | 459 T[] ReadChunks(Chunk chunk, object last) { |
123 | 460 var result = new List<T>(); |
461 var buffer = new T[m_chunkSize]; | |
462 int actual; | |
463 bool recycle; | |
464 while (chunk != null) { | |
124 | 465 // ensure all write operations on the chunk are complete |
466 chunk.Commit(); | |
467 | |
123 | 468 // we need to read the chunk using this way |
469 // since some client still may completing the dequeue | |
470 // operation, such clients most likely won't get results | |
471 while (chunk.TryDequeueBatch(buffer, 0, buffer.Length, out actual, out recycle)) | |
472 result.AddRange(new ArraySegmentCollection(buffer, 0, actual)); | |
473 | |
124 | 474 if (chunk == last) { |
475 chunk = null; | |
476 } else { | |
477 while (chunk.next == null) | |
478 Thread.MemoryBarrier(); | |
479 chunk = chunk.next; | |
480 } | |
123 | 481 } |
482 | |
483 return result.ToArray(); | |
484 } | |
485 | |
486 struct ArraySegmentCollection : ICollection<T> { | |
487 readonly T[] m_data; | |
488 readonly int m_offset; | |
489 readonly int m_length; | |
490 | |
491 public ArraySegmentCollection(T[] data, int offset, int length) { | |
492 m_data = data; | |
493 m_offset = offset; | |
494 m_length = length; | |
495 } | |
496 | |
497 #region ICollection implementation | |
498 | |
499 public void Add(T item) { | |
500 throw new InvalidOperationException(); | |
501 } | |
502 | |
503 public void Clear() { | |
504 throw new InvalidOperationException(); | |
505 } | |
506 | |
507 public bool Contains(T item) { | |
508 return false; | |
509 } | |
510 | |
511 public void CopyTo(T[] array, int arrayIndex) { | |
512 Array.Copy(m_data,m_offset,array,arrayIndex, m_length); | |
513 } | |
514 | |
515 public bool Remove(T item) { | |
516 throw new NotImplementedException(); | |
517 } | |
518 | |
519 public int Count { | |
520 get { | |
521 return m_length; | |
522 } | |
523 } | |
524 | |
525 public bool IsReadOnly { | |
526 get { | |
527 return true; | |
528 } | |
529 } | |
530 | |
531 #endregion | |
532 | |
533 #region IEnumerable implementation | |
534 | |
535 public IEnumerator<T> GetEnumerator() { | |
536 for (int i = m_offset; i < m_length + m_offset; i++) | |
537 yield return m_data[i]; | |
538 } | |
539 | |
540 #endregion | |
541 | |
542 #region IEnumerable implementation | |
543 | |
544 IEnumerator IEnumerable.GetEnumerator() { | |
545 return GetEnumerator(); | |
546 } | |
547 | |
548 #endregion | |
549 } | |
550 | |
119 | 551 #region IEnumerable implementation |
552 | |
553 class Enumerator : IEnumerator<T> { | |
554 Chunk m_current; | |
555 int m_pos = -1; | |
556 | |
557 public Enumerator(Chunk fisrt) { | |
558 m_current = fisrt; | |
559 } | |
560 | |
561 #region IEnumerator implementation | |
562 | |
563 public bool MoveNext() { | |
564 if (m_current == null) | |
565 return false; | |
566 | |
567 if (m_pos == -1) | |
568 m_pos = m_current.Low; | |
569 else | |
570 m_pos++; | |
571 if (m_pos == m_current.Hi) { | |
572 m_pos = 0; | |
573 m_current = m_current.next; | |
574 } | |
575 | |
576 return true; | |
577 } | |
578 | |
579 public void Reset() { | |
580 throw new NotSupportedException(); | |
581 } | |
582 | |
583 object IEnumerator.Current { | |
584 get { | |
585 return Current; | |
586 } | |
587 } | |
588 | |
589 #endregion | |
590 | |
591 #region IDisposable implementation | |
592 | |
593 public void Dispose() { | |
594 } | |
595 | |
596 #endregion | |
597 | |
598 #region IEnumerator implementation | |
599 | |
600 public T Current { | |
601 get { | |
602 if (m_pos == -1 || m_current == null) | |
603 throw new InvalidOperationException(); | |
604 return m_current.GetAt(m_pos); | |
605 } | |
606 } | |
607 | |
608 #endregion | |
609 } | |
610 | |
611 public IEnumerator<T> GetEnumerator() { | |
612 return new Enumerator(m_first); | |
613 } | |
614 | |
615 #endregion | |
616 | |
617 #region IEnumerable implementation | |
618 | |
619 IEnumerator IEnumerable.GetEnumerator() { | |
620 return GetEnumerator(); | |
621 } | |
622 | |
623 #endregion | |
624 } | |
625 } |