Skip to content

Commit

Permalink
Make positionToDocComment thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Aug 12, 2024
1 parent 9102baf commit 998a289
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3191,10 +3191,10 @@ Expr * EvalState::parse(
std::shared_ptr<StaticEnv> & staticEnv)
{
DocCommentMap tmpDocComments; // Only used when not origin is not a SourcePath
DocCommentMap *docComments = &tmpDocComments;
auto * docComments = &tmpDocComments;

if (auto sourcePath = std::get_if<SourcePath>(&origin)) {
auto [it, _] = positionToDocComment.try_emplace(*sourcePath);
auto [it, _] = positionToDocComment.lock()->try_emplace(*sourcePath);
docComments = &it->second;
}

Expand All @@ -3212,8 +3212,10 @@ DocComment EvalState::getDocCommentForPos(PosIdx pos)
if (!path)
return {};

auto table = positionToDocComment.find(*path);
if (table == positionToDocComment.end())
auto positionToDocComment_ = positionToDocComment.readLock();

auto table = positionToDocComment_->find(*path);
if (table == positionToDocComment_->end())
return {};

auto it = table->second.find(pos);
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ private:
* Associate source positions of certain AST nodes with their preceding doc comment, if they have one.
* Grouped by file.
*/
std::unordered_map<SourcePath, DocCommentMap> positionToDocComment;
SharedSync<std::unordered_map<SourcePath, DocCommentMap>> positionToDocComment;

LookupPath lookupPath;

Expand Down

0 comments on commit 998a289

Please sign in to comment.