[clang-tools-extra] [NFC] Avoid data race condition. (PR #118664)

Zahira Ammarguellat via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 4 13:51:03 PST 2024


https://github.com/zahiraam updated https://github.com/llvm/llvm-project/pull/118664

>From 35f795549d25ec1358b54ebd3f2176cf8e49aeff Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <Zahira.Ammarguellat at intel.com>
Date: Wed, 4 Dec 2024 08:23:12 -0800
Subject: [PATCH 1/2] [NFC] Avoid data race condition.

---
 clang-tools-extra/clangd/TUScheduler.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang-tools-extra/clangd/TUScheduler.cpp b/clang-tools-extra/clangd/TUScheduler.cpp
index 71548b59cc3088..96045380a3f21b 100644
--- a/clang-tools-extra/clangd/TUScheduler.cpp
+++ b/clang-tools-extra/clangd/TUScheduler.cpp
@@ -501,6 +501,7 @@ class PreambleThread {
       }
 
       {
+        std::unique_lock<std::mutex> 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 +1330,7 @@ void ASTWorker::startTask(llvm::StringRef Name,
                           std::optional<UpdateType> Update,
                           TUScheduler::ASTActionInvalidation Invalidation) {
   if (RunSync) {
+    std::lock_guard<std::mutex> Lock(Mutex);
     assert(!Done && "running a task after stop()");
     runTask(Name, Task);
     return;

>From 6dd0aa3de96e017fe25605150fcca3244dc4f27d Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <Zahira.Ammarguellat at intel.com>
Date: Wed, 4 Dec 2024 13:50:09 -0800
Subject: [PATCH 2/2] Remove unnecessary lock.

---
 clang-tools-extra/clangd/TUScheduler.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/clang-tools-extra/clangd/TUScheduler.cpp b/clang-tools-extra/clangd/TUScheduler.cpp
index 96045380a3f21b..75d878c9ee07a4 100644
--- a/clang-tools-extra/clangd/TUScheduler.cpp
+++ b/clang-tools-extra/clangd/TUScheduler.cpp
@@ -1330,7 +1330,6 @@ void ASTWorker::startTask(llvm::StringRef Name,
                           std::optional<UpdateType> Update,
                           TUScheduler::ASTActionInvalidation Invalidation) {
   if (RunSync) {
-    std::lock_guard<std::mutex> Lock(Mutex);
     assert(!Done && "running a task after stop()");
     runTask(Name, Task);
     return;



More information about the cfe-commits mailing list