ParallelQueryEnumerator.cs 4.0 KB

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