Integrate the Void executor and rule manifests into M01

This commit is contained in:
faumaray committed 2026-12-02 21:31:51 +03:00
commit cf0cc2f32c
10 files changed
+406 -81

No files matched your search

+29 -40
View File
@@ -3,54 +3,43 @@
#include <stdexcept>
namespace settlement {
namespace {
WorkerPool::WorkerPool(std::size_t thread_count) {
std::unique_ptr<Void::Async::ThreadExecutor> 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");
}
workers_.reserve(thread_count);
try {
for (std::size_t index = 0; index < thread_count; ++index) {
workers_.emplace_back([this] { worker_loop(); });
}
} catch (...) {
{
std::lock_guard<std::mutex> lock(mutex_);
stopping_ = true;
}
work_available_.notify_all();
for (std::thread& worker : workers_) {
if (worker.joinable()) worker.join();
}
throw;
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<void> {
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);
}
WorkerPool::~WorkerPool() {
{
std::lock_guard<std::mutex> lock(mutex_);
stopping_ = true;
}
work_available_.notify_all();
for (std::thread& worker : workers_) {
if (worker.joinable()) worker.join();
}
}
} // namespace
std::size_t WorkerPool::thread_count() const noexcept { return workers_.size(); }
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_) {}
void WorkerPool::worker_loop() {
for (;;) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(mutex_);
work_available_.wait(lock, [this] { return stopping_ || !tasks_.empty(); });
if (stopping_ && tasks_.empty()) return;
task = std::move(tasks_.front());
tasks_.pop();
}
task();
}
}
// 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
+20 -2
View File
@@ -1,5 +1,7 @@
#include "settlement/core/world.hpp"
#include <Void/Core/Scope.hpp>
#include <algorithm>
#include <array>
#include <cassert>
@@ -279,12 +281,13 @@ void Storage::add_item(ItemType type) {
}
World::World(std::uint64_t seed, std::int32_t width, std::int32_t height,
std::size_t workers, ExecutionMode mode)
std::size_t workers, ExecutionMode mode, WorkerPoolOptions worker_options)
: seed_(seed),
width_(width),
height_(height),
worker_count_(checked_worker_count(workers, mode)),
execution_mode_(mode),
worker_options_(std::move(worker_options)),
blocked_(checked_cell_count(width, height), 0) {}
std::size_t World::cell_index(GridPosition position) const noexcept {
@@ -1078,6 +1081,11 @@ void World::assign_idle_agents() {
} else {
std::vector<std::future<std::vector<std::size_t>>> choice_futures;
choice_futures.reserve(chunk_count);
const auto drain_choices = Void::Scope::on_exit([&choice_futures] {
for (auto& future : choice_futures) {
if (future.valid()) future.wait();
}
});
for (std::size_t chunk = 0; chunk < chunk_count; ++chunk) {
const std::size_t begin = chunk_start(chunk);
const std::size_t end = chunk_start(chunk + 1);
@@ -1104,6 +1112,11 @@ void World::assign_idle_agents() {
if (requests.empty()) return;
std::vector<std::future<PathResult>> futures;
const auto drain_paths = Void::Scope::on_exit([&futures] {
for (auto& future : futures) {
if (future.valid()) future.wait();
}
});
if (execution_mode_ == ExecutionMode::Parallel) {
futures.reserve(requests.size());
for (const Request& request : requests) {
@@ -1584,7 +1597,7 @@ void World::grow_sites() {
void World::step(TickProfile* profile) {
if (paused_) return;
if (execution_mode_ == ExecutionMode::Parallel && !worker_pool_ && !agents_.empty()) {
worker_pool_ = std::make_unique<WorkerPool>(worker_count_);
worker_pool_ = std::make_unique<WorkerPool>(worker_count_, worker_options_);
}
using Clock = std::chrono::steady_clock;
auto phase_start = profile ? Clock::now() : Clock::time_point{};
@@ -1662,6 +1675,11 @@ void World::step(TickProfile* profile) {
const auto chunk_count = std::min(worker_count_, agents_.size());
std::vector<std::future<std::uint64_t>> futures;
futures.reserve(chunk_count);
const auto drain_advancement = Void::Scope::on_exit([&futures] {
for (auto& future : futures) {
if (future.valid()) future.wait();
}
});
for (std::size_t chunk = 0; chunk < chunk_count; ++chunk) {
const std::size_t begin = chunk * agents_.size() / chunk_count;
const std::size_t end = (chunk + 1) * agents_.size() / chunk_count;
+3 -2
View File
@@ -609,7 +609,8 @@ void World::write_save(std::ostream& stream) const {
WorldSaveCodec::body(archive, *this, save_version);
}
std::unique_ptr<World> World::read_save(std::istream& stream, std::size_t workers, ExecutionMode mode) {
std::unique_ptr<World> World::read_save(std::istream& stream, std::size_t workers, ExecutionMode mode,
WorkerPoolOptions worker_options) {
SaveReader archive(stream);
for (char letter : signature) {
char actual = 0;
@@ -626,7 +627,7 @@ std::unique_ptr<World> World::read_save(std::istream& stream, std::size_t worker
archive.field(seed);
archive.field(width);
archive.field(height);
auto world = std::make_unique<World>(seed, width, height, workers, mode);
auto world = std::make_unique<World>(seed, width, height, workers, mode, std::move(worker_options));
WorldSaveCodec::body(archive, *world, version);
archive.finish();
if (version < 5) {