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
+16
-51
No files matched your search
@@ -4,7 +4,9 @@
|
|||||||
#include <ConfigTypes.hpp>
|
#include <ConfigTypes.hpp>
|
||||||
#include <PluginTypes.hpp>
|
#include <PluginTypes.hpp>
|
||||||
#include <Types.hpp>
|
#include <Types.hpp>
|
||||||
|
#include <Void/Async/ThreadPool.hpp>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <memory>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <future>
|
#include <future>
|
||||||
@@ -174,13 +176,11 @@ namespace Echoion::Core
|
|||||||
};
|
};
|
||||||
|
|
||||||
void startThreadPool();
|
void startThreadPool();
|
||||||
void processCommandQueue();
|
|
||||||
void executeCommandAsync(CommandTask task);
|
void executeCommandAsync(CommandTask task);
|
||||||
|
|
||||||
std::vector<std::jthread> workerThreads_;
|
// Void worker pool backing async command execution (drains
|
||||||
std::queue<CommandTask> commandQueue_;
|
// in-flight commands on shutdown via ~ThreadPool).
|
||||||
std::mutex queueMutex_;
|
std::unique_ptr<Void::Concurrency::ThreadPool> pool_;
|
||||||
std::condition_variable queueCv_;
|
|
||||||
std::atomic<bool> shutdown_{false};
|
std::atomic<bool> shutdown_{false};
|
||||||
static constexpr size_t THREAD_POOL_SIZE =
|
static constexpr size_t THREAD_POOL_SIZE =
|
||||||
4; // Handle up to 4 concurrent commands
|
4; // Handle up to 4 concurrent commands
|
||||||
|
|||||||
+11
-46
@@ -119,53 +119,20 @@ namespace Echoion::Core
|
|||||||
|
|
||||||
void CommandManager::shutdown()
|
void CommandManager::shutdown()
|
||||||
{
|
{
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(queueMutex_);
|
|
||||||
shutdown_ = true;
|
shutdown_ = true;
|
||||||
}
|
// ~ThreadPool drains queued commands and joins its workers.
|
||||||
queueCv_.notify_all();
|
pool_.reset();
|
||||||
|
|
||||||
for (auto& thread : workerThreads_) {
|
|
||||||
if (thread.joinable()) {
|
|
||||||
thread.join();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
workerThreads_.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CommandManager::startThreadPool()
|
void CommandManager::startThreadPool()
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < THREAD_POOL_SIZE; ++i) {
|
pool_ =
|
||||||
workerThreads_.emplace_back([this]() { processCommandQueue(); });
|
std::make_unique<Void::Concurrency::ThreadPool>(THREAD_POOL_SIZE);
|
||||||
}
|
|
||||||
ECHOION_LOG_INFO(
|
ECHOION_LOG_INFO(
|
||||||
std::format("CommandManager thread pool started with {} workers",
|
std::format("CommandManager thread pool started with {} workers",
|
||||||
THREAD_POOL_SIZE));
|
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)
|
void CommandManager::executeCommandAsync(CommandTask task)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@@ -288,21 +255,19 @@ namespace Echoion::Core
|
|||||||
return true; // Command was found but not allowed
|
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,
|
CommandTask task{.clientId = clientId,
|
||||||
.message = std::string(message),
|
.message = std::string(message),
|
||||||
.musicManager = musicManager,
|
.musicManager = musicManager,
|
||||||
.replyFunc = std::move(replyFunc),
|
.replyFunc = std::move(replyFunc),
|
||||||
.invokerName = std::string(invokerName)};
|
.invokerName = std::string(invokerName)};
|
||||||
|
|
||||||
{
|
pool_->submit([this, task = std::move(task)]() mutable {
|
||||||
std::lock_guard<std::mutex> lock(queueMutex_);
|
executeCommandAsync(std::move(task));
|
||||||
if (shutdown_) {
|
});
|
||||||
return false;
|
|
||||||
}
|
|
||||||
commandQueue_.push(std::move(task));
|
|
||||||
}
|
|
||||||
queueCv_.notify_one();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user