Facilitate the merge
[mono.git] / mcs / class / corlib / System.Threading.Tasks / TaskCompletionSource.cs
1 // 
2 // TaskCompletionSource.cs
3 //  
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // 
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26
27 #if NET_4_0
28 using System;
29 using System.Collections.Generic;
30
31 namespace System.Threading.Tasks
32 {
33         public class TaskCompletionSource<TResult>
34         {
35                 Task<TResult> source;
36
37                 public TaskCompletionSource ()
38                 {
39                         source = new Task<TResult> (null);
40                 }
41                 
42                 public TaskCompletionSource (object state)
43                 {
44                         source = new Task<TResult> (null, state);
45                 }
46                 
47                 public TaskCompletionSource (TaskCreationOptions options)
48                 {
49                         source = new Task<TResult> (null, options);
50                 }
51                 
52                 public TaskCompletionSource (object state, TaskCreationOptions options)
53                 {
54                         source = new Task<TResult> (null, state, options);
55                 }
56                 
57                 public void SetCanceled ()
58                 {
59                         if (!ApplyOperation (TaskStatus.Canceled, source.CancelReal))
60                                 ThrowInvalidException ();
61                 }
62                 
63                 public void SetException (Exception e)
64                 {
65                         SetException (new Exception[] { e });
66                 }
67                 
68                 public void SetException (IEnumerable<Exception> e)
69                 {
70                         if (!ApplyOperation (TaskStatus.Faulted, () => source.Exception = new AggregateException (e)))
71                                 ThrowInvalidException ();
72                 }
73                 
74                 public void SetResult (TResult result)
75                 {
76                         if (!ApplyOperation (TaskStatus.RanToCompletion, () => source.Result = result))
77                                 ThrowInvalidException ();
78                 }
79                                 
80                 void ThrowInvalidException ()
81                 {
82                         throw new InvalidOperationException ("The underlying Task is already in one of the three final states: RanToCompletion, Faulted, or Canceled.");
83                 }
84                 
85                 public bool TrySetCanceled ()
86                 {
87                         return ApplyOperation (TaskStatus.Canceled, source.CancelReal);
88                 }
89                 
90                 public bool TrySetException (Exception e)
91                 {
92                         return TrySetException (new Exception[] { e });
93                 }
94                 
95                 
96                 public bool TrySetException (IEnumerable<Exception> e)
97                 {
98                         return ApplyOperation (TaskStatus.Faulted, () => source.Exception = new AggregateException (e));
99                 }
100                 
101                 public bool TrySetResult (TResult result)
102                 {
103                         return ApplyOperation (TaskStatus.RanToCompletion, () => source.Result = result);
104                 }
105                                 
106                 bool ApplyOperation (TaskStatus newStatus, Action action)
107                 {
108                         if (CheckInvalidState ())
109                                 return false;
110                         
111                         if (action != null)
112                                 action ();
113                         source.Status = newStatus;
114                         
115                         return true;
116                 }
117                 
118                 bool CheckInvalidState ()
119                 {
120                         return source.Status == TaskStatus.RanToCompletion ||
121                                    source.Status == TaskStatus.Faulted || 
122                                    source.Status == TaskStatus.Canceled;
123                                         
124                 }
125                 
126                 public Task<TResult> Task {
127                         get {
128                                 return source;
129                         }
130                 }
131         }
132 }
133 #endif