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:
1 parent
e941a48007
commit
422a7705c5
4 files changed
+682
-32
No files matched your search
@@ -7,15 +7,22 @@
|
||||
|
||||
#include <any>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <coroutine>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <semaphore>
|
||||
#include <span>
|
||||
#include <thread>
|
||||
#include <typeindex>
|
||||
#include <typeinfo>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Fsm::Detail
|
||||
{
|
||||
@@ -79,10 +86,29 @@ namespace Void::Fsm::Detail
|
||||
|
||||
// Mailbox ops — consumer thread only (called from awaiters that
|
||||
// run on the executor, and from the worker drain).
|
||||
[[nodiscard]] bool try_take(const std::type_info& type,
|
||||
std::any& out);
|
||||
void set_waiter(const std::type_info& type,
|
||||
std::coroutine_handle<> cont, std::any* slot);
|
||||
|
||||
using TimerId = std::uint64_t;
|
||||
|
||||
/// Pop the oldest buffered event whose type is in `types`.
|
||||
[[nodiscard]] bool
|
||||
try_take_any(std::span<const std::type_info* const> types,
|
||||
EventEnvelope& out);
|
||||
|
||||
/// Suspend until an event of one of `types` arrives, or (if
|
||||
/// `deadline` is set) the deadline passes. `slot` receives the
|
||||
/// matched event; `*timed_out` is set true on timeout.
|
||||
void 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);
|
||||
|
||||
/// Schedule `on_fire` at `deadline`. Returns an id for
|
||||
/// cancel_timer.
|
||||
TimerId add_timer(std::chrono::steady_clock::time_point deadline,
|
||||
std::function<void()>
|
||||
on_fire);
|
||||
void cancel_timer(TimerId id);
|
||||
|
||||
// ── worker lifecycle ─────────────────────────────────────────
|
||||
/// Spawn the worker thread (idempotent).
|
||||
@@ -108,18 +134,44 @@ namespace Void::Fsm::Detail
|
||||
void deliver(EventEnvelope& env);
|
||||
void run_loop();
|
||||
void wake() noexcept;
|
||||
void fire_due_timers();
|
||||
[[nodiscard]] std::optional<std::chrono::steady_clock::time_point>
|
||||
next_deadline() const;
|
||||
|
||||
Async::ManualExecutor m_executor;
|
||||
Async::MpscQueue<EventEnvelope> m_queue;
|
||||
|
||||
std::unordered_map<std::type_index, std::deque<std::any>> m_buffers;
|
||||
struct Waiter {
|
||||
const std::type_info* type{nullptr};
|
||||
std::vector<const std::type_info*> types;
|
||||
std::coroutine_handle<> cont{};
|
||||
std::any* slot{nullptr};
|
||||
EventEnvelope* slot{nullptr};
|
||||
bool* timed_out{nullptr};
|
||||
TimerId timer{0};
|
||||
|
||||
[[nodiscard]] bool active() const noexcept
|
||||
{
|
||||
return static_cast<bool>(cont);
|
||||
}
|
||||
[[nodiscard]] bool wants(const std::type_info& t) const
|
||||
{
|
||||
for (const auto* ty : types) {
|
||||
if (*ty == t) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
Waiter m_waiter;
|
||||
|
||||
struct Timer {
|
||||
std::chrono::steady_clock::time_point deadline;
|
||||
std::function<void()> on_fire;
|
||||
};
|
||||
std::map<TimerId, Timer> m_timers;
|
||||
TimerId m_next_timer{1};
|
||||
|
||||
std::counting_semaphore<> m_wakeup{0};
|
||||
std::atomic<bool> m_sleeping{false};
|
||||
std::atomic<bool> m_done{false};
|
||||
|
||||
Reference in new issue
Block a user