From c72951a36d6cb9775dc1ecd9bc26bc13e796f10c Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Sat, 2 Jul 2022 13:45:09 -0700 Subject: Dropping the async interface, and adding some real uarch. --- aisa/coroutine.h | 119 ------------------------------------------------------- 1 file changed, 119 deletions(-) delete mode 100644 aisa/coroutine.h (limited to 'aisa/coroutine.h') diff --git a/aisa/coroutine.h b/aisa/coroutine.h deleted file mode 100644 index 1b55362..0000000 --- a/aisa/coroutine.h +++ /dev/null @@ -1,119 +0,0 @@ -#pragma once - -#include -#include - -namespace aisa { - - template struct task_promise; - - template struct task : public std::coroutine_handle> { - using handle = std::coroutine_handle>; - using promise_type = task_promise; - bool await_ready() const noexcept { return handle::done(); } - result_t await_resume() const noexcept; - template void await_suspend(std::coroutine_handle> h) const noexcept; - std::optional operator()() noexcept; - }; - - template<> struct task : public std::coroutine_handle> { - using handle = std::coroutine_handle>; - using promise_type = task_promise; - bool await_ready() const noexcept { return handle::done(); } - void await_resume() const noexcept; - template void await_suspend(std::coroutine_handle> h) const noexcept; - bool operator()() noexcept; - }; - - template struct task_promise { - std::coroutine_handle<> precursor; - std::optional result; - task_promise() = default; - task_promise(const task_promise &) = delete; - task get_return_object() noexcept { return task{std::coroutine_handle>::from_promise(*this)}; } - std::suspend_never initial_suspend() const noexcept { return {}; } - std::suspend_always final_suspend() const noexcept { return {}; } - void unhandled_exception() { } - void return_value(result_t x) noexcept { result = std::move(x); } - }; - - template<> struct task_promise { - std::coroutine_handle<> precursor; - task_promise() = default; - task_promise(const task_promise &) = delete; - task get_return_object() noexcept { return task{std::coroutine_handle>::from_promise(*this)}; } - std::suspend_never initial_suspend() const noexcept { return {}; } - std::suspend_always final_suspend() const noexcept { return {}; } - void unhandled_exception() { } - void return_void() noexcept { } - }; - - template result_t task::await_resume() const noexcept - { - auto x = std::move(handle::promise().result.value()); - handle::destroy(); - return x; - } - - template template void task::await_suspend(std::coroutine_handle> h) const noexcept - { - h.promise().precursor = *this; - } - - template std::optional task::operator()() noexcept - { - if (!handle::operator bool()) - return {}; - if (!handle::done()) { - auto &precursor = handle::promise().precursor; - if (precursor) { - if (!precursor.done()) - precursor.resume(); - if (precursor.done()) - precursor = nullptr; - } - if (!precursor) - handle::resume(); - } - if (handle::done()) { - auto x = await_resume(); - handle::operator=(nullptr); - return x; - } - return {}; - } - - inline void task::await_resume() const noexcept - { - handle::destroy(); - } - - template void task::await_suspend(std::coroutine_handle> h) const noexcept - { - h.promise().precursor = *this; - } - - inline bool task::operator()() noexcept - { - if (!handle::operator bool()) - return true; - if (!handle::done()) { - auto &precursor = handle::promise().precursor; - if (precursor) { - if (!precursor.done()) - precursor.resume(); - if (precursor.done()) - precursor = nullptr; - } - if (!precursor) - handle::resume(); - } - if (handle::done()) { - await_resume(); - handle::operator=(nullptr); - return true; - } - return false; - } - -} -- cgit v1.2.3