From ef094c0d78718db67ff3fcc3faf204222b5ce03c Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 23 Jun 2026 03:12:10 +0300 Subject: [PATCH] Phase C: replace CommandManager's hand-rolled pool with Void ThreadPool MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CommandManager dispatched async commands through a bespoke pool — a jthread vector, a CommandTask queue, a mutex/condition_variable, and a manual drain-on-shutdown loop. Replace it with a Void::Concurrency::ThreadPool: - startThreadPool() builds the pool at THREAD_POOL_SIZE; shutdown() flips the accept flag and resets the pool (~ThreadPool drains queued commands then joins — the same shutdown semantics the manual loop had). - dispatch() submits the CommandTask lambda straight to the pool instead of enqueuing + notifying; executeCommandAsync already swallows exceptions, so the discarded future is safe. - processCommandQueue() and the queue/cv/worker members are gone. Build green. Co-Authored-By: Claude Opus 4.8 --- include/echoion/core/CommandManager.hpp | 10 ++--- src/CommandManager.cpp | 59 +++++-------------------- 2 files changed, 17 insertions(+), 52 deletions(-) diff --git a/include/echoion/core/CommandManager.hpp b/include/echoion/core/CommandManager.hpp index 483f64b..3527f12 100644 --- a/include/echoion/core/CommandManager.hpp +++ b/include/echoion/core/CommandManager.hpp @@ -4,7 +4,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -174,13 +176,11 @@ namespace Echoion::Core }; void startThreadPool(); - void processCommandQueue(); void executeCommandAsync(CommandTask task); - std::vector workerThreads_; - std::queue commandQueue_; - std::mutex queueMutex_; - std::condition_variable queueCv_; + // Void worker pool backing async command execution (drains + // in-flight commands on shutdown via ~ThreadPool). + std::unique_ptr pool_; std::atomic shutdown_{false}; static constexpr size_t THREAD_POOL_SIZE = 4; // Handle up to 4 concurrent commands diff --git a/src/CommandManager.cpp b/src/CommandManager.cpp index fe6669c..e1c243e 100644 --- a/src/CommandManager.cpp +++ b/src/CommandManager.cpp @@ -119,53 +119,20 @@ namespace Echoion::Core void CommandManager::shutdown() { - { - std::lock_guard lock(queueMutex_); - shutdown_ = true; - } - queueCv_.notify_all(); - - for (auto& thread : workerThreads_) { - if (thread.joinable()) { - thread.join(); - } - } - workerThreads_.clear(); + shutdown_ = true; + // ~ThreadPool drains queued commands and joins its workers. + pool_.reset(); } void CommandManager::startThreadPool() { - for (size_t i = 0; i < THREAD_POOL_SIZE; ++i) { - workerThreads_.emplace_back([this]() { processCommandQueue(); }); - } + pool_ = + std::make_unique(THREAD_POOL_SIZE); ECHOION_LOG_INFO( std::format("CommandManager thread pool started with {} workers", THREAD_POOL_SIZE)); } - void CommandManager::processCommandQueue() - { - while (true) { - CommandTask task; - { - std::unique_lock lock(queueMutex_); - queueCv_.wait(lock, [this] { - return shutdown_ || !commandQueue_.empty(); - }); - - if (shutdown_ && commandQueue_.empty()) { - return; - } - - task = std::move(commandQueue_.front()); - commandQueue_.pop(); - } - - // Execute the command - executeCommandAsync(std::move(task)); - } - } - void CommandManager::executeCommandAsync(CommandTask task) { try { @@ -288,21 +255,19 @@ namespace Echoion::Core return true; // Command was found but not allowed } - // Queue the command for async execution + // Submit the command for async execution on the worker pool. + if (shutdown_ || !pool_) { + return false; + } CommandTask task{.clientId = clientId, .message = std::string(message), .musicManager = musicManager, .replyFunc = std::move(replyFunc), .invokerName = std::string(invokerName)}; - { - std::lock_guard lock(queueMutex_); - if (shutdown_) { - return false; - } - commandQueue_.push(std::move(task)); - } - queueCv_.notify_one(); + pool_->submit([this, task = std::move(task)]() mutable { + executeCommandAsync(std::move(task)); + }); return true; }