/* Coroutines for OctaSTD. * * This file is part of OctaSTD. See COPYING.md for further information. */ #ifndef OSTD_COROUTINE_HH #define OSTD_COROUTINE_HH #include #include #include #include #include #include #include #include #include "ostd/types.hh" #include "ostd/range.hh" #include "ostd/internal/context.hh" namespace ostd { constexpr size_t COROUTINE_DEFAULT_STACK_SIZE = SIGSTKSZ; struct coroutine_error: std::runtime_error { using std::runtime_error::runtime_error; }; template struct coroutine; template struct coroutine_range; namespace detail { /* like reference_wrapper but for any value */ template struct arg_wrapper { arg_wrapper() = default; arg_wrapper(T arg): p_arg(std::move(arg)) {} void operator=(T arg) { p_arg = std::move(arg); } operator T &&() { return std::move(p_arg); } void swap(arg_wrapper &other) { using std::swap; swap(p_arg, other.p_arg); } private: T p_arg = T{}; }; template struct arg_wrapper { arg_wrapper() = default; arg_wrapper(T &&arg): p_arg(&arg) {} void operator=(T &&arg) { p_arg = &arg; } operator T &&() { return *p_arg; } void swap(arg_wrapper &other) { using std::swap; swap(p_arg, other.p_arg); } private: T *p_arg = nullptr; }; template struct arg_wrapper { arg_wrapper() = default; arg_wrapper(T &arg): p_arg(&arg) {} void operator=(T &arg) { p_arg = &arg; } operator T &() { return *p_arg; } void swap(arg_wrapper &other) { using std::swap; swap(p_arg, other.p_arg); } private: T *p_arg = nullptr; }; template inline void swap(arg_wrapper &a, arg_wrapper &b) { a.swap(b); } template struct coro_types { using yield_type = std::tuple; }; template struct coro_types { using yield_type = A; }; template struct coro_types { using yield_type = std::pair; }; template using coro_args = typename coro_types::yield_type; template inline coro_args yield_ret( std::tuple...> &args, std::index_sequence ) { if constexpr(sizeof...(A) == 1) { return std::forward(std::get<0>(args)); } else if constexpr(sizeof...(A) == 2) { return std::make_pair(std::forward(std::get(args))...); } else { return std::move(args); } } /* default case, yield returns args and takes a value */ template struct coro_base: coroutine_context { protected: struct yielder { yielder(coro_base &coro): p_coro(coro) {} coro_args operator()(R &&ret) { p_coro.p_result = std::forward(ret); p_coro.yield_jump(); return yield_ret( p_coro.p_args, std::make_index_sequence{} ); } private: coro_base &p_coro; }; template void call_helper(F &func, std::index_sequence) { p_result = std::forward( func(yielder{*this}, std::forward(std::get(p_args))...) ); } R call(A ...args) { p_args = std::make_tuple(arg_wrapper(std::forward(args))...); coroutine_context::call(); return std::forward(p_result); } void swap(coro_base &other) { using std::swap; swap(p_args, other.p_args); swap(p_result, other.p_result); coroutine_context::swap(other); } std::tuple...> p_args; arg_wrapper p_result; }; /* yield takes a value but doesn't return any args */ template struct coro_base: coroutine_context { coroutine_range iter(); protected: struct yielder { yielder(coro_base &coro): p_coro(coro) {} void operator()(R &&ret) { p_coro.p_result = std::forward(ret); p_coro.yield_jump(); } private: coro_base &p_coro; }; template void call_helper(F &func, std::index_sequence) { p_result = std::forward(func(yielder{*this})); } R call() { coroutine_context::call(); return std::forward(this->p_result); } void swap(coro_base &other) { using std::swap; swap(p_result, other.p_result); coroutine_context::swap(other); } arg_wrapper p_result; }; /* yield doesn't take a value and returns args */ template struct coro_base: coroutine_context { protected: struct yielder { yielder(coro_base &coro): p_coro(coro) {} coro_args operator()() { p_coro.yield_jump(); return yield_ret( p_coro.p_args, std::make_index_sequence{} ); } private: coro_base &p_coro; }; template void call_helper(F &func, std::index_sequence) { func(yielder{*this}, std::forward(std::get(p_args))...); } void call(A ...args) { p_args = std::make_tuple(arg_wrapper(std::forward(args))...); coroutine_context::call(); } void swap(coro_base &other) { using std::swap; swap(p_args, other.p_args); coroutine_context::swap(other); } std::tuple...> p_args; }; /* yield doesn't take a value or return any args */ template<> struct coro_base: coroutine_context { protected: struct yielder { yielder(coro_base &coro): p_coro(coro) {} void operator()() { p_coro.yield_jump(); } private: coro_base &p_coro; }; template void call_helper(F &func, std::index_sequence) { func(yielder{*this}); } void call() { coroutine_context::call(); } void swap(coro_base &other) { coroutine_context::swap(other); } }; } /* namespace detail */ template struct coroutine: detail::coro_base { private: using base_t = detail::coro_base; public: using yield_type = typename detail::coro_base::yielder; /* we have no way to assign a function anyway... */ coroutine() = delete; template coroutine(F func, size_t ss = COROUTINE_DEFAULT_STACK_SIZE): base_t(), p_func(std::move(func)) { /* that way there is no context creation/stack allocation */ if (!p_func) { return; } this->make_context(ss, &context_call); } coroutine(coroutine const &) = delete; coroutine(coroutine &&c): detail::coro_base(std::move(c)), p_func(std::move(c.p_func)) { c.p_func = nullptr; } coroutine &operator=(coroutine const &) = delete; coroutine &operator=(coroutine &&c) { base_t::operator=(std::move(c)); p_func = std::move(c.p_func); c.p_func = nullptr; } ~coroutine() { if (!p_func) { /* the stack has already unwound by a normal return */ return; } this->unwind(); } explicit operator bool() const { return bool(p_func); } R resume(A ...args) { if (!p_func) { throw coroutine_error{"dead coroutine"}; } return this->call(std::forward(args)...); } R operator()(A ...args) { return resume(std::forward(args)...); } void swap(coroutine &other) { std::swap(p_func, other.p_func); base_t::swap(other); } private: /* the main entry point of the coroutine */ static void context_call(detail::transfer_t t) { auto &self = *(static_cast(t.data)); self.p_orig = t.ctx; try { self.call_helper(self.p_func, std::index_sequence_for{}); } catch (detail::coroutine_context::forced_unwind v) { /* forced_unwind is unique */ self.p_orig = v.ctx; } catch (...) { /* some other exception, will be rethrown later */ self.p_except = std::current_exception(); } /* the func has fully finished here, so mark dead, stack * will be freed by the coroutine's destructor later */ self.p_func = nullptr; /* perform a last switch back to original context */ self.yield_jump(); } std::function p_func; }; template inline void swap(coroutine &a, coroutine &b) { a.swap(b); } template struct coroutine_range: input_range> { using range_category = input_range_tag; using value_type = T; using reference = T &; using size_type = size_t; using difference_type = stream_off_t; coroutine_range() = delete; coroutine_range(coroutine &c): p_coro(&c) { pop_front(); } coroutine_range(coroutine_range const &r): p_coro(r.p_coro), p_item(r.p_item) {} bool empty() const { return !p_item; } void pop_front() { if (*p_coro) { p_item = (*p_coro)(); } else { p_item = std::nullopt; } } reference front() const { return p_item.value(); } bool equals_front(coroutine_range const &g) const { return p_coro == g.p_coro; } private: coroutine *p_coro; mutable std::optional p_item; }; namespace detail { template coroutine_range coro_base::iter() { return coroutine_range{static_cast &>(*this)}; } } } /* namespace ostd */ #endif