kadircet created this revision. kadircet added reviewers: sammccall, ioeric. Herald added subscribers: cfe-commits, jfb, arphaman, jkorous, MaskRay, ilya-biryukov.
Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D53651 Files: clangd/index/Background.cpp clangd/index/Background.h 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 <condition_variable> #include <deque> #include <string> @@ -34,7 +35,8 @@ // FIXME: resource-dir injection should be hoisted somewhere common. BackgroundIndex(Context BackgroundContext, StringRef ResourceDir, const FileSystemProvider &, - ArrayRef<std::string> URISchemes = {}); + ArrayRef<std::string> 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<Task> Queue; - std::thread Thread; // Must be last, spawned thread reads instance vars. + // Must be last, spawned thread reads instance vars. + llvm::SmallVector<std::thread, 8> 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<std::string> URISchemes) + ArrayRef<std::string> URISchemes, + size_t ThreadPoolSize) : SwapIndex(make_unique<MemIndex>()), 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> Task; { @@ -80,7 +86,7 @@ std::lock_guard<std::mutex> Lock(QueueMu); enqueueLocked(std::move(Cmd)); } - QueueCV.notify_all(); + QueueCV.notify_one(); } void BackgroundIndex::enqueueAll(StringRef Directory,
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 <condition_variable> #include <deque> #include <string> @@ -34,7 +35,8 @@ // FIXME: resource-dir injection should be hoisted somewhere common. BackgroundIndex(Context BackgroundContext, StringRef ResourceDir, const FileSystemProvider &, - ArrayRef<std::string> URISchemes = {}); + ArrayRef<std::string> 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<Task> Queue; - std::thread Thread; // Must be last, spawned thread reads instance vars. + // Must be last, spawned thread reads instance vars. + llvm::SmallVector<std::thread, 8> 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<std::string> URISchemes) + ArrayRef<std::string> URISchemes, + size_t ThreadPoolSize) : SwapIndex(make_unique<MemIndex>()), 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> Task; { @@ -80,7 +86,7 @@ std::lock_guard<std::mutex> Lock(QueueMu); enqueueLocked(std::move(Cmd)); } - QueueCV.notify_all(); + QueueCV.notify_one(); } void BackgroundIndex::enqueueAll(StringRef Directory,
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits