BroadcastBlock.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // BroadcastBlock.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. //
  23. //
  24. using System.Collections.Generic;
  25. using System.Collections.Concurrent;
  26. namespace System.Threading.Tasks.Dataflow {
  27. public sealed class BroadcastBlock<T> : IPropagatorBlock<T, T>, IReceivableSourceBlock<T> {
  28. static readonly DataflowBlockOptions defaultOptions = new DataflowBlockOptions ();
  29. readonly CompletionHelper compHelper;
  30. readonly BlockingCollection<T> messageQueue = new BlockingCollection<T> ();
  31. readonly MessageBox<T> messageBox;
  32. readonly MessageVault<T> vault;
  33. readonly DataflowBlockOptions dataflowBlockOptions;
  34. readonly Func<T, T> cloner;
  35. readonly MessageOutgoingQueue<T> outgoing;
  36. readonly TargetBuffer<T> targets = new TargetBuffer<T> ();
  37. DataflowMessageHeader headers = DataflowMessageHeader.NewValid ();
  38. public BroadcastBlock (Func<T, T> cloner) : this (cloner, defaultOptions)
  39. {
  40. }
  41. public BroadcastBlock (Func<T, T> cloner, DataflowBlockOptions dataflowBlockOptions)
  42. {
  43. if (dataflowBlockOptions == null)
  44. throw new ArgumentNullException ("dataflowBlockOptions");
  45. this.cloner = cloner;
  46. this.dataflowBlockOptions = dataflowBlockOptions;
  47. this.compHelper = CompletionHelper.GetNew (dataflowBlockOptions);
  48. this.messageBox = new PassingMessageBox<T> (this, messageQueue, compHelper,
  49. () => outgoing.IsCompleted, BroadcastProcess, dataflowBlockOptions);
  50. this.outgoing = new MessageOutgoingQueue<T> (this, compHelper,
  51. () => messageQueue.IsCompleted, () => messageBox.DecreaseCount (),
  52. dataflowBlockOptions);
  53. this.vault = new MessageVault<T> ();
  54. }
  55. public DataflowMessageStatus OfferMessage (DataflowMessageHeader messageHeader,
  56. T messageValue,
  57. ISourceBlock<T> source,
  58. bool consumeToAccept)
  59. {
  60. return messageBox.OfferMessage (messageHeader, messageValue, source, consumeToAccept);
  61. }
  62. public IDisposable LinkTo (ITargetBlock<T> target, DataflowLinkOptions linkOptions)
  63. {
  64. return targets.AddTarget (target, false); // TODO
  65. }
  66. public T ConsumeMessage (DataflowMessageHeader messageHeader, ITargetBlock<T> target, out bool messageConsumed)
  67. {
  68. return cloner(vault.ConsumeMessage (messageHeader, target, out messageConsumed));
  69. }
  70. public void ReleaseReservation (DataflowMessageHeader messageHeader, ITargetBlock<T> target)
  71. {
  72. vault.ReleaseReservation (messageHeader, target);
  73. }
  74. public bool ReserveMessage (DataflowMessageHeader messageHeader, ITargetBlock<T> target)
  75. {
  76. return vault.ReserveMessage (messageHeader, target);
  77. }
  78. public bool TryReceive (Predicate<T> filter, out T item)
  79. {
  80. return outgoing.TryReceive (filter, out item);
  81. }
  82. public bool TryReceiveAll (out IList<T> items)
  83. {
  84. return outgoing.TryReceiveAll (out items);
  85. }
  86. void BroadcastProcess ()
  87. {
  88. T input;
  89. if (!messageQueue.TryTake (out input) || targets.Current == null)
  90. return;
  91. foreach (var target in targets) {
  92. DataflowMessageHeader header = headers.Increment ();
  93. if (cloner != null)
  94. vault.StoreMessage (header, input);
  95. target.OfferMessage (header, input, this, cloner != null);
  96. // TODO: verify if it's the correct semantic
  97. T save = input;
  98. if (!messageQueue.TryTake (out input))
  99. input = save;
  100. }
  101. }
  102. public void Complete ()
  103. {
  104. messageBox.Complete ();
  105. }
  106. public void Fault (Exception ex)
  107. {
  108. compHelper.RequestFault (ex);
  109. }
  110. public Task Completion {
  111. get {
  112. return compHelper.Completion;
  113. }
  114. }
  115. public override string ToString ()
  116. {
  117. return NameHelper.GetName (this, dataflowBlockOptions);
  118. }
  119. }
  120. }