FSM (3): direct control — fluent events, free jumps, lifecycle, mutation

Adds imperative ergonomics for driving and reshaping a machine. The engine
isn't preemptible mid-state (coroutines aren't interruptible), so external
controls apply at transition boundaries and mutation is Idle-only — both
documented.

- Fluent events: send(e1, e2, …) variadic post.
- Free runtime jumps: in-state goto_state<S>() reaches any registered state
  with no declared transition ("each state can be moved to another state");
  external jump<S>() requests a redirect applied by drive() at the next
  boundary (race-safe via an atomic, non-preemptive).
- Lifecycle: reset() returns a finished/idle machine to Idle (drops the
  owned ctx, clears outcome/current/jump); restart() / restart_at<S>()
  re-run; pause() / resume() (ExecutionContext gains a paused flag — the
  worker stops draining and pump() no-ops while paused; posted events
  buffer until resume()).
- Runtime mutation (Idle only): remove<S>() (re-packs indices,
  fixes the initial), set_initial<S>(), replace<S, T>().

8 new tests (Direct): send batch, goto_state skip, boundary jump (embedder
mode), restart / restart_at, remove + set_initial, replace, pause holds
events until resume. 995 green, ASan+UBSan clean, 0 flaky in 15 runs,
-Werror clean. Completes the FSM refactor plan (1-3).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-06-14 15:44:19 +03:00
1 parent b34226369a
commit 7c7c590d79
5 files changed
+458 -4

No files matched your search

+20 -4
View File
@@ -136,8 +136,17 @@ namespace Void::Fsm::Detail
m_buffers[std::type_index{*env.type}].push_back(std::move(env.value));
}
void ExecutionContext::set_paused(bool paused)
{
m_paused.store(paused, std::memory_order_release);
wake();
}
std::size_t ExecutionContext::pump()
{
if (m_paused.load(std::memory_order_acquire)) {
return 0;
}
std::size_t work = 0;
fire_due_timers();
while (auto env = m_queue.try_pop()) {
@@ -192,17 +201,24 @@ namespace Void::Fsm::Detail
void ExecutionContext::run_loop()
{
for (;;) {
pump();
const bool paused = m_paused.load(std::memory_order_acquire);
if (!paused) {
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)) {
// Queued events only count as wakeable work when not paused.
if ((!paused && !m_queue.empty()) ||
m_done.load(std::memory_order_acquire)) {
m_sleeping.store(false, std::memory_order_release);
continue;
}
// Park until woken, or until the earliest timer is due.
if (const auto deadline = next_deadline(); deadline.has_value()) {
// Park until woken (post/resume/finish), or — when running — until
// the earliest timer is due. Paused: no timers, wait to resume.
if (const auto deadline = next_deadline();
!paused && deadline.has_value()) {
(void)m_wakeup.try_acquire_until(*deadline);
} else {
m_wakeup.acquire();