Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 3 additions & 4 deletions src/Microsoft.VisualStudio.Threading/JoinableTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using JoinRelease = Microsoft.VisualStudio.Threading.JoinableTaskCollection.JoinRelease;
Expand Down Expand Up @@ -147,7 +146,7 @@ internal JoinableTask(JoinableTaskFactory owner, bool synchronouslyBlocking, str
this.state |= JoinableTaskFlags.StartedSynchronously | JoinableTaskFlags.CompletingSynchronously;
}

if (owner.Context.IsOnMainThread)
if (owner.Context.IsOnMainThread && !this.JoinableTaskContext.IsNoOpContext)
{
this.state |= JoinableTaskFlags.StartedOnMainThread;
if (synchronouslyBlocking)
Expand Down Expand Up @@ -321,7 +320,7 @@ internal SynchronizationContext? ApplicableJobSyncContext
{
get
{
if (this.JoinableTaskContext.IsOnMainThread)
if (this.JoinableTaskContext.IsOnMainThread && !this.JoinableTaskContext.IsNoOpContext)
{
if (this.mainThreadJobSyncContext is null)
{
Expand Down Expand Up @@ -977,7 +976,7 @@ internal void CompleteOnCurrentThread()
{
bool onMainThread = false;
JoinableTaskFlags additionalFlags = JoinableTaskFlags.CompletingSynchronously;
if (this.JoinableTaskContext.IsOnMainThread)
if (this.JoinableTaskContext.IsOnMainThread && !this.JoinableTaskContext.IsNoOpContext)
{
additionalFlags |= JoinableTaskFlags.SynchronouslyBlockingMainThread;
onMainThread = true;
Expand Down
27 changes: 15 additions & 12 deletions src/Microsoft.VisualStudio.Threading/JoinableTaskFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public partial class JoinableTaskFactory
/// </summary>
private readonly JoinableTaskContext owner;

private readonly SynchronizationContext mainThreadJobSyncContext;
private readonly SynchronizationContext? mainThreadJobSyncContext;

/// <summary>
/// The collection to add all created tasks to. May be <see langword="null" />.
Expand Down Expand Up @@ -71,7 +71,7 @@ internal JoinableTaskFactory(JoinableTaskContext owner, JoinableTaskCollection?

this.owner = owner;
this.jobCollection = collection;
this.mainThreadJobSyncContext = new JoinableTaskSynchronizationContext(this);
this.mainThreadJobSyncContext = owner.IsNoOpContext ? null : new JoinableTaskSynchronizationContext(this);
}

/// <summary>
Expand Down Expand Up @@ -1017,20 +1017,23 @@ internal RunFramework(JoinableTaskFactory factory, JoinableTask joinable)
{
JoinableTaskDependencyGraph.AddDependency(this.previousJoinable, joinable);

// By definition we inherit the nesting factories of our immediate nesting task.
ListOfOftenOne<JoinableTaskFactory> nestingFactories = this.previousJoinable.NestingFactories;

// And we may add our immediate nesting parent's factory to the list of
// ancestors if it isn't already in the list.
if (this.previousJoinable.Factory != this.factory)
if (!factory.Context.IsNoOpContext)
{
if (!nestingFactories.Contains(this.previousJoinable.Factory))
// By definition we inherit the nesting factories of our immediate nesting task.
ListOfOftenOne<JoinableTaskFactory> nestingFactories = this.previousJoinable.NestingFactories;

// And we may add our immediate nesting parent's factory to the list of
// ancestors if it isn't already in the list.
if (this.previousJoinable.Factory != this.factory)
{
nestingFactories.Add(this.previousJoinable.Factory);
if (!nestingFactories.Contains(this.previousJoinable.Factory))
{
nestingFactories.Add(this.previousJoinable.Factory);
}
}
}

this.joinable.NestingFactories = nestingFactories;
this.joinable.NestingFactories = nestingFactories;
}
}

if (joinable.GetTokenizedParent() is JoinableTask tokenizedParent)
Expand Down
Loading