Files
TheRock/compiler/amd-llvm/clang-tools-extra/clangd/index/BackgroundQueue.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

152 lines
4.3 KiB
C++

//===-- BackgroundQueue.cpp - Task queue for background index -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "index/Background.h"
#include "support/Logger.h"
#include <optional>
namespace clang {
namespace clangd {
static std::atomic<bool> PreventStarvation = {false};
void BackgroundQueue::preventThreadStarvationInTests() {
PreventStarvation.store(true);
}
void BackgroundQueue::work(std::function<void()> OnIdle) {
while (true) {
std::optional<Task> Task;
{
std::unique_lock<std::mutex> Lock(Mu);
CV.wait(Lock, [&] { return ShouldStop || !Queue.empty(); });
if (ShouldStop) {
Queue.clear();
CV.notify_all();
return;
}
++Stat.Active;
std::pop_heap(Queue.begin(), Queue.end());
Task = std::move(Queue.back());
Queue.pop_back();
notifyProgress();
}
if (Task->ThreadPri != llvm::ThreadPriority::Default &&
!PreventStarvation.load())
llvm::set_thread_priority(Task->ThreadPri);
Task->Run();
if (Task->ThreadPri != llvm::ThreadPriority::Default)
llvm::set_thread_priority(llvm::ThreadPriority::Default);
{
std::unique_lock<std::mutex> Lock(Mu);
++Stat.Completed;
if (Stat.Active == 1 && Queue.empty()) {
// We just finished the last item, the queue is going idle.
assert(ShouldStop || Stat.Completed == Stat.Enqueued);
Stat.LastIdle = Stat.Completed;
if (OnIdle) {
Lock.unlock();
OnIdle();
Lock.lock();
}
}
assert(Stat.Active > 0 && "before decrementing");
--Stat.Active;
notifyProgress();
}
CV.notify_all();
}
}
void BackgroundQueue::stop() {
{
std::lock_guard<std::mutex> QueueLock(Mu);
ShouldStop = true;
}
CV.notify_all();
}
// Tweaks the priority of a newly-enqueued task, or returns false to cancel it.
bool BackgroundQueue::adjust(Task &T) {
// It is tempting to drop duplicates of queued tasks, and merely deprioritize
// duplicates of completed tasks (i.e. reindexing on CDB changes). But:
// - the background indexer doesn't support reindexing well, e.g. staleness
// is checked at *enqueue* time only, and doesn't account for compile flags
// - reindexing on compile flags is often a poor use of CPU in practice
if (T.Key && !SeenKeys.insert(T.Key).second)
return false;
T.QueuePri = std::max(T.QueuePri, Boosts.lookup(T.Tag));
return true;
}
void BackgroundQueue::push(Task T) {
{
std::lock_guard<std::mutex> Lock(Mu);
if (!adjust(T))
return;
Queue.push_back(std::move(T));
std::push_heap(Queue.begin(), Queue.end());
++Stat.Enqueued;
notifyProgress();
}
CV.notify_all();
}
void BackgroundQueue::append(std::vector<Task> Tasks) {
{
std::lock_guard<std::mutex> Lock(Mu);
for (Task &T : Tasks) {
if (!adjust(T))
continue;
Queue.push_back(std::move(T));
++Stat.Enqueued;
}
std::make_heap(Queue.begin(), Queue.end());
notifyProgress();
}
CV.notify_all();
}
void BackgroundQueue::boost(llvm::StringRef Tag, unsigned NewPriority) {
std::lock_guard<std::mutex> Lock(Mu);
unsigned &Boost = Boosts[Tag];
bool Increase = NewPriority > Boost;
Boost = NewPriority;
if (!Increase)
return; // existing tasks unaffected
unsigned Changes = 0;
for (Task &T : Queue)
if (Tag == T.Tag && NewPriority > T.QueuePri) {
T.QueuePri = NewPriority;
++Changes;
}
if (Changes)
std::make_heap(Queue.begin(), Queue.end());
// No need to signal, only rearranged items in the queue.
}
bool BackgroundQueue::blockUntilIdleForTest(
std::optional<double> TimeoutSeconds) {
std::unique_lock<std::mutex> Lock(Mu);
return wait(Lock, CV, timeoutSeconds(TimeoutSeconds),
[&] { return Queue.empty() && Stat.Active == 0; });
}
void BackgroundQueue::notifyProgress() const {
dlog("Queue: {0}/{1} ({2} active). Last idle at {3}", Stat.Completed,
Stat.Enqueued, Stat.Active, Stat.LastIdle);
if (OnProgress)
OnProgress(Stat);
}
} // namespace clangd
} // namespace clang