Update Wait and WaitAny with event disposal and single execution flow
[mono.git] / mcs / class / corlib / System.Threading.Tasks / TaskCompletionSource.cs
1 // 
2 // TaskCompletionSource.cs
3 //  
4 // Authors:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 //       Marek Safar <marek.safar@gmail.com>
7 // 
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
10 // 
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
28
29 #if NET_4_0 || MOBILE
30 using System;
31 using System.Collections.Generic;
32
33 namespace System.Threading.Tasks
34 {
35         public class TaskCompletionSource<TResult>
36         {
37                 static readonly Func<TResult> emptyFunction = () => default (TResult);
38                 static readonly Func<object, TResult> emptyParamFunction = (_) => default (TResult);
39
40                 static readonly Action<Task<TResult>, TResult> setResultAction = SetResultAction;
41                 static readonly Action<Task<TResult>, AggregateException> setExceptionAction = SetExceptionAction;
42                 static readonly Action<Task<TResult>, object> setCanceledAction = SetCanceledAction;
43
44                 readonly Task<TResult> source;
45                 SpinLock opLock = new SpinLock (false);
46
47                 public TaskCompletionSource ()
48                 {
49                         source = new Task<TResult> (emptyFunction);
50                         source.SetupScheduler (TaskScheduler.Current);
51                 }
52                 
53                 public TaskCompletionSource (object state)
54                 {
55                         source = new Task<TResult> (emptyParamFunction, state);
56                         source.SetupScheduler (TaskScheduler.Current);
57                 }
58                 
59                 public TaskCompletionSource (TaskCreationOptions creationOptions)
60                         : this (null, creationOptions)
61                 {
62                 }
63                 
64                 public TaskCompletionSource (object state, TaskCreationOptions creationOptions)
65                 {
66                         if ((creationOptions & System.Threading.Tasks.Task.WorkerTaskNotSupportedOptions) != 0)
67                                 throw new ArgumentOutOfRangeException ("creationOptions");
68
69                         source = new Task<TResult> (emptyParamFunction, state, creationOptions);
70                         source.SetupScheduler (TaskScheduler.Current);
71                 }
72                 
73                 public void SetCanceled ()
74                 {
75                         if (!TrySetCanceled ())
76                                 ThrowInvalidException ();
77                 }
78                 
79                 public void SetException (Exception exception)
80                 {
81                         if (exception == null)
82                                 throw new ArgumentNullException ("exception");
83                         
84                         SetException (new Exception[] { exception });
85                 }
86                 
87                 public void SetException (IEnumerable<Exception> exceptions)
88                 {
89                         if (!TrySetException (exceptions))
90                                 ThrowInvalidException ();
91                 }
92                 
93                 public void SetResult (TResult result)
94                 {
95                         if (!TrySetResult (result))
96                                 ThrowInvalidException ();
97                 }
98                                 
99                 static void ThrowInvalidException ()
100                 {
101                         throw new InvalidOperationException ("The underlying Task is already in one of the three final states: RanToCompletion, Faulted, or Canceled.");
102                 }
103                 
104                 public bool TrySetCanceled ()
105                 {
106                         return ApplyOperation (setCanceledAction, null);
107                 }
108                 
109                 public bool TrySetException (Exception exception)
110                 {
111                         if (exception == null)
112                                 throw new ArgumentNullException ("exception");
113                         
114                         return TrySetException (new Exception[] { exception });
115                 }
116                 
117                 public bool TrySetException (IEnumerable<Exception> exceptions)
118                 {
119                         if (exceptions == null)
120                                 throw new ArgumentNullException ("exceptions");
121
122                         var aggregate = new AggregateException (exceptions);
123                         if (aggregate.InnerExceptions.Count == 0)
124                                 throw new ArgumentNullException ("exceptions");
125                         
126                         return ApplyOperation (setExceptionAction, aggregate);
127                 }
128                 
129                 public bool TrySetResult (TResult result)
130                 {
131                         return ApplyOperation (setResultAction, result);
132                 }
133                                 
134                 bool ApplyOperation<TState> (Action<Task<TResult>, TState> action, TState state)
135                 {
136                         bool taken = false;
137                         try {
138                                 opLock.Enter (ref taken);
139                                 if (CheckInvalidState ())
140                                         return false;
141                         
142                                 source.Status = TaskStatus.Running;
143
144                                 if (action != null)
145                                         action (source, state);
146
147                                 source.Finish ();
148                         
149                                 return true;
150                         } finally {
151                                 if (taken)
152                                         opLock.Exit ();
153                         }
154                 }
155                 
156                 bool CheckInvalidState ()
157                 {
158                         return source.Status == TaskStatus.RanToCompletion ||
159                                    source.Status == TaskStatus.Faulted || 
160                                    source.Status == TaskStatus.Canceled;
161                 }
162
163                 static void SetResultAction (Task<TResult> source, TResult result)
164                 {
165                         source.Result = result;
166                 }
167
168                 static void SetExceptionAction (Task<TResult> source, AggregateException aggregate)
169                 {
170                         source.HandleGenericException (aggregate);
171                 }
172
173                 static void SetCanceledAction (Task<TResult> source, object unused)
174                 {
175                         source.CancelReal ();
176                 }
177
178                 public Task<TResult> Task {
179                         get {
180                                 return source;
181                         }
182                 }
183         }
184 }
185 #endif