Definition (Builder -> immutable shared MachineDef): - Named hierarchical states, events by name or enum identifier (P2996), states_from_enum, initial/parallel composites, shallow/deep history, deferred events, per-state timeouts, guards, entry/exit/transition actions (coroutine Task<void> or sync, wrapped), internal transitions. - build() validates (initial children, transition endpoints) and flattens into index-addressed vectors with depth info for LCA. Machine (one consumer, many producers): - post() is lock-free from any thread: Vyukov MPSC push + counting- semaphore park/wake; the single consumer (owned worker, or embedder via run_pending) owns all mutable machine state with zero locks. - Observation via seqlock over a trivially-copyable StateSnapshot of active leaves; readers retry, writer is wait-free, no heap. - Run-to-completion: innermost-first transition resolution per region, LCA-based exit/entry ordering, history snapshotted at exit time, deferred events re-injected FIFO after each state change, timers via deadline park (generation counters kill stale fires), orthogonal regions dispatched independently on the single consumer. - co_await machine.send(ev) / send_wait() resolve with SendResult after the step; actions are driven to completion on the consumer's ManualExecutor before the step ends. - swap_definition: atomic<shared_ptr> definition swap between steps; identical active-leaf names adopt seamlessly (no entry re-runs, queue intact), otherwise initial configuration; history remapped by name. - serialize_state()/restore_state(): JSON by state *names* for hot-reload and persistence; restore re-activates silently. - start() returns only once the initial configuration is entered. 40 new tests: semantics (ordering, guards, internal/self transitions), hierarchy + both history kinds, defer FIFO, parallel regions, timers, coroutine actions, awaitable/blocking send, serialize/restore round trips, seamless + fallback hot swap, and stress: 8 producers x 10k with per-producer FIFO assertion, 4-reader seqlock tear check, 50 definition swaps under sustained load. Green under ASan+UBSan, stable across repeated runs. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
43 lines
1.2 KiB
C++
43 lines
1.2 KiB
C++
#pragma once
|
|
|
|
#include <any>
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
namespace Void::Fsm
|
|
{
|
|
/// Interned identifiers. Both are assigned by the Builder; names are
|
|
/// the stable identity (they survive definition swaps and serialize
|
|
/// into hot-reload state), ids are the fast runtime currency.
|
|
using StateId = std::uint32_t;
|
|
using EventId = std::uint32_t;
|
|
|
|
inline constexpr StateId kInvalidState = ~StateId{0};
|
|
inline constexpr EventId kInvalidEvent = ~EventId{0};
|
|
|
|
/// The root pseudo-state always has id 0.
|
|
inline constexpr StateId kRootState = 0;
|
|
|
|
/// A type-erased event instance. The payload is optional; guards and
|
|
/// actions retrieve it with std::any_cast against the type they
|
|
/// expect.
|
|
struct Event {
|
|
EventId id{kInvalidEvent};
|
|
std::any payload{};
|
|
};
|
|
|
|
/// Outcome of one run-to-completion step for a posted event.
|
|
enum class SendResult : std::uint8_t {
|
|
Handled, // at least one transition fired
|
|
Deferred, // stashed by an active state's defer list
|
|
Unhandled, // no active state had a matching transition
|
|
Dropped // machine stopped before processing
|
|
};
|
|
|
|
struct FsmError {
|
|
std::string message;
|
|
};
|
|
|
|
} // namespace Void::Fsm
|