Skip to content

Commit 3daf0ab

Browse files
committed
Fix potential data race condition.
1 parent d09697f commit 3daf0ab

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

clang-tools-extra/clangd/TUScheduler.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,8 @@ class PreambleThread {
501501
}
502502

503503
{
504+
// Add a lock guard to protect the critical section.
505+
std::lock_guard<std::mutex> Lock(Mutex);
504506
WithContext Guard(std::move(CurrentReq->Ctx));
505507
// Note that we don't make use of the ContextProvider here.
506508
// Preamble tasks are always scheduled by ASTWorker tasks, and we
@@ -1329,6 +1331,8 @@ void ASTWorker::startTask(llvm::StringRef Name,
13291331
std::optional<UpdateType> Update,
13301332
TUScheduler::ASTActionInvalidation Invalidation) {
13311333
if (RunSync) {
1334+
// Add a lock guard to protect the critical section.
1335+
std::lock_guard<std::mutex> Lock(Mutex);
13321336
assert(!Done && "running a task after stop()");
13331337
runTask(Name, Task);
13341338
return;

clang-tools-extra/clangd/index/BackgroundRebuild.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ bool BackgroundIndexRebuilder::enoughTUsToRebuild() const {
3434

3535
void BackgroundIndexRebuilder::indexedTU() {
3636
maybeRebuild("after indexing enough files", [this] {
37+
// Add a lock guard to protect the critical section
38+
std::lock_guard<std::mutex> Lock(Mu);
3739
++IndexedTUs;
3840
if (Loading)
3941
return false; // rebuild once loading finishes
@@ -64,6 +66,8 @@ void BackgroundIndexRebuilder::loadedShard(size_t ShardCount) {
6466
}
6567
void BackgroundIndexRebuilder::doneLoading() {
6668
maybeRebuild("after loading index from disk", [this] {
69+
// Add a lock guard to protect the critical section.
70+
std::lock_guard<std::mutex> Lock(Mu);
6771
assert(Loading);
6872
--Loading;
6973
if (Loading) // was loading multiple batches concurrently

0 commit comments

Comments
 (0)