Replace SIZEOF_REGISTER with sizeof(mgreg_t) for consistency with sizeof(gpointer)
[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 || BOOTSTRAP_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                         source.SetupScheduler (TaskScheduler.Current);
41                 }
42                 
43                 public TaskCompletionSource (object state)
44                 {
45                         source = new Task<TResult> (null, state);
46                         source.SetupScheduler (TaskScheduler.Current);
47                 }
48                 
49                 public TaskCompletionSource (TaskCreationOptions options)
50                 {
51                         source = new Task<TResult> (null, options);
52                         source.SetupScheduler (TaskScheduler.Current);
53                 }
54                 
55                 public TaskCompletionSource (object state, TaskCreationOptions options)
56                 {
57                         source = new Task<TResult> (null, state, options);
58                         source.SetupScheduler (TaskScheduler.Current);
59                 }
60                 
61                 public void SetCanceled ()
62                 {
63                         if (!ApplyOperation (source.CancelReal))
64                                 ThrowInvalidException ();
65                 }
66                 
67                 public void SetException (Exception e)
68                 {
69                         SetException (new Exception[] { e });
70                 }
71                 
72                 public void SetException (IEnumerable<Exception> e)
73                 {
74                         if (!ApplyOperation (() => source.HandleGenericException (new AggregateException (e))))
75                                 ThrowInvalidException ();
76                 }
77                 
78                 public void SetResult (TResult result)
79                 {
80                         if (!ApplyOperation (() => source.Result = result))
81                                 ThrowInvalidException ();
82                 }
83                                 
84                 void ThrowInvalidException ()
85                 {
86                         throw new InvalidOperationException ("The underlying Task is already in one of the three final states: RanToCompletion, Faulted, or Canceled.");
87                 }
88                 
89                 public bool TrySetCanceled ()
90                 {
91                         return ApplyOperation (source.CancelReal);
92                 }
93                 
94                 public bool TrySetException (Exception e)
95                 {
96                         return TrySetException (new Exception[] { e });
97                 }
98                 
99                 public bool TrySetException (IEnumerable<Exception> e)
100                 {
101                         return ApplyOperation (() => source.HandleGenericException (new AggregateException (e)));
102                 }
103                 
104                 public bool TrySetResult (TResult result)
105                 {
106                         return ApplyOperation (() => source.Result = result);
107                 }
108                                 
109                 bool ApplyOperation (Action action)
110                 {
111                         if (CheckInvalidState ())
112                                 return false;
113                         
114                         source.Status = TaskStatus.Running;
115
116                         if (action != null)
117                                 action ();
118
119                         source.Finish ();
120                         
121                         return true;
122                 }
123                 
124                 bool CheckInvalidState ()
125                 {
126                         return source.Status == TaskStatus.RanToCompletion ||
127                                    source.Status == TaskStatus.Faulted || 
128                                    source.Status == TaskStatus.Canceled;
129                                         
130                 }
131                 
132                 public Task<TResult> Task {
133                         get {
134                                 return source;
135                         }
136                 }
137         }
138 }
139 #endif