Skip to content

Commit

Permalink
Thread-safe load of sub-projects from master project
Browse files Browse the repository at this point in the history
  • Loading branch information
tintoy committed Mar 24, 2024
1 parent 1cddfe3 commit 0d40200
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
26 changes: 15 additions & 11 deletions src/LanguageServer.Engine/Documents/MasterProjectDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace MSBuildProjectTools.LanguageServer.Documents
{
using SemanticModel;
using System.Collections.Concurrent;
using Utilities;

/// <summary>
Expand All @@ -22,7 +23,7 @@ public class MasterProjectDocument
/// <summary>
/// Sub-projects (if any).
/// </summary>
readonly Dictionary<Uri, SubProjectDocument> _subProjects = new Dictionary<Uri, SubProjectDocument>();
readonly ConcurrentDictionary<Uri, SubProjectDocument> _subProjects = new ConcurrentDictionary<Uri, SubProjectDocument>();

/// <summary>
/// Create a new <see cref="MasterProjectDocument"/>.
Expand Down Expand Up @@ -67,15 +68,21 @@ protected override void Dispose(bool disposing)
/// <summary>
/// Add a sub-project.
/// </summary>
/// <param name="subProjectDocument">
/// <param name="documentUri">
/// The sub-project.
/// </param>
public void AddSubProject(SubProjectDocument subProjectDocument)
/// <param name="createSubProjectDocument">
/// A factory delegate to create the <see cref="SubProjectDocument"/> if it does not already exist.
/// </param>
public SubProjectDocument GetOrAddSubProject(Uri documentUri, Func<SubProjectDocument> createSubProjectDocument)
{
if (subProjectDocument == null)
throw new ArgumentNullException(nameof(subProjectDocument));
if (documentUri == null)
throw new ArgumentNullException(nameof(documentUri));

if (createSubProjectDocument == null)
throw new ArgumentNullException(nameof(createSubProjectDocument));

_subProjects.Add(subProjectDocument.DocumentUri, subProjectDocument);
return _subProjects.GetOrAdd(documentUri, _ => createSubProjectDocument());
}

/// <summary>
Expand All @@ -89,11 +96,8 @@ public void RemoveSubProject(Uri documentUri)
if (documentUri == null)
throw new ArgumentNullException(nameof(documentUri));

if (!_subProjects.TryGetValue(documentUri, out SubProjectDocument subProjectDocument))
return;

subProjectDocument.Unload();
_subProjects.Remove(documentUri);
if (_subProjects.TryRemove(documentUri, out SubProjectDocument subProjectDocument))
subProjectDocument.Unload();
}

/// <summary>
Expand Down
10 changes: 4 additions & 6 deletions src/LanguageServer.Engine/Documents/Workspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,11 @@ public async Task<ProjectDocument> GetProjectDocument(Uri documentUri, bool relo
isNewProject = true;

if (MasterProject == null)
{
MasterProject = new MasterProjectDocument(this, documentUri, Log);
return MasterProject;
}
return MasterProject = new MasterProjectDocument(this, documentUri, Log);

var subProject = new SubProjectDocument(this, documentUri, Log, MasterProject);
MasterProject.AddSubProject(subProject);
SubProjectDocument subProject = MasterProject.GetOrAddSubProject(documentUri,
() => new SubProjectDocument(this, documentUri, Log, MasterProject)
);

return subProject;
});
Expand Down

0 comments on commit 0d40200

Please sign in to comment.