2 // ParallelEnumerator.cs
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2010 Jérémie "Garuma" Laval
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
29 using System.Threading;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.Concurrent;
33 using System.Linq.Parallel.QueryNodes;
35 namespace System.Linq.Parallel
37 internal class ParallelQueryEnumerator<T> : IEnumerator<T>
39 readonly int DefaultBufferSize = ParallelExecuter.GetBestWorkerNumber () * 50;
41 BlockingCollection<T> buffer;
42 IEnumerator<T> loader;
44 OrderingEnumerator<T> ordEnumerator;
50 internal ParallelQueryEnumerator (QueryBaseNode<T> node)
52 this.options = ParallelExecuter.CheckQuery (node);
54 if (options.ShouldBeSequential && options.Mode != ParallelExecutionMode.ForceParallelism) {
55 IEnumerable<T> buffer = node.GetSequential ();
56 loader = buffer.GetEnumerator ();
60 // Launch adding to the buffer asynchronously via Tasks
61 if (options.BehindOrderGuard.Value) {
62 waitAction = ParallelExecuter.ProcessAndCallback (node,
64 ordEnumerator.EndParticipation,
68 waitAction = ParallelExecuter.ProcessAndCallback (node,
70 buffer.CompleteAdding,
74 if (options.Options.HasValue && options.Options.Value == ParallelMergeOptions.FullyBuffered)
81 if (!options.BehindOrderGuard.Value) {
82 if (options.Options.HasValue && (options.Options.Value == ParallelMergeOptions.NotBuffered
83 || options.Options.Value == ParallelMergeOptions.FullyBuffered)) {
84 buffer = new BlockingCollection<T> ();
86 buffer = new BlockingCollection<T> (DefaultBufferSize);
89 IEnumerable<T> source = buffer.GetConsumingEnumerable (options.MergedToken);
90 loader = source.GetEnumerator ();
92 loader = ordEnumerator = new OrderingEnumerator<T> (options.PartitionCount, options.MergedToken);
96 public void Dispose ()
103 throw new NotSupportedException ();
106 public bool MoveNext ()
108 // If there are no stuff in the buffer
109 // but CompleteAdding hasn't been called,
110 // MoveNext blocks until further results are produced
111 if (!loader.MoveNext ())
114 current = loader.Current;
124 object IEnumerator.Current {