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
28 using System.Threading;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.Concurrent;
33 namespace System.Linq.Parallel
35 internal class OrderingEnumerator<T> : IEnumerator<T>
37 internal class SlotBucket
39 readonly TemporaryArea<long, T> temporaryArea = new TemporaryArea<long, T> ();
40 readonly KeyValuePair<long, T>[] stagingArea;
45 CountdownEvent stagingCount;
46 CountdownEvent participantCount;
48 CancellationTokenSource src = new CancellationTokenSource ();
49 CancellationToken mergedToken;
51 public SlotBucket (int count, CancellationToken token)
54 stagingCount = new CountdownEvent (count);
55 participantCount = new CountdownEvent (count);
56 stagingArea = new KeyValuePair<long, T>[count];
57 currentIndex = -count;
58 mergedToken = CancellationTokenSource.CreateLinkedTokenSource (src.Token, token).Token;
61 public void Add (KeyValuePair<long, T> value)
63 long index = value.Key;
65 if (index >= currentIndex && index < currentIndex + count) {
66 stagingArea[index % count] = value;
67 stagingCount.Signal ();
69 temporaryArea.TryAdd (index, value.Value);
70 if (index >= currentIndex && index < currentIndex + count) {
72 if (temporaryArea.TryRemove (index, out dummy)) {
73 stagingArea[index % count] = value;
74 stagingCount.Signal ();
80 // Called by each worker's endAction
81 public void EndParticipation ()
83 if (participantCount.Signal ())
87 // Called at the end with ContinueAll
97 for (int i = 0; i < count; i++) {
99 int index = i + (int)currentIndex;
101 if (stagingArea[i].Key != -1)
104 if (!temporaryArea.TryRemove (index, out temp))
108 stagingArea [i] = new KeyValuePair<long, T> (index, temp);
109 if (stagingCount.Signal ())
118 for (int i = 0; i < stagingArea.Length; i++)
119 stagingArea[i] = new KeyValuePair<long, T> (-1, default (T));
122 public KeyValuePair<long, T>[] Wait ()
125 stagingCount.Reset ();
127 Interlocked.Add (ref currentIndex, count);
131 while (!stagingCount.IsSet) {
132 if (!participantCount.IsSet) {
134 stagingCount.Wait (mergedToken);
140 if (participantCount.IsSet) {
144 if (stagingArea[0].Key != -1)
155 SlotBucket slotBucket;
157 KeyValuePair<long, T>[] slot;
160 internal OrderingEnumerator (int num, CancellationToken token)
162 slotBucket = new SlotBucket (num, token);
165 public void Dispose ()
175 public bool MoveNext ()
178 if (slot == null || ++current >= slot.Length) {
179 if ((slot = slotBucket.Wait ()) == null)
183 } while (slot[current].Key == -1);
190 return slot[current].Value;
194 object IEnumerator.Current {
196 return slot[current].Value;
200 public void Add (KeyValuePair<long, T> value, CancellationToken token)
202 slotBucket.Add (value);
205 // Called by each worker's endAction
206 public void EndParticipation ()
208 slotBucket.EndParticipation ();
211 // Called at the end with ContinueAll