Skip to content

Commit

Permalink
[clangd] Use AsyncTaskRunner in BackgroundIndex instead of std::thread
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ilya-biryukov committed May 9, 2019
1 parent d7b650c commit db68b10
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions clang-tools-extra/clangd/index/Background.cpp
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/index/Background.h
Expand Up @@ -146,7 +146,7 @@ class BackgroundIndex : public SwapIndex {
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;
};

Expand Down

0 comments on commit db68b10

Please sign in to comment.