diff --git a/clang-tools-extra/clangd/TUScheduler.cpp b/clang-tools-extra/clangd/TUScheduler.cpp index 71548b59cc308..7a05a3d461370 100644 --- a/clang-tools-extra/clangd/TUScheduler.cpp +++ b/clang-tools-extra/clangd/TUScheduler.cpp @@ -501,6 +501,8 @@ class PreambleThread { } { + // Add a lock guard to protect the critical section. + std::lock_guard Lock(Mutex); WithContext Guard(std::move(CurrentReq->Ctx)); // Note that we don't make use of the ContextProvider here. // Preamble tasks are always scheduled by ASTWorker tasks, and we @@ -1329,6 +1331,8 @@ void ASTWorker::startTask(llvm::StringRef Name, std::optional Update, TUScheduler::ASTActionInvalidation Invalidation) { if (RunSync) { + // Add a lock guard to protect the critical section. + std::lock_guard Lock(Mutex); assert(!Done && "running a task after stop()"); runTask(Name, Task); return; diff --git a/clang-tools-extra/clangd/index/BackgroundRebuild.cpp b/clang-tools-extra/clangd/index/BackgroundRebuild.cpp index 79383be012f83..b8677a32741b2 100644 --- a/clang-tools-extra/clangd/index/BackgroundRebuild.cpp +++ b/clang-tools-extra/clangd/index/BackgroundRebuild.cpp @@ -34,6 +34,8 @@ bool BackgroundIndexRebuilder::enoughTUsToRebuild() const { void BackgroundIndexRebuilder::indexedTU() { maybeRebuild("after indexing enough files", [this] { + // Add a lock guard to protect the critical section + std::lock_guard Lock(Mu); ++IndexedTUs; if (Loading) return false; // rebuild once loading finishes @@ -64,6 +66,8 @@ void BackgroundIndexRebuilder::loadedShard(size_t ShardCount) { } void BackgroundIndexRebuilder::doneLoading() { maybeRebuild("after loading index from disk", [this] { + // Add a lock guard to protect the critical section. + std::lock_guard Lock(Mu); assert(Loading); --Loading; if (Loading) // was loading multiple batches concurrently