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 BufferBlock<T> : IPropagatorBlock<T, T>, ITargetBlock<T>, IDataflowBlock, ISourceBlock<T>, IReceivableSourceBlock<T>
35 static readonly DataflowBlockOptions defaultOptions = new DataflowBlockOptions ();
37 DataflowBlockOptions dataflowBlockOptions;
38 CompletionHelper compHelper = CompletionHelper.GetNew ();
39 MessageBox<T> messageBox;
40 MessageVault<T> vault;
41 MessageOutgoingQueue<T> outgoing;
42 BlockingCollection<T> messageQueue = new BlockingCollection<T> ();
43 TargetBuffer<T> targets = new TargetBuffer<T> ();
44 DataflowMessageHeader headers = DataflowMessageHeader.NewValid ();
46 public BufferBlock () : this (defaultOptions)
51 public BufferBlock (DataflowBlockOptions dataflowBlockOptions)
53 if (dataflowBlockOptions == null)
54 throw new ArgumentNullException ("dataflowBlockOptions");
56 this.dataflowBlockOptions = dataflowBlockOptions;
57 this.messageBox = new PassingMessageBox<T> (messageQueue, compHelper, () => outgoing.IsCompleted, ProcessQueue, dataflowBlockOptions);
58 this.outgoing = new MessageOutgoingQueue<T> (compHelper, () => messageQueue.IsCompleted);
59 this.vault = new MessageVault<T> ();
62 public DataflowMessageStatus OfferMessage (DataflowMessageHeader messageHeader,
64 ISourceBlock<T> source,
67 return messageBox.OfferMessage (this, messageHeader, messageValue, source, consumeToAccept);
70 public IDisposable LinkTo (ITargetBlock<T> target, bool unlinkAfterOne)
72 var result = targets.AddTarget (target, unlinkAfterOne);
78 public T ConsumeMessage (DataflowMessageHeader messageHeader, ITargetBlock<T> target, out bool messageConsumed)
80 return vault.ConsumeMessage (messageHeader, target, out messageConsumed);
83 public void ReleaseReservation (DataflowMessageHeader messageHeader, ITargetBlock<T> target)
85 vault.ReleaseReservation (messageHeader, target);
88 public bool ReserveMessage (DataflowMessageHeader messageHeader, ITargetBlock<T> target)
90 return vault.ReserveMessage (messageHeader, target);
93 public bool TryReceive (Predicate<T> filter, out T item)
95 return outgoing.TryReceive (filter, out item);
98 public bool TryReceiveAll (out IList<T> items)
100 return outgoing.TryReceiveAll (out items);
105 ITargetBlock<T> target;
108 while (messageQueue.TryTake (out input)) {
109 if ((target = targets.Current) != null)
110 target.OfferMessage (headers.Increment (), input, this, false);
112 outgoing.AddData (input);
115 if (!outgoing.IsEmpty && (target = targets.Current) != null)
116 outgoing.ProcessForTarget (target, this, false, ref headers);
119 public void Complete ()
121 messageBox.Complete ();
122 outgoing.Complete ();
125 public void Fault (Exception ex)
127 compHelper.Fault (ex);
130 public Task Completion {
132 return compHelper.Completion;
138 return outgoing.Count;
142 public override string ToString ()
144 return NameHelper.GetName (this, dataflowBlockOptions);