Skip to content

Commit

Permalink
Fix Task child waiting for RunSynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
garuma committed Apr 16, 2012
1 parent 3fe545a commit fa8a7b4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
7 changes: 5 additions & 2 deletions mcs/class/corlib/System.Threading.Tasks/Task.cs
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,13 @@ void HandleGenericException (AggregateException e)
ProcessCompleteDelegates ();
}

internal void WaitOnChildren ()
internal bool WaitOnChildren ()
{
if (Status == TaskStatus.WaitingForChildrenToComplete && childTasks != null)
if (Status == TaskStatus.WaitingForChildrenToComplete && childTasks != null) {
childTasks.Wait ();
return true;
}
return false;
}

public void Wait ()
Expand Down
4 changes: 3 additions & 1 deletion mcs/class/corlib/System.Threading.Tasks/TaskScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ internal protected bool TryExecuteTask (Task task)

if (task.Status == TaskStatus.WaitingToRun) {
task.Execute ();
task.WaitOnChildren ();
if (task.WaitOnChildren ())
task.Wait ();

return true;
}
Expand All @@ -139,6 +140,7 @@ internal bool RunInline (Task task)

if (!task.IsCompleted)
throw new InvalidOperationException ("The TryExecuteTaskInline call to the underlying scheduler succeeded, but the task body was not invoked");

return true;
}

Expand Down
20 changes: 19 additions & 1 deletion mcs/class/corlib/Test/System.Threading.Tasks/TaskTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void WaitAny_ManyCanceled ()
}

[Test]
public void WaitAllTest()
public void WaitAllTest ()
{
ParallelTestHelper.Repeat (delegate {
int achieved = 0;
Expand Down Expand Up @@ -902,6 +902,24 @@ public void WhenChildTaskSeveralLevelsDeepHandlesAggregateExceptionErrorStillBub
Assert.IsTrue (continuationRan);
}

[Test]
public void AlreadyCompletedChildTaskShouldRunContinuationImmediately ()
{
string result = "Failed";
var testTask = new Task (() =>
{
var child = new Task<string> (() =>
{
return "Success";
}, TaskCreationOptions.AttachedToParent);
child.RunSynchronously ();
child.ContinueWith (x => { Thread.Sleep (50); result = x.Result; }, TaskContinuationOptions.AttachedToParent | TaskContinuationOptions.NotOnFaulted);
});
testTask.RunSynchronously ();

Assert.AreEqual ("Success", result);
}

[Test]
public void InlineNotTrashingParentRelationship ()
{
Expand Down

0 comments on commit fa8a7b4

Please sign in to comment.