FSM v2 (B): timers, await_event_for, reactive/declarative states + guards

Engine: ExecutionContext gains a timer set (add_timer/cancel_timer); the
worker parks with try_acquire_until(earliest deadline) instead of a bare
acquire, and fires due timers in pump()/run_loop. The event mailbox waiter
is now multi-type (await any of N event types) and carries an optional
timeout — deliver() matches a waiting state directly (event needn't sit in
a buffer) and cancels the paired timer; the timer's fire resumes the
waiter with timed_out set. Single consumer still owns all mailbox/timer
state, so it stays lock-free for state code.

Machine:
- await_event_for<E>(duration) -> std::optional<E> (nullopt on timeout);
  await_any_event(types, deadline) for reactive states; existing
  await_event<E> rebuilt on the multi-type API.
- Reactive state base: instead of run(), a state overrides
  transitions(TransitionBuilder&) declaratively —
  on.event<E>().guard(fn).action(fn).to<S>() / .finish(v) / .fail(msg),
  and on.after(d).to<S>()/.fail(). The framework-provided run() builds the
  table once and loops: await any declared event or the timeout, evaluate
  matching entries in declaration order, check guard, run action,
  transition. Guards = bool(Derived&, const E&), actions sync or
  Coro::Task<void>. to<S>() static-asserts S in States<>. Reactive and
  imperative State subclasses mix freely in one machine.

8 new tests (Reactive): guarded reactive state + action ordering mixed
with an imperative terminal; await_event_for timeout vs event; reactive
after().to<> firing vs event beating the deadline. 973 green, ASan+UBSan
clean, timer tests stable across 12 runs, -Werror clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-06-14 01:11:53 +03:00
1 parent e941a48007
commit 422a7705c5
4 files changed
+682 -32

No files matched your search

+99 -21
View File
@@ -21,51 +21,124 @@ namespace Void::Fsm::Detail
wake();
}
bool ExecutionContext::try_take(const std::type_info& type, std::any& out)
bool ExecutionContext::try_take_any(
std::span<const std::type_info* const> types, EventEnvelope& out)
{
const auto it = m_buffers.find(std::type_index{type});
if (it == m_buffers.end() || it->second.empty()) {
return false;
for (const auto* ty : types) {
const auto it = m_buffers.find(std::type_index{*ty});
if (it != m_buffers.end() && !it->second.empty()) {
out.type = ty;
out.value = std::move(it->second.front());
it->second.pop_front();
return true;
}
}
out = std::move(it->second.front());
it->second.pop_front();
return true;
return false;
}
void ExecutionContext::set_waiter(const std::type_info& type,
std::coroutine_handle<>
cont,
std::any* slot)
void ExecutionContext::set_waiter(
std::span<const std::type_info* const> types,
std::coroutine_handle<> cont, EventEnvelope* slot, bool* timed_out,
std::optional<std::chrono::steady_clock::time_point> deadline)
{
m_waiter.type = &type;
m_waiter.types.assign(types.begin(), types.end());
m_waiter.cont = cont;
m_waiter.slot = slot;
m_waiter.timed_out = timed_out;
m_waiter.timer = 0;
if (deadline.has_value()) {
m_waiter.timer = add_timer(*deadline, [this] {
// Timeout: resume the waiter with timed_out set.
if (!m_waiter.active()) {
return;
}
if (m_waiter.timed_out != nullptr) {
*m_waiter.timed_out = true;
}
auto cont2 = m_waiter.cont;
m_waiter = Waiter{};
m_executor.post(cont2);
});
}
}
ExecutionContext::TimerId ExecutionContext::add_timer(
std::chrono::steady_clock::time_point deadline,
std::function<void()>
on_fire)
{
const TimerId id = m_next_timer++;
m_timers.emplace(id, Timer{deadline, std::move(on_fire)});
return id;
}
void ExecutionContext::cancel_timer(TimerId id)
{
if (id != 0) {
m_timers.erase(id);
}
}
std::optional<std::chrono::steady_clock::time_point>
ExecutionContext::next_deadline() const
{
std::optional<std::chrono::steady_clock::time_point> next;
for (const auto& [id, timer] : m_timers) {
if (!next.has_value() || timer.deadline < *next) {
next = timer.deadline;
}
}
return next;
}
void ExecutionContext::fire_due_timers()
{
if (m_timers.empty()) {
return;
}
const auto now = std::chrono::steady_clock::now();
std::vector<std::function<void()>> due;
for (auto it = m_timers.begin(); it != m_timers.end();) {
if (it->second.deadline <= now) {
due.push_back(std::move(it->second.on_fire));
it = m_timers.erase(it);
} else {
++it;
}
}
for (auto& fn : due) {
if (fn) {
fn();
}
}
}
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();
// Match a waiting state first (lets a multi-type waiter receive an
// event without it ever sitting in a buffer).
if (m_waiter.active() && m_waiter.wants(*env.type)) {
*m_waiter.slot = std::move(env);
cancel_timer(m_waiter.timer);
auto cont = m_waiter.cont;
m_waiter = Waiter{};
m_executor.post(cont);
return;
}
// Otherwise buffer for a later await.
m_buffers[std::type_index{*env.type}].push_back(std::move(env.value));
}
std::size_t ExecutionContext::pump()
{
std::size_t work = 0;
fire_due_timers();
while (auto env = m_queue.try_pop()) {
deliver(*env);
++work;
}
work += m_executor.drain();
fire_due_timers();
return work;
}
@@ -121,7 +194,12 @@ namespace Void::Fsm::Detail
m_sleeping.store(false, std::memory_order_release);
continue;
}
m_wakeup.acquire();
// Park until woken, or until the earliest timer is due.
if (const auto deadline = next_deadline(); deadline.has_value()) {
(void)m_wakeup.try_acquire_until(*deadline);
} else {
m_wakeup.acquire();
}
m_sleeping.store(false, std::memory_order_release);
}
}