Phase C: replace CommandManager's hand-rolled pool with Void ThreadPool
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
da708fa9bb
commit
ef094c0d78
2 files changed
+17
-52
No files matched your search
@@ -4,7 +4,9 @@
|
||||
#include <ConfigTypes.hpp>
|
||||
#include <PluginTypes.hpp>
|
||||
#include <Types.hpp>
|
||||
#include <Void/Async/ThreadPool.hpp>
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
@@ -174,13 +176,11 @@ namespace Echoion::Core
|
||||
};
|
||||
|
||||
void startThreadPool();
|
||||
void processCommandQueue();
|
||||
void executeCommandAsync(CommandTask task);
|
||||
|
||||
std::vector<std::jthread> workerThreads_;
|
||||
std::queue<CommandTask> 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<Void::Concurrency::ThreadPool> pool_;
|
||||
std::atomic<bool> shutdown_{false};
|
||||
static constexpr size_t THREAD_POOL_SIZE =
|
||||
4; // Handle up to 4 concurrent commands
|
||||
|
||||
+12
-47
@@ -119,53 +119,20 @@ namespace Echoion::Core
|
||||
|
||||
void CommandManager::shutdown()
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> 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<Void::Concurrency::ThreadPool>(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<std::mutex> 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<std::mutex> 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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user