diff --git a/include/Scheduler.hh b/include/Scheduler.hh index c0704e9..97aa26d 100644 --- a/include/Scheduler.hh +++ b/include/Scheduler.hh @@ -213,12 +213,21 @@ namespace crouton { /** General purpose Awaitable to return from `yield_value`. It does nothing, just allows the Scheduler to schedule another runnable task if any. */ struct Yielder : public CORO_NS::suspend_always { - explicit Yielder(coro_handle myHandle) :_handle(myHandle) { } coro_handle await_suspend(coro_handle h) noexcept { + _handle = h; return lifecycle::yieldingTo(h, Scheduler::current().yield(h)); } - void await_resume() const noexcept { + + void await_resume() noexcept { Scheduler::current().resumed(_handle); + _handle = nullptr; + } + + ~Yielder() { + if (_handle) { + // If await_resume hasn't been called yet, then the coro is being destroyed. + Scheduler::current().destroying(_handle); + } } private: coro_handle _handle; diff --git a/include/Task.hh b/include/Task.hh index 2d6b06b..aa351ea 100644 --- a/include/Task.hh +++ b/include/Task.hh @@ -78,15 +78,14 @@ namespace crouton { // `co_yield` returns a bool: true to keep running, false if Task has been interrupted. auto yield_value(bool) { struct yielder : public Yielder { - explicit yielder(coro_handle myHandle, std::shared_ptr shared) - :Yielder(myHandle), _shared(std::move(shared)) { } - [[nodiscard]] bool await_resume() const noexcept { + explicit yielder(std::shared_ptr shared) :_shared(std::move(shared)) { } + [[nodiscard]] bool await_resume() noexcept { Yielder::await_resume(); return !_shared->interrupt; } std::shared_ptr _shared; }; - return yielder(handle(), _shared); + return yielder(_shared); } void return_void() {