Merge pull request #2228 from lambdageek/dev/sgen-timeouts
[mono.git] / mcs / class / System.Threading.Tasks.Dataflow / System.Threading.Tasks.Dataflow / OutputAvailableBlock.cs
1 // OutputAvailableBlock.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 namespace System.Threading.Tasks.Dataflow {
25         /// <summary>
26         /// This internal block is used by the <see cref="DataflowBlock.OutputAvailableAsync"/> methods
27         /// to check for available items in an asynchronous way.
28         /// </summary>
29         class OutputAvailableBlock<TOutput> : ITargetBlock<TOutput> {
30                 readonly TaskCompletionSource<bool> completion =
31                         new TaskCompletionSource<bool> ();
32                 IDisposable linkBridge;
33                 CancellationTokenRegistration cancellationRegistration;
34
35                 public DataflowMessageStatus OfferMessage (
36                         DataflowMessageHeader messageHeader, TOutput messageValue,
37                         ISourceBlock<TOutput> source, bool consumeToAccept)
38                 {
39                         if (!messageHeader.IsValid)
40                                 return DataflowMessageStatus.Declined;
41
42                         if (completion.Task.Status != TaskStatus.WaitingForActivation)
43                                 return DataflowMessageStatus.DecliningPermanently;
44
45                         completion.TrySetResult (true);
46                         CompletionSet ();
47
48                         return DataflowMessageStatus.DecliningPermanently;
49                 }
50
51                 /// <summary>
52                 /// Returns a Task that can be used to wait until output from a block is available.
53                 /// </summary>
54                 /// <param name="bridge">The disposable object returned by <see cref="ISourceBlock{TOutput}.LinkTo"/>.</param>
55                 /// <param name="token">Cancellation token for this operation.</param>
56                 public Task<bool> AsyncGet (IDisposable bridge, CancellationToken token)
57                 {
58                         linkBridge = bridge;
59                         cancellationRegistration = token.Register (() =>
60                         {
61                                 completion.TrySetCanceled ();
62                                 CompletionSet ();
63                         });
64
65                         return completion.Task;
66                 }
67
68                 /// <summary>
69                 /// Called after the result has been set,
70                 /// cleans up after this block.
71                 /// </summary>
72                 void CompletionSet ()
73                 {
74                         if (linkBridge != null) {
75                                 linkBridge.Dispose ();
76                                 linkBridge = null;
77                         }
78
79                         cancellationRegistration.Dispose ();
80                 }
81
82                 public Task Completion {
83                         get { throw new NotSupportedException (); }
84                 }
85
86                 public void Complete ()
87                 {
88                         completion.TrySetResult (false);
89                         CompletionSet ();
90                 }
91
92                 public void Fault (Exception exception)
93                 {
94                         Complete ();
95                 }
96         }
97 }