Added support for NameFormat to all blocks
[mono.git] / mcs / class / System.Threading.Tasks.Dataflow / System.Threading.Tasks.Dataflow / BufferBlock.cs
1 // BufferBlock.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
25
26 using System;
27 using System.Threading.Tasks;
28 using System.Collections.Generic;
29 using System.Collections.Concurrent;
30
31 namespace System.Threading.Tasks.Dataflow
32 {
33         public sealed class BufferBlock<T> : IPropagatorBlock<T, T>, ITargetBlock<T>, IDataflowBlock, ISourceBlock<T>, IReceivableSourceBlock<T>
34         {
35                 static readonly DataflowBlockOptions defaultOptions = new DataflowBlockOptions ();
36
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 ();
45
46                 public BufferBlock () : this (defaultOptions)
47                 {
48                         
49                 }
50
51                 public BufferBlock (DataflowBlockOptions dataflowBlockOptions)
52                 {
53                         if (dataflowBlockOptions == null)
54                                 throw new ArgumentNullException ("dataflowBlockOptions");
55
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> ();
60                 }
61
62                 public DataflowMessageStatus OfferMessage (DataflowMessageHeader messageHeader,
63                                                            T messageValue,
64                                                            ISourceBlock<T> source,
65                                                            bool consumeToAccept)
66                 {
67                         return messageBox.OfferMessage (this, messageHeader, messageValue, source, consumeToAccept);
68                 }
69
70                 public IDisposable LinkTo (ITargetBlock<T> target, bool unlinkAfterOne)
71                 {
72                         var result = targets.AddTarget (target, unlinkAfterOne);
73                         ProcessQueue ();
74
75                         return result;
76                 }
77
78                 public T ConsumeMessage (DataflowMessageHeader messageHeader, ITargetBlock<T> target, out bool messageConsumed)
79                 {
80                         return vault.ConsumeMessage (messageHeader, target, out messageConsumed);
81                 }
82
83                 public void ReleaseReservation (DataflowMessageHeader messageHeader, ITargetBlock<T> target)
84                 {
85                         vault.ReleaseReservation (messageHeader, target);
86                 }
87
88                 public bool ReserveMessage (DataflowMessageHeader messageHeader, ITargetBlock<T> target)
89                 {
90                         return vault.ReserveMessage (messageHeader, target);
91                 }
92
93                 public bool TryReceive (Predicate<T> filter, out T item)
94                 {
95                         return outgoing.TryReceive (filter, out item);
96                 }
97
98                 public bool TryReceiveAll (out IList<T> items)
99                 {
100                         return outgoing.TryReceiveAll (out items);
101                 }
102
103                 void ProcessQueue ()
104                 {
105                         ITargetBlock<T> target;
106                         T input;
107
108                         while (messageQueue.TryTake (out input)) {
109                                 if ((target = targets.Current) != null)
110                                         target.OfferMessage (headers.Increment (), input, this, false);
111                                 else
112                                         outgoing.AddData (input);
113                         }
114
115                         if (!outgoing.IsEmpty && (target = targets.Current) != null)
116                                 outgoing.ProcessForTarget (target, this, false, ref headers);
117                 }
118
119                 public void Complete ()
120                 {
121                         messageBox.Complete ();
122                         outgoing.Complete ();
123                 }
124
125                 public void Fault (Exception ex)
126                 {
127                         compHelper.Fault (ex);
128                 }
129
130                 public Task Completion {
131                         get {
132                                 return compHelper.Completion;
133                         }
134                 }
135
136                 public int Count {
137                         get {
138                                 return outgoing.Count;
139                         }
140                 }
141
142                 public override string ToString ()
143                 {
144                         return NameHelper.GetName (this, dataflowBlockOptions);
145                 }
146         }
147 }
148