#include "settlement/core/worker_pool.hpp" #include namespace settlement { namespace { std::unique_ptr start_executor( std::size_t thread_count, const WorkerPoolOptions& options) { if (thread_count < 2) { throw std::invalid_argument("worker pool needs at least two threads"); } if (options.max_admitted == 0) { throw std::invalid_argument("worker pool capacity must be positive"); } Void::Async::ThreadExecutorOptions settings; settings.workers = thread_count; settings.max_admitted = options.max_admitted; if (options.before_worker_start) { settings.before_worker_start = [hook = options.before_worker_start](std::size_t index) -> Void::Result::Result { hook(index); return {}; }; } auto executor = Void::Async::ThreadExecutor::create(std::move(settings)); if (!executor) throw std::runtime_error(executor.error().message()); const auto started = (*executor)->start(); if (!started) throw std::runtime_error(started.error().message()); return std::move(*executor); } } // namespace WorkerPool::WorkerPool(std::size_t thread_count, WorkerPoolOptions options) : thread_count_(thread_count), options_(std::move(options)), executor_(start_executor(thread_count, options_)), futures_(*executor_) {} // ThreadExecutor's destructor closes admission and joins every accepted task. // options_ outlives that join so fault-hook captures remain valid through shutdown. WorkerPool::~WorkerPool() = default; std::size_t WorkerPool::thread_count() const noexcept { return thread_count_; } } // namespace settlement