#include #include #include namespace Void::Fsm::Detail { // Default state lifecycle hooks (no-ops). The state's behaviour is driven // by a per-state, signature-inferred thunk built at registration. Coro::Task StateBase::on_enter() { co_return; } Coro::Task StateBase::on_exit() { co_return; } ExecutionContext::~ExecutionContext() { stop(); } void ExecutionContext::post(EventEnvelope env) { m_queue.push(std::move(env)); wake(); } bool ExecutionContext::try_take_any( std::span types, EventEnvelope& out) { 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; } } return false; } void ExecutionContext::set_waiter( std::span types, std::coroutine_handle<> cont, EventEnvelope* slot, bool* timed_out, std::optional deadline) { Waiter w; w.id = m_next_waiter++; w.types.assign(types.begin(), types.end()); w.cont = cont; w.slot = slot; w.timed_out = timed_out; w.timer = 0; if (deadline.has_value()) { const WaiterId id = w.id; w.timer = add_timer(*deadline, [this, id] { // Timeout: resume that waiter (if still parked) with timed_out. const auto it = std::find_if(m_waiters.begin(), m_waiters.end(), [id](const Waiter& x) { return x.id == id; }); if (it == m_waiters.end()) { return; } if (it->timed_out != nullptr) { *it->timed_out = true; } auto cont2 = it->cont; m_waiters.erase(it); m_executor.post(cont2); }); } m_waiters.push_back(std::move(w)); } ExecutionContext::TimerId ExecutionContext::add_timer( std::chrono::steady_clock::time_point deadline, std::function 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 ExecutionContext::next_deadline() const { std::optional 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> 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) { // Route to the first parked waiter (FIFO) that wants this type — lets // it receive the event without ever sitting in a buffer. for (auto it = m_waiters.begin(); it != m_waiters.end(); ++it) { if (it->wants(*env.type)) { *it->slot = std::move(env); cancel_timer(it->timer); auto cont = it->cont; m_waiters.erase(it); m_executor.post(cont); return; } } // Otherwise buffer for a later await. 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()) { deliver(*env); ++work; } work += m_executor.drain(); fire_due_timers(); return work; } void ExecutionContext::wake() noexcept { if (m_sleeping.exchange(false, std::memory_order_acq_rel)) { m_wakeup.release(); } } void ExecutionContext::start_worker() { if (m_worker_started.load(std::memory_order_acquire)) { return; } std::call_once(m_worker_once, [this] { m_worker = std::thread([this] { run_loop(); }); m_worker_started.store(true, std::memory_order_release); }); } void ExecutionContext::finish() { m_done.store(true, std::memory_order_release); wake(); } void ExecutionContext::stop() { m_done.store(true, std::memory_order_release); wake(); if (m_worker.joinable()) { m_worker.join(); } } void ExecutionContext::join() { if (m_worker.joinable()) { m_worker.join(); } } void ExecutionContext::run_loop() { for (;;) { 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); // 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 (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(); } m_sleeping.store(false, std::memory_order_release); } } } // namespace Void::Fsm::Detail