Add Service registry and rewrite Fsm as an inheritance/coroutine workflow
Service module (Void::Service):
- IService base (service_name + on_start/on_stop lifecycle).
- ServiceRegistry: type-keyed container — provide<I>(impl), get<I>()
(nullptr if absent), require<I>() (throws), has<I>(), start_all/
stop_all. Consumers depend only on the interface I and resolve at
runtime (dependency inversion); a concrete that derives IService is
recovered for lifecycle via dynamic_cast even when registered under a
plain interface. shared_mutex; reads are the hot path.
New Void::Fsm — a single inheritance + coroutine workflow FSM replacing
the deleted statechart/compile-time FSMs:
- Derive Machine<ResultT>; declare states as nested structs deriving
State; each implements Coro::Task<Step> run() returning next<S>() /
succeed(...) / fail(...).
- Async by default: a non-template Detail::ExecutionContext owns one
ManualExecutor + worker thread + a lock-free MPSC event queue + a typed
event mailbox; a single consumer touches all mutable state so state
code is lock-free/single-threaded. Embedder mode (own_worker=false)
drives via pump() for deterministic tests.
- Events: co_await await_event<E>() consumes the oldest queued E (typed
mailbox), suspending until one arrives; post<E>(e) is lock-free from
any thread.
- Nesting: co_await run_child<Sub>(args...) runs a child on the same
executor (shared events/services) and yields its Outcome.
- Dynamic: add<S>() / add_if<S>(cond) register states; next<unregistered>
fails; has_state<S>() lets states branch.
- Services: set_services(registry); state.service<I>() resolves through
the injected context.
- Observation: current_state() is a lock-free atomic-index read of an
immutable name table (P2996 identifier_of, fallback to indexed names).
- Persistence: serialize() captures {state name, on_serialize data};
restore() on an un-started machine resumes at the named state.
26 new tests (Service registry; FSM basic/success/fail, events incl.
cross-thread worker + order, nesting incl. failure propagation,
serialize/restore, dynamic registration, service resolution, and a
6-producer x 5k concurrent-post stress + lock-free current_state reader
check). 960 green, ASan+UBSan clean, stable across 10 runs, -Werror clean.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
04cf1998bd
commit
cfeaf93a53
14 files changed
+1744
No files matched your search
@@ -0,0 +1,127 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Service/Service.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <shared_mutex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <typeindex>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Service
|
||||
{
|
||||
/// Type-keyed service container: providers register an implementation
|
||||
/// against an interface type, consumers resolve by that interface and
|
||||
/// never name the concrete type — the core of the dependency-inversion
|
||||
/// the orchestrator and FSMs rely on.
|
||||
///
|
||||
/// registry.provide<IClock>(std::make_shared<SystemClock>());
|
||||
/// auto& clock = registry.require<IClock>(); // no SystemClock dep
|
||||
///
|
||||
/// Thread-safe. Reads (get/require/has) take a shared lock; they are
|
||||
/// the hot path for running FSMs resolving services per step.
|
||||
class ServiceRegistry
|
||||
{
|
||||
public:
|
||||
|
||||
ServiceRegistry() = default;
|
||||
ServiceRegistry(const ServiceRegistry&) = delete;
|
||||
ServiceRegistry& operator=(const ServiceRegistry&) = delete;
|
||||
|
||||
/// Register (or replace) the implementation for interface I.
|
||||
/// If I derives IService, the instance joins the lifecycle.
|
||||
template <typename I> void provide(std::shared_ptr<I> impl)
|
||||
{
|
||||
Entry entry;
|
||||
entry.impl = std::shared_ptr<void>(impl, impl.get());
|
||||
if constexpr (std::is_base_of_v<IService, I>) {
|
||||
entry.service = static_cast<IService*>(impl.get());
|
||||
} else {
|
||||
// The interface I doesn't derive IService, but the
|
||||
// concrete implementation might — recover the lifecycle
|
||||
// aspect by cross-cast.
|
||||
entry.service = dynamic_cast<IService*>(impl.get());
|
||||
}
|
||||
const std::type_index key{typeid(I)};
|
||||
std::unique_lock lock{m_mutex};
|
||||
if (!m_services.contains(key)) {
|
||||
m_order.push_back(key);
|
||||
}
|
||||
m_services.insert_or_assign(key, std::move(entry));
|
||||
}
|
||||
|
||||
/// Resolve I, or nullptr if not provided.
|
||||
template <typename I> [[nodiscard]] std::shared_ptr<I> get() const
|
||||
{
|
||||
std::shared_lock lock{m_mutex};
|
||||
const auto it = m_services.find(std::type_index{typeid(I)});
|
||||
if (it == m_services.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return std::static_pointer_cast<I>(it->second.impl);
|
||||
}
|
||||
|
||||
/// Resolve I; throws std::runtime_error if not provided.
|
||||
template <typename I> [[nodiscard]] I& require() const
|
||||
{
|
||||
auto p = get<I>();
|
||||
if (!p) {
|
||||
throw std::runtime_error(
|
||||
std::string{"ServiceRegistry: no service for '"} +
|
||||
typeid(I).name() + "'");
|
||||
}
|
||||
return *p;
|
||||
}
|
||||
|
||||
template <typename I> [[nodiscard]] bool has() const
|
||||
{
|
||||
std::shared_lock lock{m_mutex};
|
||||
return m_services.contains(std::type_index{typeid(I)});
|
||||
}
|
||||
|
||||
/// Call on_start() on every lifecycle service, registration
|
||||
/// order.
|
||||
void start_all()
|
||||
{
|
||||
std::shared_lock lock{m_mutex};
|
||||
for (const auto& key : m_order) {
|
||||
if (auto* s = m_services.at(key).service) {
|
||||
s->on_start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Call on_stop() on every lifecycle service, reverse order.
|
||||
void stop_all()
|
||||
{
|
||||
std::shared_lock lock{m_mutex};
|
||||
for (auto it = m_order.rbegin(); it != m_order.rend(); ++it) {
|
||||
if (auto* s = m_services.at(*it).service) {
|
||||
s->on_stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t size() const
|
||||
{
|
||||
std::shared_lock lock{m_mutex};
|
||||
return m_services.size();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
struct Entry {
|
||||
std::shared_ptr<void> impl;
|
||||
IService* service = nullptr; // non-null iff I : IService
|
||||
};
|
||||
|
||||
mutable std::shared_mutex m_mutex;
|
||||
std::unordered_map<std::type_index, Entry> m_services;
|
||||
std::vector<std::type_index> m_order;
|
||||
};
|
||||
|
||||
} // namespace Void::Service
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
namespace Void::Service
|
||||
{
|
||||
/// Base for services resolved through a ServiceRegistry by interface
|
||||
/// type. Consumers depend only on an interface (which may but need not
|
||||
/// derive IService); implementing IService additionally opts the
|
||||
/// service into registry-driven lifecycle (start_all/stop_all).
|
||||
class IService
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~IService() = default;
|
||||
|
||||
[[nodiscard]] virtual std::string_view service_name() const = 0;
|
||||
|
||||
/// Called by ServiceRegistry::start_all(), in registration
|
||||
/// order.
|
||||
virtual void on_start() {}
|
||||
|
||||
/// Called by ServiceRegistry::stop_all(), in reverse order.
|
||||
virtual void on_stop() {}
|
||||
};
|
||||
|
||||
} // namespace Void::Service
|
||||
Reference in new issue
Block a user