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