Skip to content

Commit

Permalink
Wait for background tasks queued during build loading
Browse files Browse the repository at this point in the history
Fixes #724
  • Loading branch information
KirillOsenkov committed Nov 27, 2023
1 parent c90264e commit e0d109c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/StructuredLogViewer.Core/PreprocessedFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void BuildImportMap()

string evaluationKey = GetEvaluationKey(projectEvaluation);
var importMap = GetOrCreateImportMap(evaluationKey, projectEvaluation.ProjectFile);
System.Threading.Tasks.Task.Run(() =>
build.RunInBackground(() =>
{
imports.VisitAllChildren<Import>(import => VisitImport(import, importMap));
});
Expand Down
2 changes: 2 additions & 0 deletions src/StructuredLogger/BinaryLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public static Build ReadBuild(Stream stream, Progress progress, byte[] projectIm

// Serialization.WriteStringsToFile(@"C:\temp\1.txt", strings.ToArray());

build.WaitForBackgroundTasks();

return build;
}
}
Expand Down
20 changes: 2 additions & 18 deletions src/StructuredLogger/Construction/Construction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public class Construction
private readonly MessageProcessor messageProcessor;
private readonly StringCache stringTable;

internal bool PopulatePropertiesAndItemsInBackground = PlatformUtilities.HasThreads;

public StringCache StringTable => stringTable;

public NamedNode EvaluationFolder => Build.EvaluationFolder;
Expand Down Expand Up @@ -566,14 +564,7 @@ public void StatusEventRaised(object sender, BuildStatusEventArgs e)
propertiesFolder.DisableChildrenCache = true;
}

if (PopulatePropertiesAndItemsInBackground)
{
System.Threading.Tasks.Task.Run(() => AddGlobalProperties());
}
else
{
AddGlobalProperties();
}
Build.RunInBackground(() => AddGlobalProperties());

void AddGlobalProperties()
{
Expand Down Expand Up @@ -988,14 +979,7 @@ public void UpdateProject(Project project, ProjectStartedEventArgs args)
propertyFolder.DisableChildrenCache = true;
}

if (PopulatePropertiesAndItemsInBackground)
{
System.Threading.Tasks.Task.Run(() => AddGlobalProperties());
}
else
{
AddGlobalProperties();
}
Build.RunInBackground(() => AddGlobalProperties());

void AddGlobalProperties()
{
Expand Down
9 changes: 1 addition & 8 deletions src/StructuredLogger/Construction/MessageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,7 @@ private BaseNode CreateParameterNode(string itemName, IEnumerable items, bool is

private void AddItems(IEnumerable items, TreeNode parent)
{
if (construction.PopulatePropertiesAndItemsInBackground)
{
System.Threading.Tasks.Task.Run(() => AddItemsCore(items, parent));
}
else
{
AddItemsCore(items, parent);
}
construction.Build.RunInBackground(() => AddItemsCore(items, parent));
}

private void AddItemsCore(IEnumerable items, TreeNode parent)
Expand Down
35 changes: 33 additions & 2 deletions src/StructuredLogger/ObjectModel/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading;
using TPLTask = System.Threading.Tasks.Task;

namespace Microsoft.Build.Logging.StructuredLogger
{
Expand All @@ -23,6 +23,8 @@ public class Build : TimedNode
public int FileFormatVersion { get; set; }
public byte[] SourceFilesArchive { get; set; }

private readonly List<TPLTask> backgroundTasks = new List<TPLTask>();

private IReadOnlyList<ArchiveFile> sourceFiles;
public IReadOnlyList<ArchiveFile> SourceFiles
{
Expand All @@ -39,7 +41,7 @@ public IReadOnlyList<ArchiveFile> SourceFiles
}

private string msbuildVersion;
public string MSBuildVersion
public string MSBuildVersion
{
get => msbuildVersion;
set
Expand Down Expand Up @@ -255,5 +257,34 @@ public ProjectEvaluation FindEvaluation(int id)

return projectEvaluation;
}

public void RunInBackground(Action action)
{
if (PlatformUtilities.HasThreads)
{
var task = TPLTask.Run(action);
lock (backgroundTasks)
{
backgroundTasks.Add(task);
}
}
else
{
action();
}
}

public void WaitForBackgroundTasks()
{
lock (backgroundTasks)
{
foreach (var task in backgroundTasks)
{
task.Wait();
}

backgroundTasks.Clear();
}
}
}
}

0 comments on commit e0d109c

Please sign in to comment.