ParallelQueryEnumerator.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // ParallelEnumerator.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. #if NET_4_0
  32. namespace System.Linq
  33. {
  34. internal class ParallelQueryEnumerator<T> : IEnumerator<T>
  35. {
  36. readonly int DefaultBufferSize = ParallelExecuter.GetBestWorkerNumber () * 50;
  37. BlockingCollection<T> buffer;
  38. IEnumerator<T> loader;
  39. QueryOptions options;
  40. OrderingEnumerator<T> ordEnumerator;
  41. T current;
  42. Action waitAction;
  43. internal ParallelQueryEnumerator (QueryBaseNode<T> node)
  44. {
  45. this.options = ParallelExecuter.CheckQuery (node);
  46. Setup ();
  47. // Launch adding to the buffer asynchronously via Tasks
  48. if (options.BehindOrderGuard.Value) {
  49. waitAction = ParallelExecuter.ProcessAndCallback (node,
  50. (e, i) => ordEnumerator.Add (e),
  51. (i) => ordEnumerator.EndParticipation (),
  52. ordEnumerator.Stop,
  53. options);
  54. } else {
  55. waitAction = ParallelExecuter.ProcessAndCallback (node,
  56. buffer.Add,
  57. buffer.CompleteAdding,
  58. options);
  59. }
  60. if (options.Options.HasValue && options.Options.Value == ParallelMergeOptions.FullyBuffered)
  61. waitAction ();
  62. }
  63. void Setup ()
  64. {
  65. if (!options.BehindOrderGuard.Value) {
  66. if (options.Options.HasValue && (options.Options.Value == ParallelMergeOptions.NotBuffered
  67. || options.Options.Value == ParallelMergeOptions.FullyBuffered)) {
  68. buffer = new BlockingCollection<T> ();
  69. } else {
  70. buffer = new BlockingCollection<T> (DefaultBufferSize);
  71. }
  72. IEnumerable<T> source = buffer.GetConsumingEnumerable (options.Token);
  73. loader = source.GetEnumerator ();
  74. } else {
  75. loader = ordEnumerator = new OrderingEnumerator<T> (options.PartitionCount);
  76. }
  77. }
  78. public void Dispose ()
  79. {
  80. }
  81. public void Reset ()
  82. {
  83. throw new NotSupportedException ();
  84. }
  85. public bool MoveNext ()
  86. {
  87. // If there are no stuff in the buffer
  88. // but CompleteAdding hasn't been called,
  89. // MoveNext blocks until further results are produced
  90. if (!loader.MoveNext ())
  91. return false;
  92. current = loader.Current;
  93. return true;
  94. }
  95. public T Current {
  96. get {
  97. return current;
  98. }
  99. }
  100. object IEnumerator.Current {
  101. get {
  102. return current;
  103. }
  104. }
  105. }
  106. }
  107. #endif