|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Concurrent; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.VisualStudio.Shell; |
| 10 | +using Microsoft.VisualStudio.Threading; |
| 11 | + |
| 12 | +namespace Microsoft.VisualStudio.LanguageServices.Implementation.LanguageService; |
| 13 | + |
| 14 | +using WorkTask = Func<IProgress<ServiceProgressData>, PackageRegistrationTasks, CancellationToken, Task>; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// Provides a mechanism for registering work to be done during package initialization. Work is registered |
| 18 | +/// as either main thread or background thread appropriate. This allows processing of these work items |
| 19 | +/// in a batched manner, reducing the number of thread switches required during the performance sensitive |
| 20 | +/// package loading timeframe. |
| 21 | +/// |
| 22 | +/// Note that currently the processing of these tasks isn't done concurrently. A future optimization may |
| 23 | +/// allow parallel background thread task execution, or even concurrent main and background thread work. |
| 24 | +/// </summary> |
| 25 | +internal sealed class PackageRegistrationTasks(JoinableTaskFactory jtf) |
| 26 | +{ |
| 27 | + private readonly ConcurrentQueue<WorkTask> _backgroundThreadWorkTasks = []; |
| 28 | + private readonly ConcurrentQueue<WorkTask> _mainThreadWorkTasks = []; |
| 29 | + private readonly JoinableTaskFactory _jtf = jtf; |
| 30 | + |
| 31 | + public void AddTask(bool isMainThreadTask, WorkTask task) |
| 32 | + { |
| 33 | + var workTasks = GetWorkTasks(isMainThreadTask); |
| 34 | + workTasks.Enqueue(task); |
| 35 | + } |
| 36 | + |
| 37 | + public async Task ProcessTasksAsync(IProgress<ServiceProgressData> progress, CancellationToken cancellationToken) |
| 38 | + { |
| 39 | + // prime the pump by doing the first group of bg thread work if the initiating thread is not the main thread |
| 40 | + if (!_jtf.Context.IsOnMainThread) |
| 41 | + await PerformWorkAsync(isMainThreadTask: false, progress, cancellationToken).ConfigureAwait(false); |
| 42 | + |
| 43 | + // Continue processing work until everything is completed, switching between main and bg threads as needed. |
| 44 | + while (!_mainThreadWorkTasks.IsEmpty || !_backgroundThreadWorkTasks.IsEmpty) |
| 45 | + { |
| 46 | + await PerformWorkAsync(isMainThreadTask: true, progress, cancellationToken).ConfigureAwait(false); |
| 47 | + await PerformWorkAsync(isMainThreadTask: false, progress, cancellationToken).ConfigureAwait(false); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private ConcurrentQueue<WorkTask> GetWorkTasks(bool isMainThreadTask) |
| 52 | + => isMainThreadTask ? _mainThreadWorkTasks : _backgroundThreadWorkTasks; |
| 53 | + |
| 54 | + private async Task PerformWorkAsync(bool isMainThreadTask, IProgress<ServiceProgressData> progress, CancellationToken cancellationToken) |
| 55 | + { |
| 56 | + var workTasks = GetWorkTasks(isMainThreadTask); |
| 57 | + if (workTasks.IsEmpty) |
| 58 | + return; |
| 59 | + |
| 60 | + // Ensure we're invoking the task on the right thread |
| 61 | + if (isMainThreadTask) |
| 62 | + await _jtf.SwitchToMainThreadAsync(cancellationToken); |
| 63 | + else if (_jtf.Context.IsOnMainThread) |
| 64 | + await TaskScheduler.Default; |
| 65 | + |
| 66 | + while (workTasks.TryDequeue(out var work)) |
| 67 | + { |
| 68 | + // CA(true) is important here, as we want to ensure that each iteration is done in the same |
| 69 | + // captured context. Thus, even poorly behaving tasks (ie, those that do their own thread switching) |
| 70 | + // don't effect the next loop iteration. |
| 71 | + await work(progress, this, cancellationToken).ConfigureAwait(true); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments