[clangd] Use AsyncTaskRunner in BackgroundIndex instead of std::thread
authorIlya Biryukov <ibiryukov@google.com>
Thu, 9 May 2019 12:04:07 +0000 (12:04 +0000)
committerIlya Biryukov <ibiryukov@google.com>
Thu, 9 May 2019 12:04:07 +0000 (12:04 +0000)
Summary:
To unify the way we create threads in clangd.
This should simplify landing D50993.

Reviewers: kadircet

Reviewed By: kadircet

Subscribers: MaskRay, jkorous, arphaman, jfb, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D61724

llvm-svn: 360332

clang-tools-extra/clangd/index/Background.cpp
clang-tools-extra/clangd/index/Background.h

index 0746a35..6705394 100644 (file)
@@ -148,19 +148,20 @@ BackgroundIndex::BackgroundIndex(
           })) {
   assert(ThreadPoolSize > 0 && "Thread pool size can't be zero.");
   assert(this->IndexStorageFactory && "Storage factory can not be null!");
-  while (ThreadPoolSize--)
-    ThreadPool.emplace_back([this] { run(); });
+  for (unsigned I = 0; I < ThreadPoolSize; ++I) {
+    ThreadPool.runAsync("background-worker-" + llvm::Twine(I + 1),
+                        [this] { run(); });
+  }
   if (BuildIndexPeriodMs > 0) {
     log("BackgroundIndex: build symbol index periodically every {0} ms.",
         BuildIndexPeriodMs);
-    ThreadPool.emplace_back([this] { buildIndex(); });
+    ThreadPool.runAsync("background-index-builder", [this] { buildIndex(); });
   }
 }
 
 BackgroundIndex::~BackgroundIndex() {
   stop();
-  for (auto &Thread : ThreadPool)
-    Thread.join();
+  ThreadPool.wait();
 }
 
 void BackgroundIndex::stop() {
index 2132e57..cc530d5 100644 (file)
@@ -146,7 +146,7 @@ private:
   std::condition_variable QueueCV;
   bool ShouldStop = false;
   std::deque<std::pair<Task, llvm::ThreadPriority>> Queue;
-  std::vector<std::thread> ThreadPool; // FIXME: Abstract this away.
+  AsyncTaskRunner ThreadPool;
   GlobalCompilationDatabase::CommandChanged::Subscription CommandsChanged;
 };