JoinBlock`3.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // JoinBlock`3.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.Generic;
  23. namespace System.Threading.Tasks.Dataflow
  24. {
  25. public sealed class JoinBlock<T1, T2, T3> : IReceivableSourceBlock<Tuple<T1, T2, T3>>
  26. {
  27. static readonly GroupingDataflowBlockOptions defaultOptions = new GroupingDataflowBlockOptions ();
  28. readonly CompletionHelper compHelper;
  29. readonly GroupingDataflowBlockOptions dataflowBlockOptions;
  30. readonly MessageOutgoingQueue<Tuple<T1, T2, T3>> outgoing;
  31. readonly JoinTarget<T1> target1;
  32. readonly JoinTarget<T2> target2;
  33. readonly JoinTarget<T3> target3;
  34. SpinLock targetLock = new SpinLock (false);
  35. DataflowMessageHeader headers;
  36. public JoinBlock () : this (defaultOptions)
  37. {
  38. }
  39. public JoinBlock (GroupingDataflowBlockOptions dataflowBlockOptions)
  40. {
  41. if (dataflowBlockOptions == null)
  42. throw new ArgumentNullException ("dataflowBlockOptions");
  43. this.dataflowBlockOptions = dataflowBlockOptions;
  44. this.compHelper = CompletionHelper.GetNew (dataflowBlockOptions);
  45. target1 = new JoinTarget<T1> (this, SignalArrivalTargetImpl, compHelper,
  46. () => outgoing.IsCompleted, dataflowBlockOptions);
  47. target2 = new JoinTarget<T2> (this, SignalArrivalTargetImpl, compHelper,
  48. () => outgoing.IsCompleted, dataflowBlockOptions);
  49. target3 = new JoinTarget<T3> (this, SignalArrivalTargetImpl, compHelper,
  50. () => outgoing.IsCompleted, dataflowBlockOptions);
  51. outgoing = new MessageOutgoingQueue<Tuple<T1, T2, T3>> (
  52. this, compHelper,
  53. () => target1.Buffer.IsCompleted || target2.Buffer.IsCompleted
  54. || target3.Buffer.IsCompleted,
  55. () =>
  56. {
  57. target1.DecreaseCount ();
  58. target2.DecreaseCount ();
  59. target3.DecreaseCount ();
  60. }, dataflowBlockOptions);
  61. }
  62. public IDisposable LinkTo (ITargetBlock<Tuple<T1, T2, T3>> target, DataflowLinkOptions linkOptions)
  63. {
  64. return outgoing.AddTarget (target, linkOptions);
  65. }
  66. public bool TryReceive (Predicate<Tuple<T1, T2, T3>> filter, out Tuple<T1, T2, T3> item)
  67. {
  68. return outgoing.TryReceive (filter, out item);
  69. }
  70. public bool TryReceiveAll (out IList<Tuple<T1, T2, T3>> items)
  71. {
  72. return outgoing.TryReceiveAll (out items);
  73. }
  74. public Tuple<T1, T2, T3> ConsumeMessage (DataflowMessageHeader messageHeader, ITargetBlock<Tuple<T1, T2, T3>> target, out bool messageConsumed)
  75. {
  76. return outgoing.ConsumeMessage (messageHeader, target, out messageConsumed);
  77. }
  78. public void ReleaseReservation (DataflowMessageHeader messageHeader, ITargetBlock<Tuple<T1, T2, T3>> target)
  79. {
  80. outgoing.ReleaseReservation (messageHeader, target);
  81. }
  82. public bool ReserveMessage (DataflowMessageHeader messageHeader, ITargetBlock<Tuple<T1, T2, T3>> target)
  83. {
  84. return outgoing.ReserveMessage (messageHeader, target);
  85. }
  86. public void Complete ()
  87. {
  88. outgoing.Complete ();
  89. }
  90. public void Fault (Exception ex)
  91. {
  92. compHelper.RequestFault (ex);
  93. }
  94. public Task Completion {
  95. get {
  96. return compHelper.Completion;
  97. }
  98. }
  99. // TODO : see if we can find a lockless implementation
  100. void SignalArrivalTargetImpl ()
  101. {
  102. bool taken = false;
  103. T1 value1;
  104. T2 value2;
  105. T3 value3;
  106. try {
  107. targetLock.Enter (ref taken);
  108. if (target1.Buffer.Count == 0 || target2.Buffer.Count == 0 || target3.Buffer.Count == 0)
  109. return;
  110. value1 = target1.Buffer.Take ();
  111. value2 = target2.Buffer.Take ();
  112. value3 = target3.Buffer.Take ();
  113. } finally {
  114. if (taken)
  115. targetLock.Exit ();
  116. }
  117. TriggerMessage (value1, value2, value3);
  118. }
  119. void TriggerMessage (T1 val1, T2 val2, T3 val3)
  120. {
  121. outgoing.AddData (Tuple.Create (val1, val2, val3));
  122. }
  123. public ITargetBlock<T1> Target1 {
  124. get {
  125. return target1;
  126. }
  127. }
  128. public ITargetBlock<T2> Target2 {
  129. get {
  130. return target2;
  131. }
  132. }
  133. public ITargetBlock<T3> Target3 {
  134. get {
  135. return target3;
  136. }
  137. }
  138. public override string ToString ()
  139. {
  140. return NameHelper.GetName (this, dataflowBlockOptions);
  141. }
  142. }
  143. }