OrderingEnumerator.cs 5.1 KB

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