ExecutingMessageBox.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // ExecutingMessageBox.cs
  2. //
  3. // Copyright (c) 2011 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. using System.Collections.Concurrent;
  23. namespace System.Threading.Tasks.Dataflow
  24. {
  25. internal class ExecutingMessageBox<TInput> : MessageBox<TInput>
  26. {
  27. readonly ExecutionDataflowBlockOptions options;
  28. readonly Func<bool> processItem;
  29. readonly Action outgoingQueueComplete;
  30. readonly CompletionHelper compHelper;
  31. // even number: Task is waiting to run
  32. // odd number: Task is not waiting to run
  33. // invariant: dop / 2 Tasks are running or waiting
  34. int degreeOfParallelism = 1;
  35. public ExecutingMessageBox (
  36. BlockingCollection<TInput> messageQueue, CompletionHelper compHelper,
  37. Func<bool> externalCompleteTester, Func<bool> processItem, Action outgoingQueueComplete,
  38. ExecutionDataflowBlockOptions options)
  39. : base (messageQueue, compHelper, externalCompleteTester)
  40. {
  41. this.options = options;
  42. this.processItem = processItem;
  43. this.outgoingQueueComplete = outgoingQueueComplete;
  44. this.compHelper = compHelper;
  45. }
  46. protected override void EnsureProcessing ()
  47. {
  48. StartProcessing ();
  49. }
  50. void StartProcessing ()
  51. {
  52. // atomically increase degreeOfParallelism by 1 only if it's odd
  53. // and low enough
  54. int startDegreeOfParallelism;
  55. int currentDegreeOfParallelism = degreeOfParallelism;
  56. do {
  57. startDegreeOfParallelism = currentDegreeOfParallelism;
  58. if (startDegreeOfParallelism % 2 == 0
  59. || (options.MaxDegreeOfParallelism != DataflowBlockOptions.Unbounded
  60. && startDegreeOfParallelism / 2 >= options.MaxDegreeOfParallelism))
  61. return;
  62. currentDegreeOfParallelism =
  63. Interlocked.CompareExchange (ref degreeOfParallelism,
  64. startDegreeOfParallelism + 1, startDegreeOfParallelism);
  65. } while (startDegreeOfParallelism != currentDegreeOfParallelism);
  66. Task.Factory.StartNew (ProcessQueue, TaskCreationOptions.PreferFairness);
  67. }
  68. void ProcessQueue ()
  69. {
  70. compHelper.CanFaultOrCancelImmediatelly = false;
  71. int incrementedDegreeOfParallelism =
  72. Interlocked.Increment (ref degreeOfParallelism);
  73. if ((options.MaxDegreeOfParallelism == DataflowBlockOptions.Unbounded
  74. || incrementedDegreeOfParallelism / 2 < options.MaxDegreeOfParallelism)
  75. && MessageQueue.Count > 0 && compHelper.CanRun)
  76. StartProcessing ();
  77. try {
  78. int i = 0;
  79. while (compHelper.CanRun
  80. && (options.MaxMessagesPerTask == DataflowBlockOptions.Unbounded
  81. || i++ < options.MaxMessagesPerTask)) {
  82. if (!processItem ())
  83. break;
  84. }
  85. } catch (Exception e) {
  86. compHelper.RequestFault (e);
  87. }
  88. int decrementedDegreeOfParallelism =
  89. Interlocked.Add (ref degreeOfParallelism, -2);
  90. if (decrementedDegreeOfParallelism % 2 == 1) {
  91. if (decrementedDegreeOfParallelism == 1) {
  92. compHelper.CanFaultOrCancelImmediatelly = true;
  93. base.VerifyCompleteness ();
  94. if (MessageQueue.IsCompleted)
  95. outgoingQueueComplete ();
  96. }
  97. if (MessageQueue.Count > 0)
  98. EnsureProcessing ();
  99. }
  100. }
  101. protected override void OutgoingQueueComplete ()
  102. {
  103. if (MessageQueue.IsCompleted
  104. && Thread.VolatileRead (ref degreeOfParallelism) == 1)
  105. outgoingQueueComplete ();
  106. }
  107. protected override void VerifyCompleteness ()
  108. {
  109. if (Thread.VolatileRead (ref degreeOfParallelism) == 1)
  110. base.VerifyCompleteness ();
  111. }
  112. }
  113. }