Skip to content

Commit

Permalink
Merge pull request #73514 from CyrusNajmabadi/yieldPattern
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored May 16, 2024
2 parents f086d7c + d48e4f5 commit 799727e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ private async Task FindResultsAsync(
IFindUsagesContext findContext, Document document, int position, CancellationToken cancellationToken)
{
// Ensure that we relinquish the thread so that the caller can proceed with their work.
await Task.Yield().ConfigureAwait(false);
await TaskScheduler.Default.SwitchTo(alwaysYield: true);

using (Logger.LogBlock(FunctionId, KeyValueLogMessage.Create(LogType.UserAction), cancellationToken))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.VisualStudio.Threading;
using Nerdbank.Streams;
using Roslyn.Utilities;
using StreamJsonRpc;
Expand All @@ -11,7 +12,7 @@ namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests;
/// <summary>
/// A wrapper which takes a service but actually sends calls to it through JsonRpc to ensure we can actually use the service across a wire.
/// </summary>
internal sealed class BrokeredServiceProxy<T> : IAsyncDisposable where T : class
internal sealed class BrokeredServiceProxy<T> : System.IAsyncDisposable where T : class
{
/// <summary>
/// A task that cane awaited to assert the rest of the fields in this class being assigned and non-null.
Expand All @@ -31,8 +32,8 @@ public BrokeredServiceProxy(T service)

async Task CreateServerAsync()
{
// Ensure caller can proceed.
await Task.Yield().ConfigureAwait(false);
// Always yield to ensure caller can proceed.
await TaskScheduler.Default.SwitchTo(alwaysYield: true);

var serverMultiplexingStream = await MultiplexingStream.CreateAsync(serverStream);
var serverChannel = await serverMultiplexingStream.AcceptChannelAsync("");
Expand All @@ -46,8 +47,8 @@ async Task CreateServerAsync()

async Task CreateClientAsync()
{
// Ensure caller can proceed.
await Task.Yield().ConfigureAwait(false);
// Always yield to ensure caller can proceed.
await TaskScheduler.Default.SwitchTo(alwaysYield: true);

var clientMultiplexingStream = await MultiplexingStream.CreateAsync(clientStream);
var clientChannel = await clientMultiplexingStream.OfferChannelAsync("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public async Task<FirstFixResult> GetMostSevereFixAsync(
CancellationToken cancellationToken)
{
// Ensure we yield here so the caller can continue on.
await AwaitExtensions.ConfigureAwait(Task.Yield(), false);
await TaskScheduler.Default.SwitchTo(alwaysYield: true);

await foreach (var collection in StreamFixesAsync(
document, spanToDiagnostics, fixAllForInSpan: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ private async Task PerformSearchWorkerAsync(
ISearchCallback searchCallback,
CancellationToken cancellationToken)
{
// Ensure we yield immedaitely so our caller can proceed with other work.
await Task.Yield().ConfigureAwait(false);
// Ensure we yield immediately so our caller can proceed with other work.
await TaskScheduler.Default.SwitchTo(alwaysYield: true);

var searchValue = searchQuery.QueryString.Trim();
if (string.IsNullOrWhiteSpace(searchValue))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ static async Task WaitForHighPriorityTasksAsync(CancellationToken cancellationTo
if (task.IsCompleted)
{
// Make sure to yield so continuations of 'task' can make progress.
await AwaitExtensions.ConfigureAwait(Task.Yield(), false);
await TaskScheduler.Default.SwitchTo(alwaysYield: true);
}
else
{
Expand Down

0 comments on commit 799727e

Please sign in to comment.