sema_manager: only keep latest session.maxNum sessions in case of a surge of textDocument/didChange

This commit is contained in:
Fangrui Song 2021-09-23 14:41:51 -07:00
parent 3ce756e39a
commit 5a48e6c419
2 changed files with 5 additions and 2 deletions

View File

@ -421,7 +421,8 @@ void *preambleMain(void *manager_) {
auto *manager = static_cast<SemaManager *>(manager_);
set_thread_name("preamble");
while (true) {
SemaManager::PreambleTask task = manager->preamble_tasks.dequeue();
SemaManager::PreambleTask task = manager->preamble_tasks.dequeue(
g_config ? g_config->session.maxNum : 0);
if (pipeline::g_quit.load(std::memory_order_relaxed))
break;

View File

@ -126,12 +126,14 @@ public:
bool isEmpty() { return total_count_ == 0; }
// Get the first element from the queue. Blocks until one is available.
T dequeue() {
T dequeue(int keep_only_latest = 0) {
std::unique_lock<std::mutex> lock(mutex_);
waiter_->cv.wait(lock,
[&]() { return !priority_.empty() || !queue_.empty(); });
auto execute = [&](std::deque<T> *q) {
if (keep_only_latest > 0 && q->size() > keep_only_latest)
q->erase(q->begin(), q->end() - keep_only_latest);
auto val = std::move(q->front());
q->pop_front();
--total_count_;