2010-05-25 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Core / System.Linq / ParallelQueryEnumerator.cs
blobd250b8bd28133bb6084ee123bb37c44cffe49e60
1 //
2 // ParallelEnumerator.cs
3 //
4 // Author:
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
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:
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
25 // THE SOFTWARE.
27 using System;
28 using System.Threading;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.Concurrent;
33 #if NET_4_0
35 namespace System.Linq
37 internal class ParallelQueryEnumerator<T> : IEnumerator<T>
39 readonly int DefaultBufferSize = ParallelExecuter.GetBestWorkerNumber () * 50;
41 BlockingCollection<T> buffer;
42 IEnumerator<T> loader;
43 QueryOptions options;
44 OrderingEnumerator<T> ordEnumerator;
46 T current;
48 Action waitAction;
50 internal ParallelQueryEnumerator (QueryBaseNode<T> node)
52 this.options = ParallelExecuter.CheckQuery (node);
53 Setup ();
55 // Launch adding to the buffer asynchronously via Tasks
56 if (options.BehindOrderGuard.Value) {
57 waitAction = ParallelExecuter.ProcessAndCallback (node,
58 (e, i) => ordEnumerator.Add (e),
59 (i) => ordEnumerator.EndParticipation (),
60 ordEnumerator.Stop,
61 options);
62 } else {
63 waitAction = ParallelExecuter.ProcessAndCallback (node,
64 buffer.Add,
65 buffer.CompleteAdding,
66 options);
69 if (options.Options.HasValue && options.Options.Value == ParallelMergeOptions.FullyBuffered)
70 waitAction ();
73 void Setup ()
75 if (!options.BehindOrderGuard.Value) {
76 if (options.Options.HasValue && (options.Options.Value == ParallelMergeOptions.NotBuffered
77 || options.Options.Value == ParallelMergeOptions.FullyBuffered)) {
78 buffer = new BlockingCollection<T> ();
79 } else {
80 buffer = new BlockingCollection<T> (DefaultBufferSize);
83 IEnumerable<T> source = buffer.GetConsumingEnumerable (options.Token);
85 loader = source.GetEnumerator ();
86 } else {
87 loader = ordEnumerator = new OrderingEnumerator<T> (options.PartitionCount);
91 public void Dispose ()
96 public void Reset ()
98 throw new NotSupportedException ();
101 public bool MoveNext ()
103 // If there are no stuff in the buffer
104 // but CompleteAdding hasn't been called,
105 // MoveNext blocks until further results are produced
106 if (!loader.MoveNext ())
107 return false;
109 current = loader.Current;
110 return true;
113 public T Current {
114 get {
115 return current;
119 object IEnumerator.Current {
120 get {
121 return current;
126 #endif