Merge pull request #2228 from lambdageek/dev/sgen-timeouts
[mono.git] / mcs / class / System.Threading.Tasks.Dataflow / System.Threading.Tasks.Dataflow / ActionBlock.cs
1 // ActionBlock.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.Concurrent;
25
26 namespace System.Threading.Tasks.Dataflow {
27         public sealed class ActionBlock<TInput> : ITargetBlock<TInput> {
28                 readonly CompletionHelper compHelper;
29                 readonly BlockingCollection<TInput> messageQueue = new BlockingCollection<TInput> ();
30                 readonly ExecutingMessageBoxBase<TInput> messageBox;
31                 readonly Action<TInput> action;
32                 readonly Func<TInput, Task> asyncAction;
33                 readonly ExecutionDataflowBlockOptions dataflowBlockOptions;
34
35                 ActionBlock (ExecutionDataflowBlockOptions dataflowBlockOptions)
36                 {
37                         if (dataflowBlockOptions == null)
38                                 throw new ArgumentNullException ("dataflowBlockOptions");
39
40                         this.dataflowBlockOptions = dataflowBlockOptions;
41                         this.compHelper = new CompletionHelper (dataflowBlockOptions);
42                 }
43
44                 public ActionBlock (Action<TInput> action)
45                         : this (action, ExecutionDataflowBlockOptions.Default)
46                 {
47                 }
48
49                 public ActionBlock (Action<TInput> action,
50                                     ExecutionDataflowBlockOptions dataflowBlockOptions)
51                         : this (dataflowBlockOptions)
52                 {
53                         if (action == null)
54                                 throw new ArgumentNullException ("action");
55
56                         this.action = action;
57                         this.messageBox = new ExecutingMessageBox<TInput> (this, messageQueue, compHelper,
58                                 () => true, ProcessItem, () => { }, dataflowBlockOptions);
59                 }
60
61                 public ActionBlock (Func<TInput, Task> action)
62                         : this (action, ExecutionDataflowBlockOptions.Default)
63                 {
64                 }
65
66                 public ActionBlock (Func<TInput, Task> action,
67                                     ExecutionDataflowBlockOptions dataflowBlockOptions)
68                         : this (dataflowBlockOptions)
69                 {
70                         if (action == null)
71                                 throw new ArgumentNullException ("action");
72
73                         this.asyncAction = action;
74                         this.messageBox = new AsyncExecutingMessageBox<TInput, Task> (
75                                 this, messageQueue, compHelper, () => true, AsyncProcessItem, null,
76                                 () => { }, dataflowBlockOptions);
77                 }
78
79                 DataflowMessageStatus ITargetBlock<TInput>.OfferMessage (
80                         DataflowMessageHeader messageHeader, TInput messageValue,
81                         ISourceBlock<TInput> source, bool consumeToAccept)
82                 {
83                         return messageBox.OfferMessage (
84                                 messageHeader, messageValue, source, consumeToAccept);
85                 }
86
87                 public bool Post (TInput item)
88                 {
89                         return messageBox.OfferMessage (
90                                 new DataflowMessageHeader (1), item, null, false)
91                                == DataflowMessageStatus.Accepted;
92                 }
93
94                 /// <summary>
95                 /// Processes one item from the queue if the action is synchronous.
96                 /// </summary>
97                 /// <returns>Returns whether an item was processed. Returns <c>false</c> if the queue is empty.</returns>
98                 bool ProcessItem ()
99                 {
100                         TInput data;
101                         bool dequeued = messageQueue.TryTake (out data);
102                         if (dequeued)
103                                 action (data);
104                         return dequeued;
105                 }
106
107                 /// <summary>
108                 /// Processes one item from the queue if the action is asynchronous.
109                 /// </summary>
110                 /// <param name="task">The Task that was returned by the synchronous part of the action.</param>
111                 /// <returns>Returns whether an item was processed. Returns <c>false</c> if the queue was empty.</returns>
112                 bool AsyncProcessItem(out Task task)
113                 {
114                         TInput data;
115                         bool dequeued = messageQueue.TryTake (out data);
116                         if (dequeued)
117                                 task = asyncAction (data);
118                         else
119                                 task = null;
120                         return dequeued;
121                 }
122
123                 public void Complete ()
124                 {
125                         messageBox.Complete ();
126                 }
127
128                 void IDataflowBlock.Fault (Exception exception)
129                 {
130                         compHelper.RequestFault (exception);
131                 }
132
133                 public Task Completion {
134                         get {
135                                 return compHelper.Completion;
136                         }
137                 }
138
139                 public int InputCount {
140                         get {
141                                 return messageQueue.Count;
142                         }
143                 }
144
145                 public override string ToString ()
146                 {
147                         return NameHelper.GetName (this, dataflowBlockOptions);
148                 }
149         }
150 }