QueryOptions.cs 3.6 KB

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