#pragma once #include #include #include #include #include #include #include #include #include #include namespace Void::Runtime { inline constexpr std::uint32_t kRuntimeErrorDomain = 11; enum class RuntimeErrorCode : std::int32_t { none = 0, invalid_argument = 1, not_found = 2, conflict = 3, cycle_detected = 4, invalid_state = 5, unsupported = 6, internal = 7 }; [[nodiscard]] inline Void::Result::Error make_runtime_error( RuntimeErrorCode code, std::string message = {}) { using Void::Result::Error; using Void::Result::ErrorCode; using Void::Result::ErrorKind; ErrorKind kind = ErrorKind::internal; switch (code) { case RuntimeErrorCode::none: kind = ErrorKind::internal; break; case RuntimeErrorCode::invalid_argument: kind = ErrorKind::invalid_argument; break; case RuntimeErrorCode::not_found: kind = ErrorKind::not_found; break; case RuntimeErrorCode::conflict: case RuntimeErrorCode::cycle_detected: case RuntimeErrorCode::invalid_state: kind = ErrorKind::conflict; break; case RuntimeErrorCode::unsupported: kind = ErrorKind::unsupported; break; case RuntimeErrorCode::internal: kind = ErrorKind::internal; break; } return Error{ kind, std::move(message), ErrorCode{kRuntimeErrorDomain, static_cast(code)}}; } struct RuntimeLimits { std::size_t max_cpu_tasks{4096}; std::size_t max_io_operations{8192}; std::size_t max_file_queue{256}; std::size_t max_timers{1024}; std::size_t max_services{256}; std::size_t max_owned_memory_bytes{256ULL * 1024ULL * 1024ULL}; }; struct ShutdownPolicy { std::chrono::milliseconds drain_grace{5000}; std::chrono::milliseconds export_flush_timeout{5000}; std::chrono::milliseconds total_timeout{30000}; }; [[nodiscard]] inline std::uint32_t default_cpu_workers() noexcept { const std::uint32_t hw = std::thread::hardware_concurrency(); return std::clamp(hw, 1u, 8u); } struct Options { std::uint32_t cpu_workers{default_cpu_workers()}; std::uint32_t file_workers{4}; bool embedded_mode{false}; RuntimeLimits limits{}; ShutdownPolicy shutdown{}; IO::BackendOptions io{}; }; [[nodiscard]] inline Void::Result::Result validate_options(const Options& opts) { if (!opts.embedded_mode) { if (opts.cpu_workers == 0) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_argument, "Runtime Options::cpu_workers must be >= 1 in managed mode")}; } if (opts.file_workers == 0) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_argument, "Runtime Options::file_workers must be >= 1 in managed mode")}; } } const auto& l = opts.limits; if (l.max_cpu_tasks == 0 || l.max_io_operations == 0 || l.max_file_queue == 0 || l.max_timers == 0 || l.max_services == 0 || l.max_owned_memory_bytes == 0) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_argument, "All RuntimeLimits capacity bounds must be non-zero")}; } const auto& s = opts.shutdown; if (s.drain_grace.count() <= 0 || s.export_flush_timeout.count() <= 0 || s.total_timeout.count() <= 0) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_argument, "ShutdownPolicy durations must be strictly positive")}; } if (s.drain_grace > s.total_timeout || s.export_flush_timeout > s.total_timeout) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_argument, "ShutdownPolicy drain_grace and export_flush_timeout must not exceed total_timeout")}; } if (auto io_ok = IO::validate_limits(opts.io.limits); !io_ok.has_value()) { return std::unexpected{std::move(io_ok.error())}; } // Submodule IO caps cannot exceed global host caps. if (opts.io.limits.max_operations > l.max_io_operations) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_argument, "IO::IoLimits::max_operations exceeds RuntimeLimits::max_io_operations")}; } if (opts.io.limits.max_timers > l.max_timers && opts.io.limits.max_timers != 4096) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_argument, "IO::IoLimits::max_timers exceeds RuntimeLimits::max_timers")}; } if (opts.io.limits.max_file_queue > l.max_file_queue) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_argument, "IO::IoLimits::max_file_queue exceeds RuntimeLimits::max_file_queue")}; } if (opts.io.limits.max_owned_buffer_bytes > l.max_owned_memory_bytes) { return std::unexpected{make_runtime_error( RuntimeErrorCode::invalid_argument, "IO::IoLimits::max_owned_buffer_bytes exceeds RuntimeLimits::max_owned_memory_bytes")}; } return {}; } } // namespace Void::Runtime