3 // Copyright (c) 2011 Jérémie "garuma" Laval
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:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
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
27 using System.Threading.Tasks;
28 using System.Collections.Generic;
29 using System.Collections.Concurrent;
31 namespace System.Threading.Tasks.Dataflow
33 public sealed class BroadcastBlock<T> : IPropagatorBlock<T, T>, ITargetBlock<T>, IDataflowBlock, ISourceBlock<T>, IReceivableSourceBlock<T>
35 static readonly DataflowBlockOptions defaultOptions = new DataflowBlockOptions ();
37 CompletionHelper compHelper = CompletionHelper.GetNew ();
38 BlockingCollection<T> messageQueue = new BlockingCollection<T> ();
39 MessageBox<T> messageBox;
40 MessageVault<T> vault;
41 DataflowBlockOptions dataflowBlockOptions;
42 readonly Func<T, T> cloner;
43 MessageOutgoingQueue<T> outgoing;
44 TargetBuffer<T> targets = new TargetBuffer<T> ();
45 DataflowMessageHeader headers = DataflowMessageHeader.NewValid ();
47 public BroadcastBlock (Func<T, T> cloner) : this (cloner, defaultOptions)
52 public BroadcastBlock (Func<T, T> cloner, DataflowBlockOptions dataflowBlockOptions)
54 if (dataflowBlockOptions == null)
55 throw new ArgumentNullException ("dataflowBlockOptions");
58 this.dataflowBlockOptions = dataflowBlockOptions;
59 this.messageBox = new PassingMessageBox<T> (messageQueue, compHelper, () => outgoing.IsCompleted, BroadcastProcess, dataflowBlockOptions);
60 this.outgoing = new MessageOutgoingQueue<T> (compHelper, () => messageQueue.IsCompleted);
61 this.vault = new MessageVault<T> ();
64 public DataflowMessageStatus OfferMessage (DataflowMessageHeader messageHeader,
66 ISourceBlock<T> source,
69 return messageBox.OfferMessage (this, messageHeader, messageValue, source, consumeToAccept);
72 public IDisposable LinkTo (ITargetBlock<T> target, bool unlinkAfterOne)
74 return targets.AddTarget (target, unlinkAfterOne);
77 public T ConsumeMessage (DataflowMessageHeader messageHeader, ITargetBlock<T> target, out bool messageConsumed)
79 return cloner(vault.ConsumeMessage (messageHeader, target, out messageConsumed));
82 public void ReleaseReservation (DataflowMessageHeader messageHeader, ITargetBlock<T> target)
84 vault.ReleaseReservation (messageHeader, target);
87 public bool ReserveMessage (DataflowMessageHeader messageHeader, ITargetBlock<T> target)
89 return vault.ReserveMessage (messageHeader, target);
92 public bool TryReceive (Predicate<T> filter, out T item)
94 return outgoing.TryReceive (filter, out item);
97 public bool TryReceiveAll (out IList<T> items)
99 return outgoing.TryReceiveAll (out items);
102 void BroadcastProcess ()
106 if (!messageQueue.TryTake (out input) || targets.Current == null)
109 foreach (var target in targets) {
110 DataflowMessageHeader header = headers.Increment ();
112 vault.StoreMessage (header, input);
113 target.OfferMessage (header, input, this, cloner != null);
114 // TODO: verify if it's the correct semantic
116 if (!messageQueue.TryTake (out input))
121 public void Complete ()
123 messageBox.Complete ();
126 public void Fault (Exception ex)
128 compHelper.Fault (ex);
131 public Task Completion {
133 return compHelper.Completion;