OrderingEnumerator.cs 4.6 KB

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