Removed DeflateStream.UnmanagedRead Read loop. Fixes #19313.
[mono.git] / mcs / class / System.Core / System.Linq.Parallel / ParallelExecuter.cs
1 //
2 // ParallelExecuter.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:
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
27 #if NET_4_0
28 using System;
29 using System.Threading;
30 using System.Threading.Tasks;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Collections.Concurrent;
34 using System.Linq.Parallel.QueryNodes;
35
36 namespace System.Linq.Parallel
37 {
38         internal static class ParallelExecuter
39         {
40                 internal static QueryOptions CheckQuery<T> (QueryBaseNode<T> startingNode)
41                 {
42                         return CheckQuery<T> (startingNode, false);
43                 }
44
45                 internal static QueryOptions CheckQuery<T> (QueryBaseNode<T> startingNode, bool blocking)
46                 {
47                         return CheckQuery (startingNode, GetBestWorkerNumber (blocking));
48                 }
49
50                 internal static QueryOptions CheckQuery<T> (QueryBaseNode<T> startingNode, int partitionCount)
51                 {
52                         QueryCheckerVisitor visitor = new QueryCheckerVisitor (partitionCount);
53                         startingNode.Visit (visitor);
54
55                         return visitor.Options;
56                 }
57
58                 internal static CancellationToken Chain (this CancellationToken self, CancellationTokenSource other)
59                 {
60                         CancellationTokenSource linked = CancellationTokenSource.CreateLinkedTokenSource (self, other.Token);
61                         return linked.Token;
62                 }
63
64                 internal static bool IsOrdered<TSource> (this QueryBaseNode<TSource> source)
65                 {
66                         QueryIsOrderedVisitor visitor = new QueryIsOrderedVisitor ();
67                         source.Visit (visitor);
68
69                         return visitor.BehindOrderGuard;
70                 }
71
72                 internal static int GetBestWorkerNumber ()
73                 {
74                         return GetBestWorkerNumber (false);
75                 }
76
77                 internal static int GetBestWorkerNumber (bool blocking)
78                 {
79                         return blocking && Task.CurrentId == null ? Environment.ProcessorCount + 1 : Environment.ProcessorCount;
80                 }
81
82                 internal static Task[] Process<TSource, TElement> (QueryBaseNode<TSource> node,
83                                                                    Action<TElement, CancellationToken> call,
84                                                                    Func<QueryBaseNode<TSource>, QueryOptions, IList<IEnumerable<TElement>>> acquisitionFunc,
85                                                                    QueryOptions options)
86                 {
87                         return Process<TSource, TElement> (node, call, acquisitionFunc, null, options);
88                 }
89
90                 internal static Task[] Process<TSource, TElement> (QueryBaseNode<TSource> node,
91                                                                    Action<TElement, CancellationToken> call,
92                                                                    Func<QueryBaseNode<TSource>, QueryOptions, IList<IEnumerable<TElement>>> acquisitionFunc,
93                                                                    Action endAction,
94                                                                    QueryOptions options)
95                 {
96                         CancellationTokenSource src
97                                 = CancellationTokenSource.CreateLinkedTokenSource (options.ImplementerToken, options.Token);
98
99                         IList<IEnumerable<TElement>> enumerables = acquisitionFunc (node, options);
100
101                         Task[] tasks = new Task[enumerables.Count];
102
103                         for (int i = 0; i < tasks.Length; i++) {
104                                 int index = i;
105                                 tasks[i] = Task.Factory.StartNew (() => {
106                                         try {
107                                                 foreach (TElement item in enumerables[index]) {
108                                                         if (!CheckTokens (options))
109                                                                 break;
110
111                                                         try {
112                                                                 call (item, src.Token);
113                                                         } catch (OperationCanceledException canceledException) {
114                                                                 if (canceledException.CancellationToken != src.Token)
115                                                                         throw canceledException;
116                                                         }
117
118                                                         if (!CheckTokens (options))
119                                                                 break;
120                                                 }
121                                         } finally {
122                                                 if (endAction != null)
123                                                         endAction ();
124                                         }
125                                 }, options.Token, TaskCreationOptions.AttachedToParent | TaskCreationOptions.LongRunning, TaskScheduler.Default);
126                         }
127
128                         return tasks;
129                 }
130
131                 static bool CheckTokens (QueryOptions options)
132                 {
133                         // This is from specific operators
134                         if (options.ImplementerToken.IsCancellationRequested)
135                                 return false;
136                         if (options.Token.IsCancellationRequested)
137                                 throw new OperationCanceledException (options.Token);
138                         return true;
139                 }
140
141                 internal static void ProcessAndBlock<T> (QueryBaseNode<T> node, Action<T, CancellationToken> call)
142                 {
143                         QueryOptions options = CheckQuery (node, true);
144
145                         Task[] tasks = Process (node, call, new QueryBaseNodeHelper<T> ().GetEnumerables, options);
146                         Task.WaitAll (tasks, options.Token);
147                 }
148
149                 internal static Action ProcessAndCallback<T> (QueryBaseNode<T> node, Action<T, CancellationToken> call,
150                                                               Action callback, QueryOptions options)
151                 {
152                         Task[] tasks = Process (node, call, new QueryBaseNodeHelper<T> ().GetEnumerables, options);
153                         if (callback != null)
154                                 Task.Factory.ContinueWhenAll (tasks,  (_) => callback ());
155
156                         return () => Task.WaitAll (tasks, options.Token);
157                 }
158
159                 internal static Action ProcessAndCallback<T> (QueryBaseNode<T> node, Action<KeyValuePair<long, T>, CancellationToken> call,
160                                                               Action endAction,
161                                                               Action callback, QueryOptions options)
162                 {
163                         Task[] tasks = Process (node, call, new QueryBaseNodeHelper<T> ().GetOrderedEnumerables, endAction, options);
164                         if (callback != null)
165                                 Task.Factory.ContinueWhenAll (tasks,  (_) => callback ());
166
167                         return () => Task.WaitAll (tasks, options.Token);
168                 }
169
170                 internal static void ProcessAndAggregate<T, U> (QueryBaseNode<T> node,
171                                                                 Func<U> seedFunc,
172                                                                 Func<U, T, U> localCall,
173                                                                 Action<IList<U>> call)
174                 {
175                         QueryOptions options = CheckQuery (node, true);
176
177                         IList<IEnumerable<T>> enumerables = node.GetEnumerables (options);
178                         U[] locals = new U[enumerables.Count];
179                         Task[] tasks = new Task[enumerables.Count];
180
181                         if (seedFunc != null) {
182                                 for (int i = 0; i < locals.Length; i++)
183                                         locals[i] = seedFunc ();
184                         }
185
186                         for (int i = 0; i < tasks.Length; i++) {
187                                 var procSlot = new AggregateProcessSlot<T, U> (options,
188                                                                                i,
189                                                                                enumerables[i].GetEnumerator (),
190                                                                                locals,
191                                                                                localCall,
192                                                                                seedFunc);
193
194                                 tasks[i] = Task.Factory.StartNew (procSlot.Process, options.Token);
195                         }
196
197                         Task.WaitAll (tasks, options.Token);
198
199                         if (call != null)
200                                 call (locals);
201                 }
202
203                 class AggregateProcessSlot<T, U>
204                 {
205                         readonly QueryOptions options;
206                         readonly int index;
207                         readonly IEnumerator<T> enumerator;
208                         readonly U[] locals;
209                         readonly Func<U, T, U> localCall;
210                         readonly Func<U> seedFunc;
211
212                         public AggregateProcessSlot (QueryOptions options,
213                                                      int index,
214                                                      IEnumerator<T> enumerator,
215                                                      U[] locals,
216                                                      Func<U, T, U> localCall,
217                                                      Func<U> seedFunc)
218                         {
219                                 this.options = options;
220                                 this.index = index;
221                                 this.enumerator = enumerator;
222                                 this.locals = locals;
223                                 this.localCall = localCall;
224                                 this.seedFunc = seedFunc;
225                         }
226
227                         public void Process ()
228                         {
229                                 var token = options.Token;
230                                 var implementerToken = options.ImplementerToken;
231
232                                 try {
233                                         if (seedFunc == null) {
234                                                 if (!enumerator.MoveNext ())
235                                                         return;
236                                                 locals[index] = (U)(object)enumerator.Current;
237                                         }
238
239                                         while (enumerator.MoveNext ()) {
240                                                 if (implementerToken.IsCancellationRequested)
241                                                         break;
242                                                 token.ThrowIfCancellationRequested ();
243                                                 locals[index] = localCall (locals[index], enumerator.Current);
244                                         }
245                                 } finally {
246                                         enumerator.Dispose ();
247                                 }
248                         }
249                 }
250
251                 class QueryBaseNodeHelper<T>
252                 {
253                         internal IList<IEnumerable<T>> GetEnumerables (QueryBaseNode<T> source, QueryOptions options)
254                         {
255                                 return source.GetEnumerables (options);
256                         }
257
258                         internal IList<IEnumerable<KeyValuePair<long,T>>> GetOrderedEnumerables (QueryBaseNode<T> source, QueryOptions options)
259                         {
260                                 return source.GetOrderedEnumerables (options);
261                         }
262                 }
263         }
264 }
265 #endif