Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions src/Microsoft.VisualStudio.Threading/JoinableTaskContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ public bool IsWithinJoinableTask
get { return this.AmbientTask is object; }
}

/// <summary>
/// Gets a value indicating whether gets a value indicating the JoinableTask is not associated with any main thread.
/// This allows library code to skip some additional work in the environments that do not have a main thread.
/// </summary>
Comment thread
lifengl marked this conversation as resolved.
public bool IsNoOpContext => this.UnderlyingSynchronizationContext is null;

/// <summary>
/// Gets a value indicating whether the main thread is blocked by any joinable task.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Microsoft.VisualStudio.Threading.AsyncBarrier.SignalAndWait(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState.IsCompleted.get -> bool
Microsoft.VisualStudio.Threading.JoinableTaskContext.IsNoOpContext.get -> bool
static Microsoft.VisualStudio.Threading.JoinableTaskContext.CreateNoOpContext() -> Microsoft.VisualStudio.Threading.JoinableTaskContext!
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Microsoft.VisualStudio.Threading.AsyncBarrier.SignalAndWait(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState.IsCompleted.get -> bool
Microsoft.VisualStudio.Threading.JoinableTaskContext.IsNoOpContext.get -> bool
static Microsoft.VisualStudio.Threading.JoinableTaskContext.CreateNoOpContext() -> Microsoft.VisualStudio.Threading.JoinableTaskContext!
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Microsoft.VisualStudio.Threading.AsyncBarrier.SignalAndWait(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState.IsCompleted.get -> bool
Microsoft.VisualStudio.Threading.JoinableTaskContext.IsNoOpContext.get -> bool
static Microsoft.VisualStudio.Threading.JoinableTaskContext.CreateNoOpContext() -> Microsoft.VisualStudio.Threading.JoinableTaskContext!
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Microsoft.VisualStudio.Threading.AsyncBarrier.SignalAndWait(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState.IsCompleted.get -> bool
Microsoft.VisualStudio.Threading.JoinableTaskContext.IsNoOpContext.get -> bool
static Microsoft.VisualStudio.Threading.JoinableTaskContext.CreateNoOpContext() -> Microsoft.VisualStudio.Threading.JoinableTaskContext!
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ public void Ctor_ExplicitNullSyncContext()
Thread mainThread = Thread.CurrentThread;
Assumes.NotNull(SynchronizationContext.Current);
JoinableTaskContext jtc = JoinableTaskContext.CreateNoOpContext();
Assert.True(jtc.IsNoOpContext);
await TaskScheduler.Default.SwitchTo(alwaysYield: true); // Get off the main thread.
Assert.NotSame(mainThread, Thread.CurrentThread);

Expand All @@ -745,6 +746,7 @@ public void Ctor_NullSyncContextArg_AmbientSyncContext()
Thread mainThread = Thread.CurrentThread;
Assumes.NotNull(SynchronizationContext.Current);
JoinableTaskContext jtc = new(null, null);
Assert.False(jtc.IsNoOpContext);
await TaskScheduler.Default.SwitchTo(alwaysYield: true); // Get off the main thread.
Assert.NotSame(mainThread, Thread.CurrentThread);

Expand All @@ -762,6 +764,7 @@ public void Ctor_Default()
Thread mainThread = Thread.CurrentThread;
Assumes.NotNull(SynchronizationContext.Current);
JoinableTaskContext jtc = new();
Assert.False(jtc.IsNoOpContext);
await TaskScheduler.Default.SwitchTo(alwaysYield: true); // Get off the main thread.
Assert.NotSame(mainThread, Thread.CurrentThread);

Expand All @@ -771,6 +774,28 @@ public void Ctor_Default()
});
}

[Fact]
public void Ctor_DefaultWithNoSyncContext()
{
this.SimulateUIThread(async delegate
{
await TaskScheduler.Default.SwitchTo(alwaysYield: true); // Get off the main thread.

Thread currentThread = Thread.CurrentThread;

Assumes.Null(SynchronizationContext.Current);
JoinableTaskContext jtc = new();
Assert.True(jtc.IsNoOpContext);

await TaskScheduler.Default.SwitchTo();
Assert.Same(currentThread, Thread.CurrentThread);

// Verify that switching to the main thread works.
await jtc.Factory.SwitchToMainThreadAsync(this.TimeoutToken);
Assert.Same(currentThread, Thread.CurrentThread);
});
}

protected override JoinableTaskContext CreateJoinableTaskContext()
{
return new JoinableTaskContextDerived();
Expand Down