Integrate the Void executor and rule manifests into M01
This commit is contained in:
commit
cf0cc2f32c
10 files changed
+406
-81
No files matched your search
@@ -1,55 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
#include <condition_variable>
|
||||
#include <Void/Async/FuturePool.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <stdexcept>
|
||||
#include <thread>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace settlement {
|
||||
|
||||
// Limits and optional fault hooks are execution settings and are never saved.
|
||||
// Hooks use zero-based worker/submission indices and may throw. Execution
|
||||
// hooks can run concurrently on workers or on the coordinator during overload.
|
||||
struct WorkerPoolOptions {
|
||||
std::size_t max_admitted = 4096;
|
||||
std::function<void(std::size_t)> before_worker_start;
|
||||
std::function<void(std::size_t)> before_submit;
|
||||
std::function<void(std::size_t)> before_execute;
|
||||
};
|
||||
|
||||
class WorkerPool {
|
||||
public:
|
||||
explicit WorkerPool(std::size_t thread_count);
|
||||
explicit WorkerPool(std::size_t thread_count, WorkerPoolOptions options = {});
|
||||
~WorkerPool();
|
||||
|
||||
WorkerPool(const WorkerPool&) = delete;
|
||||
WorkerPool& operator=(const WorkerPool&) = delete;
|
||||
|
||||
// World submits from its coordinator. Nested submission from a worker is
|
||||
// rejected before admission, preventing a worker from waiting on its own pool.
|
||||
template <typename Function>
|
||||
auto submit(Function&& function) -> std::future<std::invoke_result_t<Function>> {
|
||||
using Result = std::invoke_result_t<Function>;
|
||||
auto task = std::make_shared<std::packaged_task<Result()>>(std::forward<Function>(function));
|
||||
std::future<Result> result = task->get_future();
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (stopping_) {
|
||||
throw std::runtime_error("cannot submit work to a stopped worker pool");
|
||||
}
|
||||
tasks_.emplace([task]() { (*task)(); });
|
||||
if (executor_->is_executor_thread()) {
|
||||
throw std::logic_error("cannot submit nested work to the same worker pool");
|
||||
}
|
||||
work_available_.notify_one();
|
||||
return result;
|
||||
if (options_.before_submit || options_.before_execute) {
|
||||
const auto index = submission_index_.fetch_add(1, std::memory_order_relaxed);
|
||||
if (options_.before_submit) options_.before_submit(index);
|
||||
return admit([this, index, function = std::forward<Function>(function)]() mutable -> decltype(auto) {
|
||||
if (options_.before_execute) options_.before_execute(index);
|
||||
return std::invoke(function);
|
||||
});
|
||||
}
|
||||
return admit(std::forward<Function>(function));
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t thread_count() const noexcept;
|
||||
|
||||
private:
|
||||
void worker_loop();
|
||||
template <typename Function>
|
||||
auto admit(Function&& function) -> std::future<std::invoke_result_t<Function>> {
|
||||
auto submitted = futures_.try_submit(std::forward<Function>(function));
|
||||
if (submitted) return std::move(*submitted);
|
||||
if (submitted.error() != Void::Async::AdmissionError::overloaded) {
|
||||
throw std::runtime_error("cannot submit work to a stopped worker pool");
|
||||
}
|
||||
// Failed admission leaves the callable untouched. Running it on the
|
||||
// coordinator retains the same logical work and stores exceptions in its future.
|
||||
using Result = std::invoke_result_t<Function>;
|
||||
std::packaged_task<Result()> task(std::forward<Function>(function));
|
||||
auto result = task.get_future();
|
||||
task();
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::thread> workers_;
|
||||
std::queue<std::function<void()>> tasks_;
|
||||
std::mutex mutex_;
|
||||
std::condition_variable work_available_;
|
||||
bool stopping_ = false;
|
||||
std::size_t thread_count_;
|
||||
WorkerPoolOptions options_;
|
||||
std::atomic<std::size_t> submission_index_{0};
|
||||
std::unique_ptr<Void::Async::ThreadExecutor> executor_;
|
||||
Void::Async::FuturePool futures_;
|
||||
};
|
||||
|
||||
} // namespace settlement
|
||||
@@ -349,7 +349,8 @@ class World {
|
||||
public:
|
||||
// SerialReference uses one logical worker and executes on the calling thread.
|
||||
World(std::uint64_t seed, std::int32_t width, std::int32_t height,
|
||||
std::size_t workers = min_worker_count, ExecutionMode mode = ExecutionMode::Parallel);
|
||||
std::size_t workers = min_worker_count, ExecutionMode mode = ExecutionMode::Parallel,
|
||||
WorkerPoolOptions worker_options = {});
|
||||
|
||||
void set_blocked(GridPosition position, bool blocked = true);
|
||||
[[nodiscard]] bool is_walkable(GridPosition position) const noexcept;
|
||||
@@ -388,7 +389,8 @@ public:
|
||||
void write_svg(const std::string& path) const;
|
||||
void write_save(std::ostream& stream) const;
|
||||
[[nodiscard]] static std::unique_ptr<World> read_save(
|
||||
std::istream& stream, std::size_t workers = min_worker_count, ExecutionMode mode = ExecutionMode::Parallel);
|
||||
std::istream& stream, std::size_t workers = min_worker_count, ExecutionMode mode = ExecutionMode::Parallel,
|
||||
WorkerPoolOptions worker_options = {});
|
||||
|
||||
[[nodiscard]] std::uint64_t tick() const noexcept;
|
||||
[[nodiscard]] std::uint64_t seed() const noexcept;
|
||||
@@ -449,6 +451,7 @@ private:
|
||||
std::int32_t height_;
|
||||
std::size_t worker_count_;
|
||||
ExecutionMode execution_mode_;
|
||||
WorkerPoolOptions worker_options_;
|
||||
std::unique_ptr<WorkerPool> worker_pool_;
|
||||
AgentId next_agent_id_ = 1;
|
||||
JobId next_job_id_ = 1;
|
||||
|
||||
Reference in new issue
Block a user