3 // Copyright (c) 2011 Jérémie "garuma" Laval
4 // Copyright (c) 2012 Petr Onderka
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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
24 using System.Collections.Concurrent;
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;
35 ActionBlock (ExecutionDataflowBlockOptions dataflowBlockOptions)
37 if (dataflowBlockOptions == null)
38 throw new ArgumentNullException ("dataflowBlockOptions");
40 this.dataflowBlockOptions = dataflowBlockOptions;
41 this.compHelper = new CompletionHelper (dataflowBlockOptions);
44 public ActionBlock (Action<TInput> action)
45 : this (action, ExecutionDataflowBlockOptions.Default)
49 public ActionBlock (Action<TInput> action,
50 ExecutionDataflowBlockOptions dataflowBlockOptions)
51 : this (dataflowBlockOptions)
54 throw new ArgumentNullException ("action");
57 this.messageBox = new ExecutingMessageBox<TInput> (this, messageQueue, compHelper,
58 () => true, ProcessItem, () => { }, dataflowBlockOptions);
61 public ActionBlock (Func<TInput, Task> action)
62 : this (action, ExecutionDataflowBlockOptions.Default)
66 public ActionBlock (Func<TInput, Task> action,
67 ExecutionDataflowBlockOptions dataflowBlockOptions)
68 : this (dataflowBlockOptions)
71 throw new ArgumentNullException ("action");
73 this.asyncAction = action;
74 this.messageBox = new AsyncExecutingMessageBox<TInput, Task> (
75 this, messageQueue, compHelper, () => true, AsyncProcessItem, null,
76 () => { }, dataflowBlockOptions);
79 DataflowMessageStatus ITargetBlock<TInput>.OfferMessage (
80 DataflowMessageHeader messageHeader, TInput messageValue,
81 ISourceBlock<TInput> source, bool consumeToAccept)
83 return messageBox.OfferMessage (
84 messageHeader, messageValue, source, consumeToAccept);
87 public bool Post (TInput item)
89 return messageBox.OfferMessage (
90 new DataflowMessageHeader (1), item, null, false)
91 == DataflowMessageStatus.Accepted;
95 /// Processes one item from the queue if the action is synchronous.
97 /// <returns>Returns whether an item was processed. Returns <c>false</c> if the queue is empty.</returns>
101 bool dequeued = messageQueue.TryTake (out data);
108 /// Processes one item from the queue if the action is asynchronous.
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)
115 bool dequeued = messageQueue.TryTake (out data);
117 task = asyncAction (data);
123 public void Complete ()
125 messageBox.Complete ();
128 void IDataflowBlock.Fault (Exception exception)
130 compHelper.RequestFault (exception);
133 public Task Completion {
135 return compHelper.Completion;
139 public int InputCount {
141 return messageQueue.Count;
145 public override string ToString ()
147 return NameHelper.GetName (this, dataflowBlockOptions);