Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions src/Microsoft.VisualStudio.Threading/JoinableTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -590,13 +590,34 @@ private Task QueueNeedProcessEvent
/// before the async operation has completed.
/// </param>
/// <returns>A task that completes after the asynchronous operation completes and the join is reverted.</returns>
public async Task JoinAsync(CancellationToken cancellationToken = default(CancellationToken))
public Task JoinAsync(CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();

using (this.AmbientJobJoinsThis())
if (!cancellationToken.CanBeCanceled)
{
await this.Task.WithCancellation(AwaitShouldCaptureSyncContext, cancellationToken).ConfigureAwait(AwaitShouldCaptureSyncContext);
// if a completed or failed JoinableTask will remove itself from parent dependency chains, so we don't repeat it which requires the sync lock.
_ = this.AmbientJobJoinsThis();
return this.Task;
}
else
{
return JoinSlowAsync(this, cancellationToken);
}

static async Task JoinSlowAsync(JoinableTask me, CancellationToken cancellationToken)
{
JoinRelease dependency = me.AmbientJobJoinsThis();

try
{
await me.Task.WithCancellation(continueOnCapturedContext: AwaitShouldCaptureSyncContext, cancellationToken).ConfigureAwait(AwaitShouldCaptureSyncContext);
}
catch
{
dependency.Dispose();
throw;
}
}
}

Expand Down Expand Up @@ -1055,6 +1076,20 @@ internal int GetPendingEventCountForSynchronousTask(JoinableTask synchronousTask
return this.queueNeedProcessEvent;
}

private protected JoinRelease AmbientJobJoinsThis()
{
if (!this.IsCompleted)
{
JoinableTask? ambientJob = this.JoinableTaskContext.AmbientTask;
if (ambientJob is object && ambientJob != this)
{
return JoinableTaskDependencyGraph.AddDependency(ambientJob, this);
}
}

return default(JoinRelease);
}

private static bool TryDequeueSelfOrDependencies(IJoinableTaskDependent currentNode, bool onMainThread, HashSet<IJoinableTaskDependent> visited, [NotNullWhen(true)] out SingleExecuteProtector? work)
{
Requires.NotNull(currentNode, nameof(currentNode));
Expand Down Expand Up @@ -1191,19 +1226,5 @@ private void AddStateFlags(JoinableTaskFlags flags)
}
}
}

private JoinRelease AmbientJobJoinsThis()
{
if (!this.IsCompleted)
{
JoinableTask? ambientJob = this.JoinableTaskContext.AmbientTask;
if (ambientJob is object && ambientJob != this)
{
return JoinableTaskDependencyGraph.AddDependency(ambientJob, this);
}
}

return default(JoinRelease);
}
}
}
25 changes: 21 additions & 4 deletions src/Microsoft.VisualStudio.Threading/JoinableTask`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,29 @@ internal JoinableTask(JoinableTaskFactory owner, bool synchronouslyBlocking, Joi
return this.Task;
}

return JoinSlowAsync(cancellationToken);
if (!cancellationToken.CanBeCanceled)
{
// if a completed or failed JoinableTask will remove itself from parent dependency chains, so we don't repeat it which requires the sync lock.
_ = this.AmbientJobJoinsThis();
return this.Task;
}

async Task<T> JoinSlowAsync(CancellationToken cancellationToken)
return JoinSlowAsync(this, cancellationToken);

static async Task<T> JoinSlowAsync(JoinableTask<T> me, CancellationToken cancellationToken)
{
await base.JoinAsync(cancellationToken).ConfigureAwait(AwaitShouldCaptureSyncContext);
return await this.Task.ConfigureAwait(AwaitShouldCaptureSyncContext);
JoinableTaskCollection.JoinRelease dependency = me.AmbientJobJoinsThis();

try
{
await me.Task.WithCancellation(continueOnCapturedContext: AwaitShouldCaptureSyncContext, cancellationToken).ConfigureAwait(AwaitShouldCaptureSyncContext);
return await me.Task.ConfigureAwait(AwaitShouldCaptureSyncContext);
}
catch
{
dependency.Dispose();
throw;
}
}
}

Expand Down