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:
faumarayandClaude Fable 5 committed 2026-06-14 00:39:05 +03:00
1 parent 04cf1998bd
commit cfeaf93a53
14 files changed
+1744

No files matched your search

+121
View File
@@ -0,0 +1,121 @@
#include <Void/Fsm/Detail/Engine.hpp>
#include <utility>
namespace Void::Fsm::Detail
{
ExecutionContext::~ExecutionContext() { stop(); }
void ExecutionContext::post(EventEnvelope env)
{
m_queue.push(std::move(env));
wake();
}
bool ExecutionContext::try_take(const std::type_info& type, std::any& out)
{
const auto it = m_buffers.find(std::type_index{type});
if (it == m_buffers.end() || it->second.empty()) {
return false;
}
out = std::move(it->second.front());
it->second.pop_front();
return true;
}
void ExecutionContext::set_waiter(const std::type_info& type,
std::coroutine_handle<>
cont,
std::any* slot)
{
m_waiter.type = &type;
m_waiter.cont = cont;
m_waiter.slot = slot;
}
void ExecutionContext::deliver(EventEnvelope& env)
{
auto& buffer = m_buffers[std::type_index{*env.type}];
buffer.push_back(std::move(env.value));
// Resume a matching waiter, if any.
if (m_waiter.type != nullptr && *m_waiter.type == *env.type &&
!buffer.empty()) {
*m_waiter.slot = std::move(buffer.front());
buffer.pop_front();
auto cont = m_waiter.cont;
m_waiter = Waiter{};
m_executor.post(cont);
}
}
std::size_t ExecutionContext::pump()
{
std::size_t work = 0;
while (auto env = m_queue.try_pop()) {
deliver(*env);
++work;
}
work += m_executor.drain();
return work;
}
void ExecutionContext::wake() noexcept
{
if (m_sleeping.exchange(false, std::memory_order_acq_rel)) {
m_wakeup.release();
}
}
void ExecutionContext::start_worker()
{
if (m_worker_started.load(std::memory_order_acquire)) {
return;
}
std::call_once(m_worker_once, [this] {
m_worker = std::thread([this] { run_loop(); });
m_worker_started.store(true, std::memory_order_release);
});
}
void ExecutionContext::finish()
{
m_done.store(true, std::memory_order_release);
wake();
}
void ExecutionContext::stop()
{
m_done.store(true, std::memory_order_release);
wake();
if (m_worker.joinable()) {
m_worker.join();
}
}
void ExecutionContext::join()
{
if (m_worker.joinable()) {
m_worker.join();
}
}
void ExecutionContext::run_loop()
{
for (;;) {
pump();
if (m_done.load(std::memory_order_acquire)) {
return;
}
m_sleeping.store(true, std::memory_order_release);
if (!m_queue.empty() || m_done.load(std::memory_order_acquire)) {
m_sleeping.store(false, std::memory_order_release);
continue;
}
m_wakeup.acquire();
m_sleeping.store(false, std::memory_order_release);
}
}
} // namespace Void::Fsm::Detail