#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Void::Runtime { enum class HostState { constructed, starting, running, stopping_admission, draining, joining, stopped, failed_start, shutdown_incomplete }; struct EmbeddedResources { Async::Executor* executor{nullptr}; IO::Driver* io{nullptr}; const Void::Time::MonotonicClock* clock{&Void::Time::SystemMonotonicClock::instance()}; Void::Memory::BudgetResource* memory_budget{nullptr}; [[nodiscard]] Void::Result::Result validate() const { if (executor == nullptr) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_argument, "EmbeddedResources::executor must not be null")}; } if (io == nullptr) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_argument, "EmbeddedResources::io must not be null")}; } if (clock == nullptr) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_argument, "EmbeddedResources::clock must not be null")}; } return {}; } }; struct ShutdownReport { bool complete{true}; HostState state{HostState::stopped}; std::size_t pending_owners{0}; std::vector pending_owner_ids{}; ShutdownPhase phase{ShutdownPhase::not_started}; std::size_t pending_business_tasks{0}; std::size_t pending_cpu_admitted{0}; std::size_t pending_io_operations{0}; std::size_t retained_buffer_bytes{0}; bool drain_grace_expired{false}; bool export_flush_expired{false}; bool owner_release_pending{false}; }; [[nodiscard]] inline ResourceGraph make_default_resource_graph(bool embedded_mode) { const ResourceOwnership own = embedded_mode ? ResourceOwnership::borrowed : ResourceOwnership::owned; ResourceGraph g; (void)g.add_node(ResourceNode{"clock", ResourceKind::clock, own, true}); (void)g.add_node(ResourceNode{"memory_budget", ResourceKind::memory_budget, own, true}); (void)g.add_node(ResourceNode{"executor", ResourceKind::executor, own, true}); (void)g.add_node(ResourceNode{"io_driver", ResourceKind::io_driver, own, true}); (void)g.add_dependency("executor", "clock"); (void)g.add_dependency("executor", "memory_budget"); (void)g.add_dependency("io_driver", "clock"); (void)g.add_dependency("io_driver", "memory_budget"); (void)g.add_dependency("io_driver", "executor"); return g; } } // namespace Void::Runtime #include namespace Void::Runtime { /// Runtime composition handle managing startup, readiness, business execution, /// and phased structured shutdown. Destruction blocks until shutdown is complete /// in managed mode (or in embedded mode with a ManualExecutor). class Host { public: Host(const Host&) = delete; Host& operator=(const Host&) = delete; Host(Host&&) noexcept = default; Host& operator=(Host&&) noexcept = default; ~Host() = default; [[nodiscard]] static Void::Result::Result create(Options options = {}) { if (auto ok = validate_options(options); !ok.has_value()) { return std::unexpected{std::move(ok.error())}; } ResourceGraph g = make_default_resource_graph(options.embedded_mode); auto plan = g.validate_and_plan(); if (!plan.has_value()) { return std::unexpected{std::move(plan.error())}; } auto budget = std::make_unique( options.limits.max_owned_memory_bytes); return Host{ std::move(options), std::move(*plan), EmbeddedResources{}, std::move(budget), {}}; } [[nodiscard]] static Void::Result::Result create_embedded( Options options, EmbeddedResources embedded) { options.embedded_mode = true; if (auto ok = validate_options(options); !ok.has_value()) { return std::unexpected{std::move(ok.error())}; } if (auto eok = embedded.validate(); !eok.has_value()) { return std::unexpected{std::move(eok.error())}; } ResourceGraph g = make_default_resource_graph(true); auto plan = g.validate_and_plan(); if (!plan.has_value()) { return std::unexpected{std::move(plan.error())}; } std::unique_ptr budget; if (embedded.memory_budget == nullptr) { budget = std::make_unique( options.limits.max_owned_memory_bytes); } return Host{ std::move(options), std::move(*plan), embedded, std::move(budget), {}}; } [[nodiscard]] bool valid() const noexcept { return core_ != nullptr; } [[nodiscard]] HostState state() const noexcept { return core_ ? core_->current_state() : HostState::stopped; } [[nodiscard]] bool is_running() const noexcept { return state() == HostState::running; } [[nodiscard]] bool is_ready() const noexcept { return core_ ? core_->current_ready() : false; } [[nodiscard]] std::size_t started_worker_count() const noexcept { return core_ ? core_->current_workers() : 0; } [[nodiscard]] std::size_t cpu_admitted() const noexcept { return core_ ? core_->cpu_admitted_count() : 0; } [[nodiscard]] Async::Executor* executor() noexcept { return core_ ? core_->active_executor() : nullptr; } [[nodiscard]] IO::Driver* io() noexcept { return core_ ? core_->get_io() : nullptr; } [[nodiscard]] std::optional owner_thread() const noexcept { return core_ ? core_->current_owner_thread() : std::nullopt; } /// Precondition: `valid()`. [[nodiscard]] const Options& options() const noexcept { assert(valid()); return core_->options; } /// Precondition: `valid()`. [[nodiscard]] const LifecyclePlan& lifecycle_plan() const noexcept { assert(valid()); return core_->plan; } /// Precondition: `valid()`. [[nodiscard]] const EmbeddedResources& embedded_resources() const noexcept { assert(valid()); return core_->embedded; } /// Precondition: `valid()`. [[nodiscard]] const Void::Time::MonotonicClock& clock() const noexcept { assert(valid()); return core_->active_clock(); } /// Precondition: `valid()`. [[nodiscard]] Void::Memory::BudgetResource& memory_budget() noexcept { assert(valid()); return core_->active_budget(); } [[nodiscard]] Void::Result::Result start(StartOptions opts = {}) { if (!core_) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_state, "Cannot start a moved-from Host")}; } return core_->start_impl(std::move(opts)); } [[nodiscard]] std::expected spawn( Coro::Task&& task, Async::OperationOptions opts = {}) { if (!core_) { return std::unexpected{Async::AdmissionError::stopped}; } return core_->spawn_impl(std::move(task), std::move(opts)); } void begin_shutdown() noexcept { if (core_) { core_->begin_shutdown_impl(); } } [[nodiscard]] Void::Result::Result poll_shutdown(Async::OperationOptions opts = {}) { if (!core_) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_state, "Cannot poll_shutdown on a moved-from Host")}; } return core_->poll_shutdown_impl(std::move(opts)); } [[nodiscard]] Void::Result::Result shutdown_blocking(Async::OperationOptions opts = {}) { if (!core_) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_state, "Cannot shutdown_blocking on a moved-from Host")}; } return core_->shutdown_blocking_impl(std::move(opts), false); } [[nodiscard]] Void::Result::Result shutdown_pumping( Async::ManualExecutor& manual, Async::OperationOptions opts = {}, Async::TimerQueue* timers = nullptr) { if (!core_) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_state, "Cannot shutdown_pumping on a moved-from Host")}; } return core_->shutdown_pumping_impl( manual, std::move(opts), timers, false); } [[nodiscard]] std::optional last_shutdown_report() const { return core_ ? core_->current_last_report() : std::nullopt; } private: friend class HostBuilder; Host(Options options, LifecyclePlan plan, EmbeddedResources embedded, std::unique_ptr owned_budget, std::vector> components) : core_(std::make_unique( std::move(options), std::move(plan), embedded, std::move(owned_budget), std::move(components))) { } std::unique_ptr core_{}; }; class HostBuilder { public: HostBuilder() : graph_(make_default_resource_graph(false)) { } HostBuilder& with_options(Options options) { options_ = std::move(options); if (!embedded_.has_value()) { graph_ = make_default_resource_graph(options_.embedded_mode); components_.clear(); } return *this; } HostBuilder& with_embedded(EmbeddedResources embedded) { options_.embedded_mode = true; embedded_ = embedded; graph_ = make_default_resource_graph(true); components_.clear(); return *this; } [[nodiscard]] Void::Result::Result add_resource(ResourceNode node) { return graph_.add_node(std::move(node)); } [[nodiscard]] Void::Result::Result add_dependency( std::string dependent, std::string dependency, bool required = true) { return graph_.add_dependency( std::move(dependent), std::move(dependency), required); } [[nodiscard]] Void::Result::Result add_component( std::string id, ResourceKind kind, std::shared_ptr component, std::vector deps = {}, bool critical = true) { if (id.empty() || !component) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_argument, "Component id must be non-empty and component pointer must be non-null")}; } if (auto nr = graph_.add_node( ResourceNode{id, kind, ResourceOwnership::owned, critical}); !nr.has_value()) { return nr; } for (auto& d : deps) { if (auto dr = graph_.add_dependency(id, std::move(d), true); !dr.has_value()) { return dr; } } components_.push_back(ComponentSpec{ std::move(id), kind, std::move(component), critical}); return {}; } [[nodiscard]] Void::Result::Result build() const { Options opts = options_; if (embedded_.has_value()) { opts.embedded_mode = true; if (auto eok = embedded_->validate(); !eok.has_value()) { return std::unexpected{std::move(eok.error())}; } } if (auto ok = validate_options(opts); !ok.has_value()) { return std::unexpected{std::move(ok.error())}; } auto plan = graph_.validate_and_plan(); if (!plan.has_value()) { return std::unexpected{std::move(plan.error())}; } const EmbeddedResources emb = embedded_.value_or(EmbeddedResources{}); std::unique_ptr budget; if (emb.memory_budget == nullptr) { budget = std::make_unique( opts.limits.max_owned_memory_bytes); } std::vector> entries; entries.reserve(components_.size()); for (const auto& c : components_) { auto entry = std::make_shared(); entry->id = c.id; entry->kind = c.kind; entry->critical = c.critical; entry->component = c.component; entries.push_back(std::move(entry)); } return Host{ std::move(opts), std::move(*plan), emb, std::move(budget), std::move(entries)}; } private: struct ComponentSpec { std::string id{}; ResourceKind kind{ResourceKind::service}; std::shared_ptr component{}; bool critical{true}; }; Options options_{}; std::optional embedded_{}; ResourceGraph graph_; std::vector components_{}; }; } // namespace Void::Runtime