5 // Jérémie Laval <jeremie dot laval at xamarin dot com>
6 // Marek Safar <marek.safar@gmail.com>
8 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 using System.Collections.Generic;
34 namespace System.Threading.Tasks
36 interface IContinuation
41 class TaskContinuation : IContinuation
44 readonly TaskContinuationOptions continuationOptions;
46 public TaskContinuation (Task task, TaskContinuationOptions continuationOptions)
49 this.continuationOptions = continuationOptions;
52 bool ContinuationStatusCheck (TaskContinuationOptions kind)
54 if (kind == TaskContinuationOptions.None)
57 int kindCode = (int) kind;
58 var status = task.ContinuationAncestor.Status;
60 if (kindCode >= ((int) TaskContinuationOptions.NotOnRanToCompletion)) {
61 // Remove other options
62 kind &= ~(TaskContinuationOptions.PreferFairness
63 | TaskContinuationOptions.LongRunning
64 | TaskContinuationOptions.AttachedToParent
65 | TaskContinuationOptions.ExecuteSynchronously);
67 if (status == TaskStatus.Canceled) {
68 if (kind == TaskContinuationOptions.NotOnCanceled)
70 if (kind == TaskContinuationOptions.OnlyOnFaulted)
72 if (kind == TaskContinuationOptions.OnlyOnRanToCompletion)
74 } else if (status == TaskStatus.Faulted) {
75 if (kind == TaskContinuationOptions.NotOnFaulted)
77 if (kind == TaskContinuationOptions.OnlyOnCanceled)
79 if (kind == TaskContinuationOptions.OnlyOnRanToCompletion)
81 } else if (status == TaskStatus.RanToCompletion) {
82 if (kind == TaskContinuationOptions.NotOnRanToCompletion)
84 if (kind == TaskContinuationOptions.OnlyOnFaulted)
86 if (kind == TaskContinuationOptions.OnlyOnCanceled)
94 public void Execute ()
96 if (!ContinuationStatusCheck (continuationOptions)) {
102 // The task may have been canceled externally
103 if (task.IsCompleted)
106 if ((continuationOptions & TaskContinuationOptions.ExecuteSynchronously) != 0)
107 task.RunSynchronouslyCore (task.scheduler);
113 class ActionContinuation : IContinuation
115 readonly Action action;
117 public ActionContinuation (Action action)
119 this.action = action;
122 public void Execute ()
128 class SchedulerAwaitContinuation : IContinuation
132 public SchedulerAwaitContinuation (Task task)
137 public void Execute ()
139 task.RunSynchronouslyCore (task.scheduler);
143 class SynchronizationContextContinuation : IContinuation
145 readonly Action action;
146 readonly SynchronizationContext ctx;
148 public SynchronizationContextContinuation (Action action, SynchronizationContext ctx)
150 this.action = action;
154 public void Execute ()
156 ctx.Post (l => ((Action) l) (), action);
160 sealed class WhenAllContinuation : IContinuation
163 readonly IList<Task> tasks;
166 public WhenAllContinuation (Task owner, IList<Task> tasks)
169 this.counter = tasks.Count;
173 public void Execute ()
175 if (Interlocked.Decrement (ref counter) != 0)
178 owner.Status = TaskStatus.Running;
180 bool canceled = false;
181 List<Exception> exceptions = null;
182 foreach (var task in tasks) {
183 if (task.IsFaulted) {
184 if (exceptions == null)
185 exceptions = new List<Exception> ();
187 exceptions.AddRange (task.Exception.InnerExceptions);
191 if (task.IsCanceled) {
196 if (exceptions != null) {
197 owner.TrySetException (new AggregateException (exceptions));
210 sealed class WhenAllContinuation<TResult> : IContinuation
212 readonly Task<TResult[]> owner;
213 readonly IList<Task<TResult>> tasks;
216 public WhenAllContinuation (Task<TResult[]> owner, IList<Task<TResult>> tasks)
219 this.counter = tasks.Count;
223 public void Execute ()
225 if (Interlocked.Decrement (ref counter) != 0)
228 bool canceled = false;
229 List<Exception> exceptions = null;
230 TResult[] results = null;
231 for (int i = 0; i < tasks.Count; ++i) {
232 var task = tasks [i];
233 if (task.IsFaulted) {
234 if (exceptions == null)
235 exceptions = new List<Exception> ();
237 exceptions.AddRange (task.Exception.InnerExceptions);
241 if (task.IsCanceled) {
246 if (results == null) {
247 if (canceled || exceptions != null)
250 results = new TResult[tasks.Count];
253 results[i] = task.Result;
256 if (exceptions != null) {
257 owner.TrySetException (new AggregateException (exceptions));
266 owner.TrySetResult (results);
270 sealed class WhenAnyContinuation<T> : IContinuation where T : Task
272 readonly Task<T> owner;
273 readonly IList<T> tasks;
274 AtomicBooleanValue executed;
276 public WhenAnyContinuation (Task<T> owner, IList<T> tasks)
280 executed = new AtomicBooleanValue ();
283 public void Execute ()
285 if (!executed.TryRelaxedSet ())
288 bool owner_notified = false;
289 for (int i = 0; i < tasks.Count; ++i) {
291 if (!task.IsCompleted) {
292 task.RemoveContinuation (this);
299 owner.TrySetResult (task);
300 owner_notified = true;
305 sealed class ManualResetContinuation : IContinuation, IDisposable
307 readonly ManualResetEventSlim evt;
309 public ManualResetContinuation ()
311 this.evt = new ManualResetEventSlim ();
314 public ManualResetEventSlim Event {
320 public void Dispose ()
325 public void Execute ()
331 sealed class CountdownContinuation : IContinuation, IDisposable
333 readonly CountdownEvent evt;
335 public CountdownContinuation (int initialCount)
337 this.evt = new CountdownEvent (initialCount);
340 public CountdownEvent Event {
346 public void Dispose ()
351 public void Execute ()
357 sealed class DisposeContinuation : IContinuation
359 readonly IDisposable instance;
361 public DisposeContinuation (IDisposable instance)
363 this.instance = instance;
366 public void Execute ()