do not check order sequence if option /order was not used
[mono.git] / mcs / class / System.Core / System.Linq.Parallel / QueryOptions.cs
1 //
2 // QueryOptions.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
31 namespace System.Linq.Parallel
32 {
33         internal class QueryOptions
34         {
35                 public ParallelMergeOptions? Options {
36                         get;
37                         private set;
38                 }
39
40                 public ParallelExecutionMode? Mode {
41                         get;
42                         private set;
43                 }
44
45                 public CancellationToken Token {
46                         get;
47                         private set;
48                 }
49                 /* This token is to be used by some operator (like Take) to tell that
50                  * the execution of the query can be prematurly stopped
51                  *
52                  * It is set when passing QueryOptions to the different node's Get method
53                  * and ParallelExecuter should check after the call to this method is this guy has been
54                  * set. Operator may chain up multiple cancellation token that way.
55                  * When checking for this token, the task body should simply return.
56                  */
57                 public CancellationToken ImplementerToken {
58                         get;
59                         private set;
60                 }
61
62                 public bool UseStrip {
63                         get;
64                         private set;
65                 }
66
67                 public bool? BehindOrderGuard {
68                         get;
69                         private set;
70                 }
71
72                 public int PartitionCount {
73                         get;
74                         private set;
75                 }
76
77                 public Tuple<bool, bool, bool> PartitionerSettings {
78                         get;
79                         internal set;
80                 }
81
82                 public CancellationToken MergedToken {
83                         get;
84                         private set;
85                 }
86
87                 public bool ShouldBeSequential {
88                         get;
89                         private set;
90                 }
91
92                 public QueryOptions (ParallelMergeOptions? options,
93                                      ParallelExecutionMode? mode,
94                                      CancellationToken token,
95                                      bool useStrip,
96                                      bool? behindOrderGuard,
97                                      int partitionCount,
98                                      CancellationToken implementerToken,
99                                      bool shouldBeSequential)
100                 {
101                         Options = options;
102                         Mode = mode;
103                         Token = token;
104                         UseStrip = useStrip;
105                         BehindOrderGuard = behindOrderGuard;
106                         PartitionCount = partitionCount;
107                         PartitionerSettings = null;
108                         ImplementerToken = implementerToken;
109                         ShouldBeSequential = shouldBeSequential;
110
111                         MergeTokens (token, implementerToken);
112                 }
113
114                 void MergeTokens (CancellationToken token, CancellationToken implementerToken)
115                 {
116                         bool implementedNone = implementerToken == CancellationToken.None;
117                         bool tokenNone = token == CancellationToken.None;
118                         if (!implementedNone && !tokenNone)
119                                 MergedToken = CancellationTokenSource.CreateLinkedTokenSource (implementerToken, token).Token;
120                         else if (implementedNone && !tokenNone)
121                                 MergedToken = token;
122                         else if (!implementedNone && tokenNone)
123                                 MergedToken = implementerToken;
124                         else
125                                 MergedToken = CancellationToken.None;
126                 }
127         }
128 }
129 #endif