OrderingEnumerator.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // OrderingEnumerator.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2010 Jérémie "Garuma" Laval
  8. //
  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:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  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
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Threading;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Collections.Concurrent;
  31. namespace System.Linq.Parallel
  32. {
  33. internal class OrderingEnumerator<T> : IEnumerator<T>
  34. {
  35. internal class SlotBucket
  36. {
  37. readonly TemporaryArea<long, T> temporaryArea = new TemporaryArea<long, T> ();
  38. readonly KeyValuePair<long, T>[] stagingArea;
  39. long currentIndex;
  40. readonly int count;
  41. CountdownEvent stagingCount;
  42. CountdownEvent participantCount;
  43. CancellationTokenSource src = new CancellationTokenSource ();
  44. CancellationToken mergedToken;
  45. public SlotBucket (int count, CancellationToken token)
  46. {
  47. this.count = count;
  48. stagingCount = new CountdownEvent (count);
  49. participantCount = new CountdownEvent (count);
  50. stagingArea = new KeyValuePair<long, T>[count];
  51. currentIndex = -count;
  52. mergedToken = CancellationTokenSource.CreateLinkedTokenSource (src.Token, token).Token;
  53. }
  54. public void Add (KeyValuePair<long, T> value)
  55. {
  56. long index = value.Key;
  57. if (index >= currentIndex && index < currentIndex + count) {
  58. stagingArea[index % count] = value;
  59. stagingCount.Signal ();
  60. } else {
  61. temporaryArea.TryAdd (index, value.Value);
  62. if (index >= currentIndex && index < currentIndex + count) {
  63. T dummy;
  64. if (temporaryArea.TryRemove (index, out dummy)) {
  65. stagingArea[index % count] = value;
  66. stagingCount.Signal ();
  67. }
  68. }
  69. }
  70. }
  71. // Called by each worker's endAction
  72. public void EndParticipation ()
  73. {
  74. if (participantCount.Signal ())
  75. src.Cancel ();
  76. }
  77. // Called at the end with ContinueAll
  78. public void Stop ()
  79. {
  80. src.Cancel ();
  81. }
  82. bool Skim ()
  83. {
  84. bool result = false;
  85. for (int i = 0; i < count; i++) {
  86. T temp;
  87. int index = i + (int)currentIndex;
  88. if (stagingArea[i].Key != -1)
  89. continue;
  90. if (!temporaryArea.TryRemove (index, out temp))
  91. continue;
  92. result = true;
  93. stagingArea [i] = new KeyValuePair<long, T> (index, temp);
  94. if (stagingCount.Signal ())
  95. break;
  96. }
  97. return result;
  98. }
  99. void Clean ()
  100. {
  101. for (int i = 0; i < stagingArea.Length; i++)
  102. stagingArea[i] = new KeyValuePair<long, T> (-1, default (T));
  103. }
  104. public KeyValuePair<long, T>[] Wait ()
  105. {
  106. Clean ();
  107. stagingCount.Reset ();
  108. Interlocked.Add (ref currentIndex, count);
  109. Skim ();
  110. while (!stagingCount.IsSet) {
  111. if (!participantCount.IsSet) {
  112. try {
  113. stagingCount.Wait (mergedToken);
  114. } catch {
  115. Skim ();
  116. }
  117. }
  118. if (participantCount.IsSet) {
  119. if (Skim ())
  120. continue;
  121. // Totally finished
  122. if (stagingArea[0].Key != -1)
  123. break;
  124. else
  125. return null;
  126. }
  127. }
  128. return stagingArea;
  129. }
  130. }
  131. SlotBucket slotBucket;
  132. KeyValuePair<long, T>[] slot;
  133. int current;
  134. internal OrderingEnumerator (int num, CancellationToken token)
  135. {
  136. slotBucket = new SlotBucket (num, token);
  137. }
  138. public void Dispose ()
  139. {
  140. slotBucket.Stop ();
  141. }
  142. public void Reset ()
  143. {
  144. }
  145. public bool MoveNext ()
  146. {
  147. do {
  148. if (slot == null || ++current >= slot.Length) {
  149. if ((slot = slotBucket.Wait ()) == null)
  150. return false;
  151. current = 0;
  152. }
  153. } while (slot[current].Key == -1);
  154. return true;
  155. }
  156. public T Current {
  157. get {
  158. return slot[current].Value;
  159. }
  160. }
  161. object IEnumerator.Current {
  162. get {
  163. return slot[current].Value;
  164. }
  165. }
  166. public void Add (KeyValuePair<long, T> value, CancellationToken token)
  167. {
  168. slotBucket.Add (value);
  169. }
  170. // Called by each worker's endAction
  171. public void EndParticipation ()
  172. {
  173. slotBucket.EndParticipation ();
  174. }
  175. // Called at the end with ContinueAll
  176. public void Stop ()
  177. {
  178. slotBucket.Stop ();
  179. }
  180. }
  181. }