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