diff --git a/src/pipeline.cc b/src/pipeline.cc index 78ecde80..3fa887ba 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -423,6 +423,7 @@ void ThreadLeave() { std::lock_guard lock(thread_mtx); if (!--pending_threads) no_pending_threads.notify_one(); + assert(pending_threads >= 0); } void Init() { @@ -526,13 +527,13 @@ void LaunchStdin() { ReflectMember(reader, "method", method); if (method.empty()) continue; + const auto should_exit = method == "exit"; // g_config is not available before "initialize". Use 0 in that case. on_request->PushBack( {id, std::move(method), std::move(message), std::move(document), chrono::steady_clock::now() + chrono::milliseconds(g_config ? g_config->request.timeout : 0)}); - - if (method == "exit") + if (should_exit) break; } }).detach(); diff --git a/src/sema_manager.cc b/src/sema_manager.cc index 585319e6..61542ef1 100644 --- a/src/sema_manager.cc +++ b/src/sema_manager.cc @@ -406,6 +406,10 @@ void BuildPreamble(Session &session, CompilerInvocation &CI, void *PreambleMain(void *manager_) { auto *manager = static_cast(manager_); + pipeline::ThreadEnter(); + struct RAII { + ~RAII() { pipeline::ThreadLeave(); } + } raii{}; set_thread_name("preamble"); while (true) { SemaManager::PreambleTask task = manager->preamble_tasks.Dequeue(); @@ -432,12 +436,15 @@ void *PreambleMain(void *manager_) { manager->ScheduleDiag(task.path, debounce); } } - pipeline::ThreadLeave(); return nullptr; } void *CompletionMain(void *manager_) { auto *manager = static_cast(manager_); + pipeline::ThreadEnter(); + struct RAII { + ~RAII() { pipeline::ThreadLeave(); } + } raii{}; set_thread_name("comp"); while (true) { std::unique_ptr task = manager->comp_tasks.Dequeue(); @@ -493,7 +500,6 @@ void *CompletionMain(void *manager_) { task->on_complete(&Clang->getCodeCompletionConsumer()); } - pipeline::ThreadLeave(); return nullptr; } @@ -527,6 +533,10 @@ void printDiag(llvm::raw_string_ostream &OS, const DiagBase &d) { void *DiagnosticMain(void *manager_) { auto *manager = static_cast(manager_); + pipeline::ThreadEnter(); + struct RAII { + ~RAII() { pipeline::ThreadLeave(); } + } raii{}; set_thread_name("diag"); while (true) { SemaManager::DiagTask task = manager->diag_tasks.Dequeue(); @@ -643,7 +653,6 @@ void *DiagnosticMain(void *manager_) { } manager->on_diagnostic_(task.path, ls_diags); } - pipeline::ThreadLeave(); return nullptr; }