ParallelLoopState.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // ParallelState.cs
  2. //
  3. // Copyright (c) 2008 Jérémie "Garuma" Laval
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. //
  24. #if NET_4_0
  25. using System;
  26. using System.Threading;
  27. namespace System.Threading.Tasks
  28. {
  29. [System.Diagnostics.DebuggerDisplayAttribute ("ShouldExitCurrentIteration = {ShouldExitCurrentIteration}")]
  30. public class ParallelLoopState
  31. {
  32. internal class ExternalInfos
  33. {
  34. public bool IsStopped;
  35. public AtomicBooleanValue IsBroken = new AtomicBooleanValue ();
  36. public volatile bool IsExceptional;
  37. public long? LowestBreakIteration;
  38. }
  39. ExternalInfos extInfos;
  40. internal ParallelLoopState (ExternalInfos extInfos)
  41. {
  42. this.extInfos = extInfos;
  43. }
  44. public bool IsStopped {
  45. get {
  46. return extInfos.IsStopped;
  47. }
  48. }
  49. public bool IsExceptional {
  50. get {
  51. return extInfos.IsExceptional;
  52. }
  53. }
  54. public long? LowestBreakIteration {
  55. get {
  56. return extInfos.LowestBreakIteration;
  57. }
  58. }
  59. internal int CurrentIteration {
  60. get;
  61. set;
  62. }
  63. public bool ShouldExitCurrentIteration {
  64. get {
  65. return IsExceptional || IsStopped;
  66. }
  67. }
  68. public void Break ()
  69. {
  70. if (extInfos.IsStopped)
  71. throw new InvalidOperationException ("The Stop method was previously called. Break and Stop may not be used in combination by iterations of the same loop.");
  72. bool result = extInfos.IsBroken.Exchange (true);
  73. if (!result)
  74. extInfos.LowestBreakIteration = CurrentIteration;
  75. }
  76. public void Stop ()
  77. {
  78. if (extInfos.IsBroken.Value)
  79. throw new InvalidOperationException ("The Break method was previously called. Break and Stop may not be used in combination by iterations of the same loop.");
  80. extInfos.IsStopped = true;
  81. }
  82. }
  83. }
  84. #endif