Started adding documentation for non-public types and methods
[mono.git] / mcs / class / System.Threading.Tasks.Dataflow / System.Threading.Tasks.Dataflow / BroadcastBlock.cs
1 // BroadcastBlock.cs
2 //
3 // Copyright (c) 2011 Jérémie "garuma" Laval
4 // Copyright (c) 2012 Petr Onderka
5 //
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:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
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
22 // THE SOFTWARE.
23
24 using System.Collections.Generic;
25 using System.Collections.Concurrent;
26
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;
35
36                 public BroadcastBlock (Func<T, T> cloningFunction)
37                         : this (cloningFunction, DataflowBlockOptions.Default)
38                 {
39                 }
40
41                 public BroadcastBlock (Func<T, T> cloningFunction,
42                                        DataflowBlockOptions dataflowBlockOptions)
43                 {
44                         if (dataflowBlockOptions == null)
45                                 throw new ArgumentNullException ("dataflowBlockOptions");
46
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);
55                 }
56
57                 DataflowMessageStatus ITargetBlock<T>.OfferMessage (
58                         DataflowMessageHeader messageHeader, T messageValue, ISourceBlock<T> source,
59                         bool consumeToAccept)
60                 {
61                         return messageBox.OfferMessage (messageHeader, messageValue, source,
62                                 consumeToAccept);
63                 }
64
65                 public IDisposable LinkTo (ITargetBlock<T> target, DataflowLinkOptions linkOptions)
66                 {
67                         if (linkOptions == null)
68                                 throw new ArgumentNullException("linkOptions");
69
70                         return outgoing.AddTarget (target, linkOptions);
71                 }
72
73                 T ISourceBlock<T>.ConsumeMessage (DataflowMessageHeader messageHeader,
74                                                   ITargetBlock<T> target,
75                                                   out bool messageConsumed)
76                 {
77                         T message = outgoing.ConsumeMessage (
78                                 messageHeader, target, out messageConsumed);
79                         if (messageConsumed && cloningFunction != null)
80                                 message = cloningFunction (message);
81                         return message;
82                 }
83
84                 bool ISourceBlock<T>.ReserveMessage (DataflowMessageHeader messageHeader,
85                                                      ITargetBlock<T> target)
86                 {
87                         return outgoing.ReserveMessage (messageHeader, target);
88                 }
89
90                 void ISourceBlock<T>.ReleaseReservation (DataflowMessageHeader messageHeader,
91                                                          ITargetBlock<T> target)
92                 {
93                         outgoing.ReleaseReservation (messageHeader, target);
94                 }
95
96                 public bool TryReceive (Predicate<T> filter, out T item)
97                 {
98                         var received = outgoing.TryReceive (filter, out item);
99                         if (received && cloningFunction != null)
100                                 item = cloningFunction (item);
101                         return received;
102                 }
103
104                 bool IReceivableSourceBlock<T>.TryReceiveAll (out IList<T> items)
105                 {
106                         T item;
107                         if (!TryReceive (null, out item)) {
108                                 items = null;
109                                 return false;
110                         }
111
112                         items = new[] { item };
113                         return true;
114                 }
115
116                 /// <summary>
117                 /// Moves items from the input queue to the output queue.
118                 /// </summary>
119                 void BroadcastProcess ()
120                 {
121                         T item;
122                         while (messageQueue.TryTake (out item))
123                                 outgoing.AddData (item);
124                 }
125
126                 public void Complete ()
127                 {
128                         messageBox.Complete ();
129                         outgoing.Complete ();
130                 }
131
132                 void IDataflowBlock.Fault (Exception exception)
133                 {
134                         compHelper.RequestFault (exception);
135                 }
136
137                 public Task Completion {
138                         get { return compHelper.Completion; }
139                 }
140
141                 public override string ToString ()
142                 {
143                         return NameHelper.GetName (this, dataflowBlockOptions);
144                 }
145         }
146 }