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