Remove the two old FSMs ahead of the inheritance-based rewrite
Deletes the compile-time Void::StateMachine and the builder-based runtime Void::Fsm (Definition/Machine/Event/PluginBehavior + their standalone tests, the FSM behaviour plugins, and the FSM x Plugin integration test and its CMake wiring). The general-purpose Plugin module and its stateful hot-reload tests are untouched. Both are being replaced by a single inheritance + coroutine workflow FSM (next phase). 936 tests green (51 old FSM/StateMachine tests removed). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6b5f61f00a
commit
04cf1998bd
20 files changed
+3
-3793
No files matched your search
@@ -1,273 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/MpscQueue.hpp>
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/Data/Reflect.hpp>
|
||||
#include <Void/Fsm/Definition.hpp>
|
||||
#include <Void/Fsm/Event.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <coroutine>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <semaphore>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Fsm
|
||||
{
|
||||
/// Lock-free observable snapshot of the active configuration. Holds
|
||||
/// the active *leaf* states (compositions are implied by ancestry);
|
||||
/// fixed-size so the seqlock copy is trivial.
|
||||
struct StateSnapshot {
|
||||
static constexpr std::size_t kMaxLeaves = 16;
|
||||
|
||||
std::array<StateId, kMaxLeaves> leaves{};
|
||||
std::uint32_t leaf_count{0};
|
||||
std::uint64_t step{0}; // run-to-completion step counter
|
||||
bool running{false};
|
||||
|
||||
[[nodiscard]] bool contains(StateId id) const noexcept
|
||||
{
|
||||
for (std::uint32_t i = 0; i < leaf_count; ++i) {
|
||||
if (leaves[i] == id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// ── Machine ───────────────────────────────────────────────────────────
|
||||
// Runtime engine driving one MachineDef.
|
||||
//
|
||||
// Concurrency model (MPSC): any thread may post()/send(); exactly one
|
||||
// consumer — the owned worker thread (default) or the embedding thread
|
||||
// via run_one()/run_pending() — executes run-to-completion steps. All
|
||||
// mutable machine state is owned by that consumer; observers read a
|
||||
// seqlock-protected snapshot.
|
||||
//
|
||||
// Hot path costs: post() = wait-free queue push (+ a semaphore release
|
||||
// only when the worker parked); current()/is_in() = seqlock read,
|
||||
// retry on contention; no mutexes anywhere on these paths.
|
||||
struct MachineOptions {
|
||||
/// Spawn a worker thread in start(). When false the embedder
|
||||
/// drives the machine with run_pending().
|
||||
bool own_worker = true;
|
||||
};
|
||||
|
||||
class Machine
|
||||
{
|
||||
public:
|
||||
|
||||
using Options = MachineOptions;
|
||||
|
||||
explicit Machine(std::shared_ptr<const MachineDef> def,
|
||||
Options options = {});
|
||||
|
||||
/// Stops the machine (drains nothing; pending events are
|
||||
/// dropped) and joins the worker.
|
||||
~Machine();
|
||||
|
||||
Machine(const Machine&) = delete;
|
||||
Machine& operator=(const Machine&) = delete;
|
||||
Machine(Machine&&) = delete;
|
||||
Machine& operator=(Machine&&) = delete;
|
||||
|
||||
// ── lifecycle ─────────────────────────────────────────────────
|
||||
|
||||
/// Enter the initial configuration (entry actions run on the
|
||||
/// consumer). With own_worker the worker starts here.
|
||||
void start();
|
||||
|
||||
/// Process queued events, then stop. Joins the worker.
|
||||
void stop();
|
||||
|
||||
[[nodiscard]] bool running() const noexcept;
|
||||
|
||||
// ── event submission (any thread) ─────────────────────────────
|
||||
|
||||
/// Fire-and-forget. Lock-free. Returns false if the machine
|
||||
/// is not accepting events (not started or stopped).
|
||||
bool post(Event event);
|
||||
|
||||
template <typename E>
|
||||
requires std::is_enum_v<E>
|
||||
bool post(E e, std::any payload = {})
|
||||
{
|
||||
const auto def = definition();
|
||||
const auto id = def->event_id(e);
|
||||
if (!id.has_value()) {
|
||||
return false;
|
||||
}
|
||||
return post(Event{*id, std::move(payload)});
|
||||
}
|
||||
|
||||
/// Awaitable submission: co_await machine.send(ev) resumes
|
||||
/// after the event's run-to-completion step with its result.
|
||||
/// Resumption runs on the machine's consumer thread unless a
|
||||
/// resume executor is given.
|
||||
class SendAwaitable
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]] bool await_ready() const noexcept
|
||||
{
|
||||
return m_result.has_value();
|
||||
}
|
||||
|
||||
void await_suspend(std::coroutine_handle<> h);
|
||||
|
||||
[[nodiscard]] SendResult await_resume() const noexcept
|
||||
{
|
||||
return m_result.value_or(SendResult::Dropped);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
friend class Machine;
|
||||
SendAwaitable(Machine& machine, Event event,
|
||||
Async::Executor* resume_on)
|
||||
: m_machine(&machine), m_event(std::move(event)),
|
||||
m_resume_on(resume_on)
|
||||
{
|
||||
}
|
||||
|
||||
Machine* m_machine;
|
||||
Event m_event;
|
||||
Async::Executor* m_resume_on;
|
||||
std::optional<SendResult> m_result;
|
||||
};
|
||||
|
||||
[[nodiscard]] SendAwaitable
|
||||
send(Event event, Async::Executor* resume_on = nullptr)
|
||||
{
|
||||
return SendAwaitable{*this, std::move(event), resume_on};
|
||||
}
|
||||
|
||||
/// Blocking submission: returns once the event's step ran.
|
||||
SendResult send_wait(Event event);
|
||||
|
||||
// ── observation (any thread, lock-free) ───────────────────────
|
||||
|
||||
[[nodiscard]] StateSnapshot current() const noexcept;
|
||||
|
||||
/// True if `state` (leaf or composite) is in the active
|
||||
/// configuration.
|
||||
[[nodiscard]] bool is_in(StateId state) const;
|
||||
[[nodiscard]] bool is_in(std::string_view state) const;
|
||||
|
||||
[[nodiscard]] std::shared_ptr<const MachineDef> definition() const
|
||||
{
|
||||
return m_def.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
// ── hot swap / persistence ────────────────────────────────────
|
||||
|
||||
/// Replace the definition between run-to-completion steps.
|
||||
/// Active states are re-mapped by name; the queue keeps
|
||||
/// flowing. States whose names vanished fall back to the new
|
||||
/// definition's initial configuration for that region.
|
||||
void swap_definition(std::shared_ptr<const MachineDef> def);
|
||||
|
||||
/// Serialize the active configuration + history (by state
|
||||
/// name) for hot-reload / persistence. Processed as a control
|
||||
/// step on the consumer; safe from any thread while running.
|
||||
[[nodiscard]] Json::Value serialize_state();
|
||||
|
||||
/// Restore a configuration produced by serialize_state()
|
||||
/// (optionally migrated). Must be called before start().
|
||||
[[nodiscard]] std::expected<void, FsmError>
|
||||
restore_state(const Json::Value& state);
|
||||
|
||||
// ── embedder-driven mode (own_worker = false) ─────────────────
|
||||
|
||||
/// Process queued events/control work; returns the number of
|
||||
/// envelopes handled. Single consumer only.
|
||||
std::size_t run_pending();
|
||||
|
||||
/// Block the calling thread (the consumer) until `until`
|
||||
/// returns true, processing events as they arrive. Intended
|
||||
/// for tests and simple embedders.
|
||||
void run_until(const std::function<bool()>& until);
|
||||
|
||||
private:
|
||||
|
||||
struct Envelope; // Event + completion plumbing
|
||||
struct HistoryRecord {
|
||||
std::vector<StateId> leaves; // deep: leaves; shallow: child
|
||||
};
|
||||
|
||||
// consumer-side core
|
||||
void consumer_loop();
|
||||
std::size_t drain_once();
|
||||
bool handle_envelope(Envelope& env);
|
||||
SendResult dispatch_event(const Event& event);
|
||||
void enter_initial_configuration();
|
||||
void perform_transition(const MachineDef::TransitionDef& t,
|
||||
StateId source_leaf,
|
||||
const Event& event);
|
||||
void enter_target(StateId lca, StateId target, const Event& event);
|
||||
void descend_into(StateId state, const Event& event);
|
||||
void exit_to_lca(StateId leaf, StateId lca, const Event& event);
|
||||
void exit_single(StateId state, const Event& event);
|
||||
void enter_single(StateId state, const Event& event);
|
||||
void run_action(const Action& action, const Event& event);
|
||||
void publish_snapshot();
|
||||
void rearm_timers();
|
||||
void fire_due_timers();
|
||||
void reinject_deferred();
|
||||
[[nodiscard]] std::optional<std::chrono::steady_clock::time_point>
|
||||
next_deadline() const;
|
||||
void apply_swap(std::shared_ptr<const MachineDef> next);
|
||||
std::vector<std::string> active_leaf_names() const;
|
||||
|
||||
void wake_consumer() noexcept;
|
||||
|
||||
// immutable-ish shared state
|
||||
std::atomic<std::shared_ptr<const MachineDef>> m_def;
|
||||
Options m_options;
|
||||
|
||||
// MPSC queue + parking
|
||||
Async::MpscQueue<Envelope*> m_queue;
|
||||
std::counting_semaphore<> m_wakeup{0};
|
||||
std::atomic<bool> m_sleeping{false};
|
||||
std::atomic<bool> m_accepting{false};
|
||||
std::atomic<bool> m_stop{false};
|
||||
std::thread m_worker;
|
||||
|
||||
// seqlock snapshot
|
||||
mutable std::atomic<std::uint64_t> m_seq{0};
|
||||
StateSnapshot m_snapshot{};
|
||||
|
||||
// ── consumer-owned state (no synchronization) ─────────────────
|
||||
std::shared_ptr<const MachineDef> m_active_def;
|
||||
std::vector<bool> m_active; // by StateId
|
||||
std::vector<StateId> m_leaves; // active leaves, region order
|
||||
std::unordered_map<StateId, HistoryRecord> m_history;
|
||||
std::vector<Envelope*> m_deferred; // stashed events
|
||||
bool m_deferred_dirty{false}; // re-inject after change
|
||||
std::uint64_t m_step{0};
|
||||
Async::ManualExecutor m_executor; // drives coroutine actions
|
||||
|
||||
struct TimerArm {
|
||||
StateId state;
|
||||
std::chrono::steady_clock::time_point deadline;
|
||||
EventId fires;
|
||||
std::uint64_t generation;
|
||||
};
|
||||
std::vector<TimerArm> m_timers;
|
||||
std::vector<std::uint64_t> m_activation; // per state
|
||||
|
||||
// configuration captured by restore_state(), applied silently
|
||||
// (no entry actions) when start() runs
|
||||
std::optional<Json::Value> m_pending_restore;
|
||||
};
|
||||
|
||||
} // namespace Void::Fsm
|
||||
Reference in new issue
Block a user