[PATCH] D61724: [clangd] Use AsyncTaskRunner in BackgroundIndex instead of std::thread

Ilya Biryukov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu May 9 04:27:02 PDT 2019


ilya-biryukov created this revision.
ilya-biryukov added a reviewer: kadircet.
Herald added subscribers: jfb, arphaman, jkorous, MaskRay.
Herald added a project: clang.

To unify the way we create threads in clangd.
This should simplify landing D50993 <https://reviews.llvm.org/D50993>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D61724

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


Index: clang-tools-extra/clangd/index/Background.h
===================================================================
--- clang-tools-extra/clangd/index/Background.h
+++ clang-tools-extra/clangd/index/Background.h
@@ -146,7 +146,7 @@
   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;
 };
 
Index: clang-tools-extra/clangd/index/Background.cpp
===================================================================
--- clang-tools-extra/clangd/index/Background.cpp
+++ clang-tools-extra/clangd/index/Background.cpp
@@ -148,19 +148,20 @@
           })) {
   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 = 1; I <= ThreadPoolSize; ++I) {
+    ThreadPool.runAsync("background-worker-" + llvm::Twine(I),
+                        [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() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61724.198788.patch
Type: text/x-patch
Size: 1637 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190509/d1f1fd86/attachment.bin>


More information about the cfe-commits mailing list