Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / corlib / System.Runtime.CompilerServices / TaskAwaiter.cs
index 855d48156614eb21c1580b942a50bccc8e0e6ee8..07110cf2b75406173394e05f1380e1533c9359a5 100644 (file)
 
 using System.Threading;
 using System.Threading.Tasks;
+using System.Runtime.ExceptionServices;
 
 namespace System.Runtime.CompilerServices
 {
-       public struct TaskAwaiter
+       public struct TaskAwaiter : ICriticalNotifyCompletion
        {
                readonly Task task;
 
@@ -51,28 +52,62 @@ namespace System.Runtime.CompilerServices
 
                public void GetResult ()
                {
+                       if (!task.IsCompleted)
+                               task.WaitCore (Timeout.Infinite, CancellationToken.None, true);
+
                        if (task.Status != TaskStatus.RanToCompletion)
-                               throw HandleUnexpectedTaskResult (task);
+                               // Merge current and dispatched stack traces if there is any
+                               ExceptionDispatchInfo.Capture (HandleUnexpectedTaskResult (task)).Throw ();
                }
 
                internal static Exception HandleUnexpectedTaskResult (Task task)
                {
+                       var slot = task.ExceptionSlot;
                        switch (task.Status) {
                        case TaskStatus.Canceled:
+                               // Use original exception when we have one
+                               if (slot.Exception != null)
+                                       goto case TaskStatus.Faulted;
+
                                return new TaskCanceledException (task);
                        case TaskStatus.Faulted:
-                               return task.Exception.InnerException;
+                               // Mark the exception as observed when GetResult throws
+                               slot.Observed = true;
+                               return slot.Exception.InnerException;
                        default:
-                               return new InvalidOperationException ("The task has not finished yet");
+                               throw new ArgumentException (string.Format ("Unexpected task `{0}' status `{1}'", task.Id, task.Status));
                        }
                }
 
-               internal static void HandleOnCompleted (Task task, Action continuation, bool continueOnSourceContext)
+               internal static void HandleOnCompleted (Task task, Action continuation, bool continueOnSourceContext, bool manageContext)
                {
-                       if (continueOnSourceContext && SynchronizationContext.Current != null) {
+                       if (continueOnSourceContext && SynchronizationContext.Current != null && SynchronizationContext.Current.GetType () != typeof (SynchronizationContext)) {
                                task.ContinueWith (new SynchronizationContextContinuation (continuation, SynchronizationContext.Current));
                        } else {
-                               task.ContinueWith (new ActionContinuation (continuation));
+                               IContinuation cont;
+                               Task cont_task;
+                               if (continueOnSourceContext && !TaskScheduler.IsDefault) {
+                                       cont_task = new Task (TaskActionInvoker.Create (continuation), null, CancellationToken.None, TaskCreationOptions.None, null);
+                                       cont_task.SetupScheduler (TaskScheduler.Current);
+                                       cont = new SchedulerAwaitContinuation (cont_task);
+                               } else {
+                                       cont_task = null;
+                                       cont = new AwaiterActionContinuation (continuation);
+                               }
+
+                               //
+                               // This is awaiter continuation. For finished tasks we get false result and need to
+                               // queue the continuation otherwise the task would block
+                               //
+                               if (task.ContinueWith (cont, false))
+                                       return;
+
+                               if (cont_task == null) {
+                                       cont_task = new Task (TaskActionInvoker.Create (continuation), null, CancellationToken.None, TaskCreationOptions.None, null);
+                                       cont_task.SetupScheduler (TaskScheduler.Current);
+                               }
+
+                               cont_task.Schedule (true);
                        }
                }
 
@@ -81,7 +116,15 @@ namespace System.Runtime.CompilerServices
                        if (continuation == null)
                                throw new ArgumentNullException ("continuation");
 
-                       HandleOnCompleted (task, continuation, true);
+                       HandleOnCompleted (task, continuation, true, true);
+               }
+               
+               public void UnsafeOnCompleted (Action continuation)
+               {
+                       if (continuation == null)
+                               throw new ArgumentNullException ("continuation");
+
+                       HandleOnCompleted (task, continuation, true, false);
                }
        }
 }