From c6c0303ef81ef0019307bbead90b408f1659a5a5 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Thu, 3 Dec 2026 20:10:11 +0300 Subject: [PATCH] RPC: host adapter boundary for embedded and managed modes (S-RPC-05) --- include/Void/RPC/HostAdapter.hpp | 76 ++++++++++++++++++++++++++++ src/RPC/HostAdapter.cpp | 67 ++++++++++++++++++++++++ tests/standalone/RPC/HostAdapter.cpp | 58 +++++++++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 include/Void/RPC/HostAdapter.hpp create mode 100644 src/RPC/HostAdapter.cpp create mode 100644 tests/standalone/RPC/HostAdapter.cpp diff --git a/include/Void/RPC/HostAdapter.hpp b/include/Void/RPC/HostAdapter.hpp new file mode 100644 index 0000000..faf602f --- /dev/null +++ b/include/Void/RPC/HostAdapter.hpp @@ -0,0 +1,76 @@ +#pragma once + +#include +#include +#include +#include + +namespace Void::RPC +{ + /// Capacity bounds for a deferred host. + struct HostLimits { + std::size_t max_pending_tasks{1024}; + }; + + /// How an RPC host schedules work. + /// + /// Embedded hosts run work immediately on the posting thread. Managed + /// hosts only queue it and are driven by an external executor, so the + /// same RPC objects work in both modes without owning a thread. + class HostAdapter + { + public: + + HostAdapter() = default; + HostAdapter(const HostAdapter&) = delete; + HostAdapter& operator=(const HostAdapter&) = delete; + virtual ~HostAdapter() = default; + + /// Schedule a task. Returns false when the task is empty or the + /// adapter is at capacity (the caller applies backpressure). + [[nodiscard]] virtual bool post(std::function task) = 0; + + /// True when posted work runs without an external pump. + [[nodiscard]] virtual bool drains_automatically() const noexcept = 0; + }; + + /// Embedded host: runs each task inline on the posting thread. + class InlineHostAdapter final : public HostAdapter + { + public: + + [[nodiscard]] bool post(std::function task) override; + [[nodiscard]] bool drains_automatically() const noexcept override + { + return true; + } + }; + + /// Managed host: queues tasks until an external driver calls run_ready(). + class DeferredHostAdapter final : public HostAdapter + { + public: + + explicit DeferredHostAdapter(HostLimits limits = {}); + + [[nodiscard]] bool post(std::function task) override; + [[nodiscard]] bool drains_automatically() const noexcept override + { + return false; + } + + [[nodiscard]] std::size_t pending() const noexcept; + [[nodiscard]] std::size_t capacity() const noexcept; + + /// Run up to `budget` queued tasks (0 means drain everything that + /// is ready). Tasks posted by a running task join the same pump. + /// Returns the number of tasks run. + std::size_t run_ready(std::size_t budget = 0); + + private: + + mutable std::mutex m_mutex; + HostLimits m_limits; + std::deque> m_tasks; + }; +} // namespace Void::RPC diff --git a/src/RPC/HostAdapter.cpp b/src/RPC/HostAdapter.cpp new file mode 100644 index 0000000..f39be8f --- /dev/null +++ b/src/RPC/HostAdapter.cpp @@ -0,0 +1,67 @@ +#include + +#include + +namespace Void::RPC +{ + bool InlineHostAdapter::post(std::function task) + { + if (!task) { + return false; + } + task(); + return true; + } + + DeferredHostAdapter::DeferredHostAdapter(HostLimits limits) + : m_limits(limits) + { + } + + bool DeferredHostAdapter::post(std::function task) + { + if (!task) { + return false; + } + std::lock_guard lock{m_mutex}; + if (m_tasks.size() >= m_limits.max_pending_tasks) { + return false; + } + m_tasks.push_back(std::move(task)); + return true; + } + + std::size_t DeferredHostAdapter::pending() const noexcept + { + std::lock_guard lock{m_mutex}; + return m_tasks.size(); + } + + std::size_t DeferredHostAdapter::capacity() const noexcept + { + std::lock_guard lock{m_mutex}; + return m_limits.max_pending_tasks; + } + + std::size_t DeferredHostAdapter::run_ready(std::size_t budget) + { + std::size_t run = 0; + for (;;) { + if (budget != 0 && run >= budget) { + break; + } + std::function task; + { + std::lock_guard lock{m_mutex}; + if (m_tasks.empty()) { + break; + } + task = std::move(m_tasks.front()); + m_tasks.pop_front(); + } + ++run; + task(); + } + return run; + } +} // namespace Void::RPC diff --git a/tests/standalone/RPC/HostAdapter.cpp b/tests/standalone/RPC/HostAdapter.cpp new file mode 100644 index 0000000..1293773 --- /dev/null +++ b/tests/standalone/RPC/HostAdapter.cpp @@ -0,0 +1,58 @@ +#include +#include +#include + +#include + +namespace +{ + using namespace Void::RPC; +} // namespace + +Void::Testing::Test rpc_inline_host_runs_work_immediately() +{ + InlineHostAdapter host; + int ran = 0; + Void::Testing::expect(host.post([&ran] { ++ran; })); + Void::Testing::expect_eq(ran, 1); + Void::Testing::expect(host.drains_automatically()); + Void::Testing::expect(!host.post({})); +} + +Void::Testing::Test rpc_deferred_host_is_bounded_and_externally_driven() +{ + DeferredHostAdapter host{HostLimits{.max_pending_tasks = 2}}; + int ran = 0; + Void::Testing::expect(host.post([&ran] { ++ran; })); + Void::Testing::expect(host.post([&ran] { ++ran; })); + // At capacity: the caller must apply backpressure. + Void::Testing::expect(!host.post([&ran] { ++ran; })); + Void::Testing::expect(!host.post({})); + Void::Testing::expect_eq(host.pending(), std::size_t{2}); + Void::Testing::expect_eq(host.capacity(), std::size_t{2}); + Void::Testing::expect(!host.drains_automatically()); + Void::Testing::expect_eq(ran, 0); + + // Budgeted pump. + Void::Testing::expect_eq(host.run_ready(1), std::size_t{1}); + Void::Testing::expect_eq(ran, 1); + Void::Testing::expect_eq(host.pending(), std::size_t{1}); + + // Zero budget drains everything that is ready. + Void::Testing::expect_eq(host.run_ready(0), std::size_t{1}); + Void::Testing::expect_eq(ran, 2); + Void::Testing::expect_eq(host.pending(), std::size_t{0}); +} + +Void::Testing::Test rpc_deferred_host_runs_nested_posts_in_same_pump() +{ + DeferredHostAdapter host; + int ran = 0; + Void::Testing::expect(host.post([&] { + ++ran; + Void::Testing::expect(host.post([&] { ++ran; })); + })); + Void::Testing::expect_eq(host.run_ready(0), std::size_t{2}); + Void::Testing::expect_eq(ran, 2); + Void::Testing::expect_eq(host.pending(), std::size_t{0}); +}