diff --git a/include/Void/Async/Task.hpp b/include/Void/Async/Task.hpp index 71e6845..0ae71fb 100644 --- a/include/Void/Async/Task.hpp +++ b/include/Void/Async/Task.hpp @@ -8,188 +8,247 @@ namespace Void::Coro { -// ── Task ─────────────────────────────────────────────────────────────────── -// A lazy coroutine that produces a single value via co_return. -// The coroutine does NOT run until explicitly driven (run() or sync_wait()). -// -// Awaiting a Task from inside another Task is supported: -// Task inner() { co_return 42; } -// Task outer() { co_return co_await inner() + 1; } -// -// Task is NOT thread-safe and uses no scheduler. Everything runs on the -// caller's thread synchronously when sync_wait() is called. + // ── Task + // ─────────────────────────────────────────────────────────────────── A + // lazy coroutine that produces a single value via co_return. The coroutine + // does NOT run until explicitly driven (run() or sync_wait()). + // + // Awaiting a Task from inside another Task is supported: + // Task inner() { co_return 42; } + // Task outer() { co_return co_await inner() + 1; } + // + // Task is NOT thread-safe and uses no scheduler. Everything runs on the + // caller's thread synchronously when sync_wait() is called. -template -class Task -{ -public: - struct promise_type - { - std::optional result; - std::exception_ptr exception; - std::coroutine_handle<> continuation; // parent waiting on this task + template class Task + { + public: - Task get_return_object() - { - return Task{std::coroutine_handle::from_promise(*this)}; - } + struct promise_type { + std::optional result; + std::exception_ptr exception; + std::coroutine_handle<> + continuation; // parent waiting on this task - std::suspend_always initial_suspend() noexcept { return {}; } + Task get_return_object() + { + return Task{ + std::coroutine_handle::from_promise( + *this)}; + } - struct FinalAwaiter - { - bool await_ready() noexcept { return false; } - void await_resume() noexcept {} - std::coroutine_handle<> await_suspend( - std::coroutine_handle h) noexcept - { - // Resume parent if it was awaiting us; otherwise just stop. - auto cont = h.promise().continuation; - return cont ? cont : std::noop_coroutine(); - } - }; + std::suspend_always initial_suspend() noexcept + { + return {}; + } - FinalAwaiter final_suspend() noexcept { return {}; } + struct FinalAwaiter { + bool await_ready() noexcept { return false; } + void await_resume() noexcept {} + std::coroutine_handle<> await_suspend( + std::coroutine_handle h) noexcept + { + // Resume parent if it was awaiting us; + // otherwise just stop. + auto cont = h.promise().continuation; + return cont ? cont : std::noop_coroutine(); + } + }; - void return_value(T v) { result.emplace(std::move(v)); } - void unhandled_exception() { exception = std::current_exception(); } - }; + FinalAwaiter final_suspend() noexcept { return {}; } - using handle_type = std::coroutine_handle; + void return_value(T v) { result.emplace(std::move(v)); } + void unhandled_exception() + { + exception = std::current_exception(); + } + }; - // ── construction ────────────────────────────────────────────────────────── + using handle_type = std::coroutine_handle; - explicit Task(handle_type h) noexcept : handle_{h} {} + // ── construction + // ────────────────────────────────────────────────────────── - Task(const Task&) = delete; - Task& operator=(const Task&) = delete; + explicit Task(handle_type h) noexcept : handle_{h} {} - Task(Task&& o) noexcept : handle_{std::exchange(o.handle_, {})} {} - Task& operator=(Task&& o) noexcept - { - if (this != &o) { - if (handle_) handle_.destroy(); - handle_ = std::exchange(o.handle_, {}); - } - return *this; - } + Task(const Task&) = delete; + Task& operator=(const Task&) = delete; - ~Task() { if (handle_) handle_.destroy(); } + Task(Task&& o) noexcept : handle_{std::exchange(o.handle_, {})} {} + Task& operator=(Task&& o) noexcept + { + if (this != &o) { + if (handle_) { + handle_.destroy(); + } + handle_ = std::exchange(o.handle_, {}); + } + return *this; + } - [[nodiscard]] bool done() const noexcept { return !handle_ || handle_.done(); } + ~Task() + { + if (handle_) { + handle_.destroy(); + } + } - // ── co_await support (allows Task to be awaited by another Task) ─────── + [[nodiscard]] bool done() const noexcept + { + return !handle_ || handle_.done(); + } - [[nodiscard]] bool await_ready() const noexcept { return false; } + // Non-owning view of the coroutine handle (e.g. to schedule it on + // an executor while the Task retains ownership for cleanup). + [[nodiscard]] handle_type handle() const noexcept + { + return handle_; + } - std::coroutine_handle<> await_suspend(std::coroutine_handle<> continuation) noexcept - { - handle_.promise().continuation = continuation; - return handle_; // resume this task's coroutine - } + // ── co_await support (allows Task to be awaited by another + // Task) ─────── - T await_resume() - { - auto& p = handle_.promise(); - if (p.exception) std::rethrow_exception(p.exception); - return std::move(*p.result); - } + [[nodiscard]] bool await_ready() const noexcept { return false; } - // Synchronous execution — runs the coroutine to completion on the - // calling thread. Returns the result or rethrows any stored exception. - T run() - { - handle_.resume(); - return await_resume(); - } + std::coroutine_handle<> + await_suspend(std::coroutine_handle<> continuation) noexcept + { + handle_.promise().continuation = continuation; + return handle_; // resume this task's coroutine + } -private: - handle_type handle_; -}; + T await_resume() + { + auto& p = handle_.promise(); + if (p.exception) { + std::rethrow_exception(p.exception); + } + return std::move(*p.result); + } -// ── Task ──────────────────────────────────────────────────────────────── + // Synchronous execution — runs the coroutine to completion on the + // calling thread. Returns the result or rethrows any stored + // exception. + T run() + { + handle_.resume(); + return await_resume(); + } -template <> -class Task -{ -public: - struct promise_type - { - std::exception_ptr exception; - std::coroutine_handle<> continuation; + private: - Task get_return_object() - { - return Task{std::coroutine_handle::from_promise(*this)}; - } + handle_type handle_; + }; - std::suspend_always initial_suspend() noexcept { return {}; } + // ── Task + // ──────────────────────────────────────────────────────────────── - struct FinalAwaiter - { - bool await_ready() noexcept { return false; } - void await_resume() noexcept {} - std::coroutine_handle<> await_suspend( - std::coroutine_handle h) noexcept - { - auto cont = h.promise().continuation; - return cont ? cont : std::noop_coroutine(); - } - }; + template <> class Task + { + public: - FinalAwaiter final_suspend() noexcept { return {}; } + struct promise_type { + std::exception_ptr exception; + std::coroutine_handle<> continuation; - void return_void() noexcept {} - void unhandled_exception() { exception = std::current_exception(); } - }; + Task get_return_object() + { + return Task{ + std::coroutine_handle::from_promise( + *this)}; + } - using handle_type = std::coroutine_handle; + std::suspend_always initial_suspend() noexcept + { + return {}; + } - explicit Task(handle_type h) noexcept : handle_{h} {} + struct FinalAwaiter { + bool await_ready() noexcept { return false; } + void await_resume() noexcept {} + std::coroutine_handle<> await_suspend( + std::coroutine_handle h) noexcept + { + auto cont = h.promise().continuation; + return cont ? cont : std::noop_coroutine(); + } + }; - Task(const Task&) = delete; - Task& operator=(const Task&) = delete; - Task(Task&& o) noexcept : handle_{std::exchange(o.handle_, {})} {} - Task& operator=(Task&& o) noexcept - { - if (this != &o) { - if (handle_) handle_.destroy(); - handle_ = std::exchange(o.handle_, {}); - } - return *this; - } + FinalAwaiter final_suspend() noexcept { return {}; } - ~Task() { if (handle_) handle_.destroy(); } + void return_void() noexcept {} + void unhandled_exception() + { + exception = std::current_exception(); + } + }; - [[nodiscard]] bool done() const noexcept { return !handle_ || handle_.done(); } + using handle_type = std::coroutine_handle; - [[nodiscard]] bool await_ready() const noexcept { return false; } - std::coroutine_handle<> await_suspend(std::coroutine_handle<> c) noexcept - { - handle_.promise().continuation = c; - return handle_; - } - void await_resume() - { - if (handle_.promise().exception) - std::rethrow_exception(handle_.promise().exception); - } + explicit Task(handle_type h) noexcept : handle_{h} {} - void run() - { - handle_.resume(); - await_resume(); - } + Task(const Task&) = delete; + Task& operator=(const Task&) = delete; + Task(Task&& o) noexcept : handle_{std::exchange(o.handle_, {})} {} + Task& operator=(Task&& o) noexcept + { + if (this != &o) { + if (handle_) { + handle_.destroy(); + } + handle_ = std::exchange(o.handle_, {}); + } + return *this; + } -private: - handle_type handle_; -}; + ~Task() + { + if (handle_) { + handle_.destroy(); + } + } -// ── sync_wait — drive a Task to completion synchronously ────────────────── + [[nodiscard]] bool done() const noexcept + { + return !handle_ || handle_.done(); + } -template -T sync_wait(Task task) { return task.run(); } + // Non-owning view of the coroutine handle. + [[nodiscard]] handle_type handle() const noexcept + { + return handle_; + } -inline void sync_wait(Task task) { task.run(); } + [[nodiscard]] bool await_ready() const noexcept { return false; } + std::coroutine_handle<> + await_suspend(std::coroutine_handle<> c) noexcept + { + handle_.promise().continuation = c; + return handle_; + } + void await_resume() + { + if (handle_.promise().exception) { + std::rethrow_exception(handle_.promise().exception); + } + } + + void run() + { + handle_.resume(); + await_resume(); + } + + private: + + handle_type handle_; + }; + + // ── sync_wait — drive a Task to completion synchronously + // ────────────────── + + template T sync_wait(Task task) { return task.run(); } + + inline void sync_wait(Task task) { task.run(); } } // namespace Void::Coro diff --git a/include/Void/Fsm/Machine.hpp b/include/Void/Fsm/Machine.hpp index 9f28c90..41166f1 100644 --- a/include/Void/Fsm/Machine.hpp +++ b/include/Void/Fsm/Machine.hpp @@ -533,7 +533,11 @@ namespace Void::Fsm m_owns_ctx = true; } m_status.store(Status::Running); - Async::spawn(m_ctx->executor(), driver()); + // Own the driver coroutine (rather than detaching it) so a + // machine stopped while parked on an event can destroy the + // suspended frame chain instead of leaking it. + m_driver.emplace(driver()); + m_ctx->executor().post(m_driver->handle()); if (m_options.own_worker && m_owns_ctx) { m_ctx->start_worker(); } @@ -589,6 +593,11 @@ namespace Void::Fsm if (m_owns_ctx && m_ctx) { m_ctx->stop(); } + // The worker is joined now (no concurrent resume), so destroy + // the owned driver — this cascade-destroys the suspended + // coroutine chain (a machine parked on an event) instead of + // leaking it. Safe on a finished coroutine too. + m_driver.reset(); } /// Return a finished (or idle) machine to Idle so it can run @@ -602,6 +611,7 @@ namespace Void::Fsm if (m_owns_ctx && m_ctx) { m_ctx->stop(); } + m_driver.reset(); // destroy the (possibly parked) driver chain m_ctx.reset(); m_owns_ctx = false; m_current.store(-1, std::memory_order_release); @@ -1421,6 +1431,11 @@ namespace Void::Fsm // External jump request (jump()); applied by drive() at the // next transition boundary. std::atomic m_jump{nullptr}; + + // The owned top-level driver coroutine (worker mode). Holding it + // (rather than detaching via Async::spawn) lets stop() destroy the + // suspended frame chain of a machine parked on an event. + std::optional> m_driver; }; } // namespace Void::Fsm