2 // OrderingEnumerator.cs
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2010 Jérémie "Garuma" Laval
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 using System.Threading;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.Concurrent;
34 namespace System.Linq.Parallel
36 internal class OrderingEnumerator<T> : IEnumerator<T>
38 internal class SlotBucket
40 readonly TemporaryArea<long, T> temporaryArea = new TemporaryArea<long, T> ();
41 readonly KeyValuePair<long, T>[] stagingArea;
46 CountdownEvent stagingCount;
47 CountdownEvent participantCount;
49 CancellationTokenSource src = new CancellationTokenSource ();
50 CancellationToken mergedToken;
52 public SlotBucket (int count, CancellationToken token)
55 stagingCount = new CountdownEvent (count);
56 participantCount = new CountdownEvent (count);
57 stagingArea = new KeyValuePair<long, T>[count];
58 currentIndex = -count;
59 mergedToken = CancellationTokenSource.CreateLinkedTokenSource (src.Token, token).Token;
62 public void Add (KeyValuePair<long, T> value)
64 long index = value.Key;
66 if (index >= currentIndex && index < currentIndex + count) {
67 stagingArea[index % count] = value;
68 stagingCount.Signal ();
70 temporaryArea.TryAdd (index, value.Value);
71 if (index >= currentIndex && index < currentIndex + count) {
73 if (temporaryArea.TryRemove (index, out dummy)) {
74 stagingArea[index % count] = value;
75 stagingCount.Signal ();
81 // Called by each worker's endAction
82 public void EndParticipation ()
84 if (participantCount.Signal ())
88 // Called at the end with ContinueAll
98 for (int i = 0; i < count; i++) {
100 int index = i + (int)currentIndex;
102 if (stagingArea[i].Key != -1)
105 if (!temporaryArea.TryRemove (index, out temp))
109 stagingArea [i] = new KeyValuePair<long, T> (index, temp);
110 if (stagingCount.Signal ())
119 for (int i = 0; i < stagingArea.Length; i++)
120 stagingArea[i] = new KeyValuePair<long, T> (-1, default (T));
123 public KeyValuePair<long, T>[] Wait ()
126 stagingCount.Reset ();
128 Interlocked.Add (ref currentIndex, count);
132 while (!stagingCount.IsSet) {
133 if (!participantCount.IsSet) {
135 stagingCount.Wait (mergedToken);
141 if (participantCount.IsSet) {
145 if (stagingArea[0].Key != -1)
156 SlotBucket slotBucket;
158 KeyValuePair<long, T>[] slot;
161 internal OrderingEnumerator (int num, CancellationToken token)
163 slotBucket = new SlotBucket (num, token);
166 public void Dispose ()
176 public bool MoveNext ()
179 if (slot == null || ++current >= slot.Length) {
180 if ((slot = slotBucket.Wait ()) == null)
184 } while (slot[current].Key == -1);
191 return slot[current].Value;
195 object IEnumerator.Current {
197 return slot[current].Value;
201 public void Add (KeyValuePair<long, T> value, CancellationToken token)
203 slotBucket.Add (value);
206 // Called by each worker's endAction
207 public void EndParticipation ()
209 slotBucket.EndParticipation ();
212 // Called at the end with ContinueAll