Index: clangd/index/Background.h =================================================================== --- clangd/index/Background.h +++ clangd/index/Background.h @@ -16,6 +16,7 @@ #include "index/Index.h" #include "clang/Tooling/CompilationDatabase.h" #include "llvm/Support/SHA1.h" +#include "TUScheduler.h" #include #include #include @@ -34,7 +35,8 @@ // FIXME: resource-dir injection should be hoisted somewhere common. BackgroundIndex(Context BackgroundContext, StringRef ResourceDir, const FileSystemProvider &, - ArrayRef URISchemes = {}); + ArrayRef URISchemes = {}, + size_t ThreadPoolSize = getDefaultAsyncThreadsCount()); ~BackgroundIndex(); // Blocks while the current task finishes. // Enqueue a translation unit for indexing. @@ -74,7 +76,8 @@ std::condition_variable QueueCV; bool ShouldStop = false; std::deque Queue; - std::thread Thread; // Must be last, spawned thread reads instance vars. + // Must be last, spawned thread reads instance vars. + llvm::SmallVector ThreadPool; }; } // namespace clangd Index: clangd/index/Background.cpp =================================================================== --- clangd/index/Background.cpp +++ clangd/index/Background.cpp @@ -25,14 +25,20 @@ BackgroundIndex::BackgroundIndex(Context BackgroundContext, StringRef ResourceDir, const FileSystemProvider &FSProvider, - ArrayRef URISchemes) + ArrayRef URISchemes, + size_t ThreadPoolSize) : SwapIndex(make_unique()), ResourceDir(ResourceDir), FSProvider(FSProvider), BackgroundContext(std::move(BackgroundContext)), - URISchemes(URISchemes), Thread([this] { run(); }) {} + URISchemes(URISchemes) { + assert(ThreadPoolSize > 0 && "Thread pool size can't be zero."); + while(ThreadPoolSize--) + ThreadPool.emplace_back([this] { run(); }); +} BackgroundIndex::~BackgroundIndex() { stop(); - Thread.join(); + for (auto& Thread : ThreadPool) + Thread.join(); } void BackgroundIndex::stop() { @@ -44,7 +50,7 @@ } void BackgroundIndex::run() { - WithContext Background(std::move(BackgroundContext)); + WithContext Background(BackgroundContext.clone()); while (true) { Optional Task; { @@ -80,7 +86,7 @@ std::lock_guard Lock(QueueMu); enqueueLocked(std::move(Cmd)); } - QueueCV.notify_all(); + QueueCV.notify_one(); } void BackgroundIndex::enqueueAll(StringRef Directory,