RPC: host adapter boundary for embedded and managed modes (S-RPC-05)

This commit is contained in:
faumaray committed 2026-12-03 20:10:11 +03:00
1 parent 1f92dd054e
commit c6c0303ef8
3 files changed
+201

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
#pragma once
#include <cstddef>
#include <deque>
#include <functional>
#include <mutex>
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<void()> 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<void()> 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<void()> 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<std::function<void()>> m_tasks;
};
} // namespace Void::RPC
+67
View File
@@ -0,0 +1,67 @@
#include <Void/RPC/HostAdapter.hpp>
#include <utility>
namespace Void::RPC
{
bool InlineHostAdapter::post(std::function<void()> task)
{
if (!task) {
return false;
}
task();
return true;
}
DeferredHostAdapter::DeferredHostAdapter(HostLimits limits)
: m_limits(limits)
{
}
bool DeferredHostAdapter::post(std::function<void()> 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<void()> 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
+58
View File
@@ -0,0 +1,58 @@
#include <Void/RPC/HostAdapter.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cstddef>
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});
}