3 // Copyright (c) 2011 Jérémie "garuma" Laval
4 // Copyright (c) 2012 Petr Onderka
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 using System.Collections.Generic;
25 using System.Collections.Concurrent;
27 namespace System.Threading.Tasks.Dataflow {
28 public sealed class BroadcastBlock<T> : IPropagatorBlock<T, T>, IReceivableSourceBlock<T> {
29 readonly CompletionHelper compHelper;
30 readonly BlockingCollection<T> messageQueue = new BlockingCollection<T> ();
31 readonly MessageBox<T> messageBox;
32 readonly DataflowBlockOptions dataflowBlockOptions;
33 readonly Func<T, T> cloningFunction;
34 readonly BroadcastOutgoingQueue<T> outgoing;
36 public BroadcastBlock (Func<T, T> cloningFunction)
37 : this (cloningFunction, DataflowBlockOptions.Default)
41 public BroadcastBlock (Func<T, T> cloningFunction,
42 DataflowBlockOptions dataflowBlockOptions)
44 if (dataflowBlockOptions == null)
45 throw new ArgumentNullException ("dataflowBlockOptions");
47 this.cloningFunction = cloningFunction;
48 this.dataflowBlockOptions = dataflowBlockOptions;
49 this.compHelper = CompletionHelper.GetNew (dataflowBlockOptions);
50 this.messageBox = new PassingMessageBox<T> (this, messageQueue, compHelper,
51 () => outgoing.IsCompleted, _ => BroadcastProcess (), dataflowBlockOptions);
52 this.outgoing = new BroadcastOutgoingQueue<T> (this, compHelper,
53 () => messageQueue.IsCompleted, messageBox.DecreaseCount,
54 dataflowBlockOptions, cloningFunction != null);
57 DataflowMessageStatus ITargetBlock<T>.OfferMessage (
58 DataflowMessageHeader messageHeader, T messageValue, ISourceBlock<T> source,
61 return messageBox.OfferMessage (messageHeader, messageValue, source,
65 public IDisposable LinkTo (ITargetBlock<T> target, DataflowLinkOptions linkOptions)
67 if (linkOptions == null)
68 throw new ArgumentNullException("linkOptions");
70 return outgoing.AddTarget (target, linkOptions);
73 T ISourceBlock<T>.ConsumeMessage (DataflowMessageHeader messageHeader,
74 ITargetBlock<T> target,
75 out bool messageConsumed)
77 T message = outgoing.ConsumeMessage (
78 messageHeader, target, out messageConsumed);
79 if (messageConsumed && cloningFunction != null)
80 message = cloningFunction (message);
84 bool ISourceBlock<T>.ReserveMessage (DataflowMessageHeader messageHeader,
85 ITargetBlock<T> target)
87 return outgoing.ReserveMessage (messageHeader, target);
90 void ISourceBlock<T>.ReleaseReservation (DataflowMessageHeader messageHeader,
91 ITargetBlock<T> target)
93 outgoing.ReleaseReservation (messageHeader, target);
96 public bool TryReceive (Predicate<T> filter, out T item)
98 var received = outgoing.TryReceive (filter, out item);
99 if (received && cloningFunction != null)
100 item = cloningFunction (item);
104 bool IReceivableSourceBlock<T>.TryReceiveAll (out IList<T> items)
107 if (!TryReceive (null, out item)) {
112 items = new[] { item };
117 /// Moves items from the input queue to the output queue.
119 void BroadcastProcess ()
122 while (messageQueue.TryTake (out item))
123 outgoing.AddData (item);
126 public void Complete ()
128 messageBox.Complete ();
129 outgoing.Complete ();
132 void IDataflowBlock.Fault (Exception exception)
134 compHelper.RequestFault (exception);
137 public Task Completion {
138 get { return compHelper.Completion; }
141 public override string ToString ()
143 return NameHelper.GetName (this, dataflowBlockOptions);