QueryOptions.cs 3.6 KB

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