System.Drawing: added email to icon and test file headers
[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 //
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 BroadcastBlock<T> : IPropagatorBlock<T, T>, ITargetBlock<T>, IDataflowBlock, ISourceBlock<T>, IReceivableSourceBlock<T>
34         {
35                 static readonly DataflowBlockOptions defaultOptions = new DataflowBlockOptions ();
36
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 ();
46
47                 public BroadcastBlock (Func<T, T> cloner) : this (cloner, defaultOptions)
48                 {
49
50                 }
51
52                 public BroadcastBlock (Func<T, T> cloner, DataflowBlockOptions dataflowBlockOptions)
53                 {
54                         if (dataflowBlockOptions == null)
55                                 throw new ArgumentNullException ("dataflowBlockOptions");
56
57                         this.cloner = cloner;
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> ();
62                 }
63
64                 public DataflowMessageStatus OfferMessage (DataflowMessageHeader messageHeader,
65                                                            T messageValue,
66                                                            ISourceBlock<T> source,
67                                                            bool consumeToAccept)
68                 {
69                         return messageBox.OfferMessage (this, messageHeader, messageValue, source, consumeToAccept);
70                 }
71
72                 public IDisposable LinkTo (ITargetBlock<T> target, bool unlinkAfterOne)
73                 {
74                         return targets.AddTarget (target, unlinkAfterOne);
75                 }
76
77                 public T ConsumeMessage (DataflowMessageHeader messageHeader, ITargetBlock<T> target, out bool messageConsumed)
78                 {
79                         return cloner(vault.ConsumeMessage (messageHeader, target, out messageConsumed));
80                 }
81
82                 public void ReleaseReservation (DataflowMessageHeader messageHeader, ITargetBlock<T> target)
83                 {
84                         vault.ReleaseReservation (messageHeader, target);
85                 }
86
87                 public bool ReserveMessage (DataflowMessageHeader messageHeader, ITargetBlock<T> target)
88                 {
89                         return vault.ReserveMessage (messageHeader, target);
90                 }
91
92                 public bool TryReceive (Predicate<T> filter, out T item)
93                 {
94                         return outgoing.TryReceive (filter, out item);
95                 }
96
97                 public bool TryReceiveAll (out IList<T> items)
98                 {
99                         return outgoing.TryReceiveAll (out items);
100                 }
101
102                 void BroadcastProcess ()
103                 {
104                         T input;
105
106                         if (!messageQueue.TryTake (out input) || targets.Current == null)
107                                 return;
108
109                         foreach (var target in targets) {
110                                 DataflowMessageHeader header = headers.Increment ();
111                                 if (cloner != null)
112                                         vault.StoreMessage (header, input);
113                                 target.OfferMessage (header, input, this, cloner != null);
114                                 // TODO: verify if it's the correct semantic
115                                 T save = input;
116                                 if (!messageQueue.TryTake (out input))
117                                         input = save;
118                         }
119                 }
120
121                 public void Complete ()
122                 {
123                         messageBox.Complete ();
124                 }
125
126                 public void Fault (Exception ex)
127                 {
128                         compHelper.Fault (ex);
129                 }
130
131                 public Task Completion {
132                         get {
133                                 return compHelper.Completion;
134                         }
135                 }
136         }
137 }
138