2017-03-04 17:25:09 +00:00
|
|
|
/* Coroutines for OctaSTD.
|
|
|
|
*
|
|
|
|
* This file is part of OctaSTD. See COPYING.md for further information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSTD_COROUTINE_HH
|
|
|
|
#define OSTD_COROUTINE_HH
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <exception>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <utility>
|
|
|
|
#include <tuple>
|
2017-03-05 22:48:51 +00:00
|
|
|
#include <type_traits>
|
2017-03-05 23:47:15 +00:00
|
|
|
#include <optional>
|
2017-03-04 17:25:09 +00:00
|
|
|
|
|
|
|
#include "ostd/types.hh"
|
2017-03-05 15:48:44 +00:00
|
|
|
#include "ostd/platform.hh"
|
2017-03-05 23:47:15 +00:00
|
|
|
#include "ostd/range.hh"
|
2017-03-05 15:48:44 +00:00
|
|
|
|
|
|
|
/* from boost.context */
|
|
|
|
#ifdef OSTD_PLATFORM_WIN32
|
|
|
|
# if (defined(i386) || defined(__i386__) || defined(__i386) || \
|
|
|
|
defined(__i486__) || defined(__i586__) || defined(__i686__) || \
|
|
|
|
defined(__X86__) || defined(_X86_) || defined(__THW_INTEL__) || \
|
|
|
|
defined(__I86__) || defined(__INTEL__) || defined(__IA32__) || \
|
|
|
|
defined(_M_IX86) || defined(_I86_))
|
|
|
|
# define OSTD_CONTEXT_CDECL __cdecl
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef OSTD_CONTEXT_CDECL
|
|
|
|
#define OSTD_CONTEXT_CDECL
|
|
|
|
#endif
|
2017-03-04 17:25:09 +00:00
|
|
|
|
|
|
|
namespace ostd {
|
|
|
|
|
|
|
|
constexpr size_t COROUTINE_DEFAULT_STACK_SIZE = SIGSTKSZ;
|
|
|
|
|
|
|
|
struct coroutine_error: std::runtime_error {
|
|
|
|
using std::runtime_error::runtime_error;
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:48:44 +00:00
|
|
|
namespace detail {
|
|
|
|
/* from boost.fcontext */
|
|
|
|
using fcontext_t = void *;
|
|
|
|
|
|
|
|
struct transfer_t {
|
|
|
|
fcontext_t ctx;
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" OSTD_EXPORT
|
|
|
|
transfer_t OSTD_CONTEXT_CDECL ostd_jump_fcontext(
|
|
|
|
fcontext_t const to, void *vp
|
|
|
|
);
|
|
|
|
|
|
|
|
extern "C" OSTD_EXPORT
|
|
|
|
fcontext_t OSTD_CONTEXT_CDECL ostd_make_fcontext(
|
|
|
|
void *sp, size_t size, void (*fn)(transfer_t)
|
|
|
|
);
|
|
|
|
|
|
|
|
extern "C" OSTD_EXPORT
|
|
|
|
transfer_t OSTD_CONTEXT_CDECL ostd_ontop_fcontext(
|
|
|
|
fcontext_t const to, void *vp, transfer_t (*fn)(transfer_t)
|
|
|
|
);
|
|
|
|
|
|
|
|
struct forced_unwind {
|
|
|
|
fcontext_t ctx;
|
|
|
|
forced_unwind(fcontext_t c): ctx(c) {}
|
|
|
|
};
|
2017-03-04 17:25:09 +00:00
|
|
|
|
2017-03-07 01:07:13 +00:00
|
|
|
struct coroutine_context {
|
|
|
|
coroutine_context(size_t ss, void (*callp)(void *)):
|
|
|
|
p_stack(new byte[ss]), p_callp(callp)
|
|
|
|
{
|
|
|
|
p_coro = ostd_make_fcontext(p_stack.get() + ss, ss, context_call);
|
|
|
|
}
|
2017-03-06 17:37:33 +00:00
|
|
|
|
2017-03-07 01:07:13 +00:00
|
|
|
coroutine_context(coroutine_context const &) = delete;
|
|
|
|
coroutine_context(coroutine_context &&c):
|
|
|
|
p_stack(std::move(c.p_stack)), p_coro(c.p_coro), p_orig(c.p_orig),
|
|
|
|
p_except(std::move(c.p_except)), p_callp(c.p_callp)
|
|
|
|
{
|
|
|
|
c.p_coro = c.p_orig = nullptr;
|
|
|
|
c.p_callp = nullptr;
|
|
|
|
}
|
2017-03-06 17:37:33 +00:00
|
|
|
|
2017-03-07 01:07:13 +00:00
|
|
|
coroutine_context &operator=(coroutine_context const &) = delete;
|
|
|
|
coroutine_context &operator=(coroutine_context &&c) {
|
|
|
|
swap(c);
|
|
|
|
return *this;
|
2017-03-04 17:25:09 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 01:07:13 +00:00
|
|
|
void call() {
|
|
|
|
coro_jump();
|
|
|
|
if (p_except) {
|
|
|
|
std::rethrow_exception(std::move(p_except));
|
2017-03-05 15:48:44 +00:00
|
|
|
}
|
2017-03-07 01:07:13 +00:00
|
|
|
}
|
2017-03-06 17:37:33 +00:00
|
|
|
|
2017-03-07 01:07:13 +00:00
|
|
|
void unwind() {
|
|
|
|
ostd_ontop_fcontext(
|
|
|
|
std::exchange(p_coro, nullptr), nullptr,
|
|
|
|
[](transfer_t t) -> transfer_t {
|
|
|
|
throw forced_unwind{t.ctx};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2017-03-04 17:25:09 +00:00
|
|
|
|
2017-03-07 01:07:13 +00:00
|
|
|
void coro_jump() {
|
|
|
|
p_coro = ostd_jump_fcontext(p_coro, this).ctx;
|
|
|
|
}
|
2017-03-06 17:37:33 +00:00
|
|
|
|
2017-03-07 01:07:13 +00:00
|
|
|
void yield_jump() {
|
|
|
|
p_orig = ostd_jump_fcontext(p_orig, nullptr).ctx;
|
|
|
|
}
|
2017-03-06 17:37:33 +00:00
|
|
|
|
2017-03-07 01:07:13 +00:00
|
|
|
void swap(coroutine_context &other) noexcept {
|
|
|
|
std::swap(p_stack, other.p_stack);
|
|
|
|
std::swap(p_coro, other.p_coro);
|
|
|
|
std::swap(p_orig, other.p_orig);
|
|
|
|
std::swap(p_except, other.p_except);
|
|
|
|
std::swap(p_callp, other.p_callp);
|
|
|
|
}
|
2017-03-05 15:48:44 +00:00
|
|
|
|
2017-03-07 01:07:13 +00:00
|
|
|
private:
|
|
|
|
static void context_call(transfer_t t) {
|
|
|
|
auto &self = *(static_cast<coroutine_context *>(t.data));
|
|
|
|
self.p_orig = t.ctx;
|
|
|
|
try {
|
|
|
|
self.p_callp(t.data);
|
|
|
|
} catch (forced_unwind v) {
|
|
|
|
self.p_orig = v.ctx;
|
|
|
|
} catch (...) {
|
|
|
|
self.p_except = std::current_exception();
|
|
|
|
}
|
|
|
|
self.yield_jump();
|
|
|
|
}
|
2017-03-04 17:25:09 +00:00
|
|
|
|
2017-03-07 01:07:13 +00:00
|
|
|
/* TODO: new'ing the stack is sub-optimal */
|
|
|
|
std::unique_ptr<byte[]> p_stack;
|
|
|
|
fcontext_t p_coro;
|
|
|
|
fcontext_t p_orig;
|
|
|
|
std::exception_ptr p_except;
|
|
|
|
void (*p_callp)(void *);
|
|
|
|
};
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 17:25:09 +00:00
|
|
|
template<typename T>
|
|
|
|
struct coroutine;
|
|
|
|
|
2017-03-06 18:01:17 +00:00
|
|
|
template<typename T>
|
|
|
|
struct coroutine_range;
|
|
|
|
|
2017-03-04 17:25:09 +00:00
|
|
|
namespace detail {
|
2017-03-05 22:48:51 +00:00
|
|
|
/* like reference_wrapper but for any value */
|
|
|
|
template<typename T>
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
void swap(arg_wrapper &other) {
|
|
|
|
std::swap(p_arg, other.p_arg);
|
|
|
|
}
|
|
|
|
|
2017-03-05 22:48:51 +00:00
|
|
|
private:
|
|
|
|
T p_arg = T{};
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct arg_wrapper<T &&> {
|
|
|
|
arg_wrapper() = default;
|
|
|
|
arg_wrapper(T &&arg): p_arg(&arg) {}
|
|
|
|
|
|
|
|
void operator=(T &&arg) {
|
|
|
|
p_arg = &arg;
|
|
|
|
}
|
|
|
|
operator T &&() {
|
|
|
|
return *p_arg;
|
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
void swap(arg_wrapper &other) {
|
|
|
|
std::swap(p_arg, other.p_arg);
|
|
|
|
}
|
|
|
|
|
2017-03-05 22:48:51 +00:00
|
|
|
private:
|
|
|
|
T *p_arg = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct arg_wrapper<T &> {
|
|
|
|
arg_wrapper() = default;
|
|
|
|
arg_wrapper(T &arg): p_arg(&arg) {}
|
|
|
|
|
|
|
|
void operator=(T &arg) {
|
|
|
|
p_arg = &arg;
|
|
|
|
}
|
|
|
|
operator T &() {
|
|
|
|
return *p_arg;
|
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
void swap(arg_wrapper &other) {
|
|
|
|
std::swap(p_arg, other.p_arg);
|
|
|
|
}
|
|
|
|
|
2017-03-05 22:48:51 +00:00
|
|
|
private:
|
|
|
|
T *p_arg = nullptr;
|
|
|
|
};
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
template<typename T>
|
|
|
|
inline void swap(arg_wrapper<T> &a, arg_wrapper<T> &b) {
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2017-03-05 18:29:44 +00:00
|
|
|
template<typename ...A>
|
|
|
|
struct coro_types {
|
2017-03-05 22:48:51 +00:00
|
|
|
using yield_type = std::tuple<A...>;
|
2017-03-05 18:29:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename A>
|
|
|
|
struct coro_types<A> {
|
2017-03-05 22:48:51 +00:00
|
|
|
using yield_type = A;
|
2017-03-05 18:29:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename A, typename B>
|
|
|
|
struct coro_types<A, B> {
|
2017-03-05 22:48:51 +00:00
|
|
|
using yield_type = std::pair<A, B>;
|
2017-03-05 18:29:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ...A>
|
|
|
|
using coro_args = typename coro_types<A...>::yield_type;
|
|
|
|
|
|
|
|
template<typename ...A, size_t ...I>
|
|
|
|
inline coro_args<A...> yield_ret(
|
2017-03-05 22:48:51 +00:00
|
|
|
std::tuple<arg_wrapper<A>...> &args, std::index_sequence<I...>
|
2017-03-05 18:29:44 +00:00
|
|
|
) {
|
|
|
|
if constexpr(sizeof...(A) == 1) {
|
2017-03-05 22:48:51 +00:00
|
|
|
return std::forward<A...>(std::get<0>(args));
|
2017-03-05 18:29:44 +00:00
|
|
|
} else if constexpr(sizeof...(A) == 2) {
|
|
|
|
return std::make_pair(std::forward<A>(std::get<I>(args))...);
|
|
|
|
} else {
|
|
|
|
return std::move(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 19:11:39 +00:00
|
|
|
/* default case, yield returns args and takes a value */
|
2017-03-04 17:25:09 +00:00
|
|
|
template<typename R, typename ...A>
|
2017-03-07 01:07:13 +00:00
|
|
|
struct coro_base: detail::coroutine_context {
|
2017-03-06 17:37:33 +00:00
|
|
|
protected:
|
2017-03-04 17:25:09 +00:00
|
|
|
coro_base(void (*callp)(void *), size_t ss):
|
2017-03-07 01:07:13 +00:00
|
|
|
detail::coroutine_context(ss, callp)
|
2017-03-04 17:25:09 +00:00
|
|
|
{}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
coro_base(coro_base const &) = delete;
|
2017-03-07 01:07:13 +00:00
|
|
|
coro_base(coro_base &&c) = default;
|
2017-03-04 17:25:09 +00:00
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
coro_base &operator=(coro_base const &) = delete;
|
2017-03-07 01:07:13 +00:00
|
|
|
coro_base &operator=(coro_base &&c) = default;
|
2017-03-06 17:37:33 +00:00
|
|
|
|
|
|
|
struct yielder {
|
|
|
|
yielder(coro_base<R, A...> &coro): p_coro(coro) {}
|
|
|
|
|
|
|
|
coro_args<A...> operator()(R &&ret) {
|
|
|
|
p_coro.p_result = std::forward<R>(ret);
|
2017-03-07 01:07:13 +00:00
|
|
|
p_coro.yield_jump();
|
2017-03-06 17:37:33 +00:00
|
|
|
return yield_ret(
|
|
|
|
p_coro.p_args, std::make_index_sequence<sizeof...(A)>{}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
coro_base<R, A...> &p_coro;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename F, size_t ...I>
|
|
|
|
void call_helper(F &func, std::index_sequence<I...>) {
|
2017-03-05 22:07:00 +00:00
|
|
|
p_result = std::forward<R>(
|
2017-03-06 17:37:33 +00:00
|
|
|
func(yielder{*this}, std::forward<A>(std::get<I>(p_args))...)
|
2017-03-05 22:07:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
R call(A ...args) {
|
2017-03-05 22:48:51 +00:00
|
|
|
p_args = std::make_tuple(arg_wrapper<A>(std::forward<A>(args))...);
|
2017-03-07 01:07:13 +00:00
|
|
|
detail::coroutine_context::call();
|
2017-03-05 22:48:51 +00:00
|
|
|
return std::forward<R>(p_result);
|
2017-03-05 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
void swap(coro_base &other) {
|
|
|
|
std::swap(p_args, other.p_args);
|
|
|
|
std::swap(p_result, other.p_result);
|
2017-03-07 01:07:13 +00:00
|
|
|
detail::coroutine_context::swap(other);
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 22:48:51 +00:00
|
|
|
std::tuple<arg_wrapper<A>...> p_args;
|
|
|
|
arg_wrapper<R> p_result;
|
2017-03-04 17:25:09 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 19:11:39 +00:00
|
|
|
/* yield takes a value but doesn't return any args */
|
|
|
|
template<typename R>
|
2017-03-07 01:07:13 +00:00
|
|
|
struct coro_base<R>: detail::coroutine_context {
|
2017-03-06 18:01:17 +00:00
|
|
|
coroutine_range<R> iter();
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
protected:
|
2017-03-05 19:11:39 +00:00
|
|
|
coro_base(void (*callp)(void *), size_t ss):
|
2017-03-07 01:07:13 +00:00
|
|
|
detail::coroutine_context(ss, callp)
|
2017-03-05 19:11:39 +00:00
|
|
|
{}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
coro_base(coro_base const &) = delete;
|
2017-03-07 01:07:13 +00:00
|
|
|
coro_base(coro_base &&c) = default;
|
2017-03-05 19:11:39 +00:00
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
coro_base &operator=(coro_base const &) = delete;
|
2017-03-07 01:07:13 +00:00
|
|
|
coro_base &operator=(coro_base &&c) = default;
|
2017-03-06 17:37:33 +00:00
|
|
|
|
|
|
|
struct yielder {
|
|
|
|
yielder(coro_base<R> &coro): p_coro(coro) {}
|
|
|
|
|
|
|
|
void operator()(R &&ret) {
|
|
|
|
p_coro.p_result = std::forward<R>(ret);
|
2017-03-07 01:07:13 +00:00
|
|
|
p_coro.yield_jump();
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
coro_base<R> &p_coro;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename F, size_t ...I>
|
|
|
|
void call_helper(F &func, std::index_sequence<I...>) {
|
|
|
|
p_result = std::forward<R>(func(yielder{*this}));
|
2017-03-05 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R call() {
|
2017-03-07 01:07:13 +00:00
|
|
|
detail::coroutine_context::call();
|
2017-03-05 22:07:00 +00:00
|
|
|
return std::forward<R>(this->p_result);
|
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
void swap(coro_base &other) {
|
|
|
|
std::swap(p_result, other.p_result);
|
2017-03-07 01:07:13 +00:00
|
|
|
detail::coroutine_context::swap(other);
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 22:48:51 +00:00
|
|
|
arg_wrapper<R> p_result;
|
2017-03-05 19:11:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* yield doesn't take a value and returns args */
|
2017-03-04 17:25:09 +00:00
|
|
|
template<typename ...A>
|
2017-03-07 01:07:13 +00:00
|
|
|
struct coro_base<void, A...>: detail::coroutine_context {
|
2017-03-06 17:37:33 +00:00
|
|
|
protected:
|
2017-03-04 17:25:09 +00:00
|
|
|
coro_base(void (*callp)(void *), size_t ss):
|
2017-03-07 01:07:13 +00:00
|
|
|
detail::coroutine_context(ss, callp)
|
2017-03-04 17:25:09 +00:00
|
|
|
{}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
coro_base(coro_base const &) = delete;
|
2017-03-07 01:07:13 +00:00
|
|
|
coro_base(coro_base &&c) = default;
|
2017-03-04 17:25:09 +00:00
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
coro_base &operator=(coro_base const &) = delete;
|
2017-03-07 01:07:13 +00:00
|
|
|
coro_base &operator=(coro_base &&c) = default;
|
2017-03-06 17:37:33 +00:00
|
|
|
|
|
|
|
struct yielder {
|
|
|
|
yielder(coro_base<void, A...> &coro): p_coro(coro) {}
|
|
|
|
|
|
|
|
coro_args<A...> operator()() {
|
2017-03-07 01:07:13 +00:00
|
|
|
p_coro.yield_jump();
|
2017-03-06 17:37:33 +00:00
|
|
|
return yield_ret(
|
|
|
|
p_coro.p_args, std::make_index_sequence<sizeof...(A)>{}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
coro_base<void, A...> &p_coro;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename F, size_t ...I>
|
|
|
|
void call_helper(F &func, std::index_sequence<I...>) {
|
|
|
|
func(yielder{*this}, std::forward<A>(std::get<I>(p_args))...);
|
2017-03-05 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void call(A ...args) {
|
2017-03-05 22:48:51 +00:00
|
|
|
p_args = std::make_tuple(arg_wrapper<A>(std::forward<A>(args))...);
|
2017-03-07 01:07:13 +00:00
|
|
|
detail::coroutine_context::call();
|
2017-03-05 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
void swap(coro_base &other) {
|
|
|
|
std::swap(p_args, other.p_args);
|
2017-03-07 01:07:13 +00:00
|
|
|
detail::coroutine_context::swap(other);
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 22:48:51 +00:00
|
|
|
std::tuple<arg_wrapper<A>...> p_args;
|
2017-03-04 17:25:09 +00:00
|
|
|
};
|
2017-03-05 19:11:39 +00:00
|
|
|
|
|
|
|
/* yield doesn't take a value or return any args */
|
|
|
|
template<>
|
2017-03-07 01:07:13 +00:00
|
|
|
struct coro_base<void>: detail::coroutine_context {
|
2017-03-06 17:37:33 +00:00
|
|
|
protected:
|
2017-03-05 19:11:39 +00:00
|
|
|
coro_base(void (*callp)(void *), size_t ss):
|
2017-03-07 01:07:13 +00:00
|
|
|
detail::coroutine_context(ss, callp)
|
2017-03-05 19:11:39 +00:00
|
|
|
{}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
coro_base(coro_base const &) = delete;
|
2017-03-07 01:07:13 +00:00
|
|
|
coro_base(coro_base &&c) = default;
|
2017-03-05 19:11:39 +00:00
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
coro_base &operator=(coro_base const &) = delete;
|
2017-03-07 01:07:13 +00:00
|
|
|
coro_base &operator=(coro_base &&c) = default;
|
2017-03-06 17:37:33 +00:00
|
|
|
|
|
|
|
struct yielder {
|
|
|
|
yielder(coro_base<void> &coro): p_coro(coro) {}
|
|
|
|
|
|
|
|
void operator()() {
|
2017-03-07 01:07:13 +00:00
|
|
|
p_coro.yield_jump();
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
coro_base<void> &p_coro;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename F, size_t ...I>
|
|
|
|
void call_helper(F &func, std::index_sequence<I...>) {
|
|
|
|
func(yielder{*this});
|
2017-03-05 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void call() {
|
2017-03-07 01:07:13 +00:00
|
|
|
detail::coroutine_context::call();
|
2017-03-05 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
void swap(coro_base &other) {
|
2017-03-07 01:07:13 +00:00
|
|
|
detail::coroutine_context::swap(other);
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
2017-03-05 19:11:39 +00:00
|
|
|
};
|
2017-03-04 17:25:09 +00:00
|
|
|
} /* namespace detail */
|
|
|
|
|
|
|
|
template<typename R, typename ...A>
|
|
|
|
struct coroutine<R(A...)>: detail::coro_base<R, A...> {
|
2017-03-06 17:37:33 +00:00
|
|
|
private:
|
|
|
|
using base_t = detail::coro_base<R, A...>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using yield_type = typename detail::coro_base<R, A...>::yielder;
|
|
|
|
|
2017-03-05 18:50:25 +00:00
|
|
|
template<typename F>
|
|
|
|
coroutine(F func, size_t ss = COROUTINE_DEFAULT_STACK_SIZE):
|
2017-03-05 15:48:44 +00:00
|
|
|
detail::coro_base<R, A...>(&context_call, ss), p_func(std::move(func))
|
2017-03-04 17:25:09 +00:00
|
|
|
{}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
coroutine(coroutine const &) = delete;
|
2017-03-07 00:12:52 +00:00
|
|
|
coroutine(coroutine &&c):
|
|
|
|
detail::coro_base<R, A...>(std::move(c)), p_func(std::move(c.p_func))
|
|
|
|
{
|
|
|
|
c.p_func = nullptr;
|
|
|
|
}
|
2017-03-06 17:37:33 +00:00
|
|
|
|
|
|
|
coroutine &operator=(coroutine const &) = delete;
|
2017-03-07 00:12:52 +00:00
|
|
|
coroutine &operator=(coroutine &&c) {
|
|
|
|
base_t::operator=(std::move(c));
|
|
|
|
p_func = std::move(c.p_func);
|
|
|
|
c.p_func = nullptr;
|
|
|
|
}
|
2017-03-06 17:37:33 +00:00
|
|
|
|
2017-03-05 15:48:44 +00:00
|
|
|
~coroutine() {
|
2017-03-07 00:12:52 +00:00
|
|
|
/* we have to check both because of potential moves */
|
|
|
|
if (!p_func) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-07 01:07:13 +00:00
|
|
|
this->unwind();
|
2017-03-05 15:48:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 17:25:09 +00:00
|
|
|
operator bool() const {
|
2017-03-07 00:12:52 +00:00
|
|
|
return bool(p_func);
|
2017-03-04 17:25:09 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
R resume(A ...args) {
|
2017-03-07 00:12:52 +00:00
|
|
|
if (!p_func) {
|
|
|
|
throw coroutine_error{"dead coroutine"};
|
|
|
|
}
|
2017-03-06 17:37:33 +00:00
|
|
|
return this->call(std::forward<A>(args)...);
|
|
|
|
}
|
|
|
|
|
2017-03-04 17:25:09 +00:00
|
|
|
R operator()(A ...args) {
|
2017-03-07 00:12:52 +00:00
|
|
|
return resume(std::forward<A>(args)...);
|
2017-03-04 17:25:09 +00:00
|
|
|
}
|
2017-03-06 17:37:33 +00:00
|
|
|
|
|
|
|
void swap(coroutine &other) {
|
|
|
|
std::swap(p_func, other.p_func);
|
|
|
|
base_t::swap(other);
|
|
|
|
}
|
|
|
|
|
2017-03-04 17:25:09 +00:00
|
|
|
private:
|
2017-03-05 15:48:44 +00:00
|
|
|
static void context_call(void *data) {
|
2017-03-04 17:25:09 +00:00
|
|
|
coroutine &self = *(static_cast<coroutine *>(data));
|
2017-03-06 17:37:33 +00:00
|
|
|
self.call_helper(self.p_func, std::index_sequence_for<A...>{});
|
2017-03-07 00:12:52 +00:00
|
|
|
self.p_func = nullptr;
|
2017-03-04 17:25:09 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
std::function<R(yield_type, A...)> p_func;
|
2017-03-04 17:25:09 +00:00
|
|
|
};
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
template<typename R, typename ...A>
|
|
|
|
inline void swap(coroutine<R(A...)> &a, coroutine<R(A...)> &b) {
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:47:15 +00:00
|
|
|
template<typename T>
|
2017-03-06 18:01:17 +00:00
|
|
|
struct coroutine_range: input_range<coroutine_range<T>> {
|
2017-03-05 23:47:15 +00:00
|
|
|
using range_category = input_range_tag;
|
|
|
|
using value_type = T;
|
|
|
|
using reference = T &;
|
|
|
|
using size_type = size_t;
|
|
|
|
using difference_type = stream_off_t;
|
|
|
|
|
2017-03-06 18:01:17 +00:00
|
|
|
coroutine_range() = delete;
|
|
|
|
coroutine_range(coroutine<T()> &c): p_coro(&c) {
|
2017-03-06 01:09:52 +00:00
|
|
|
pop_front();
|
2017-03-05 23:47:15 +00:00
|
|
|
}
|
2017-03-06 18:01:17 +00:00
|
|
|
coroutine_range(coroutine_range const &r):
|
|
|
|
p_coro(r.p_coro), p_item(r.p_item) {}
|
2017-03-05 23:47:15 +00:00
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return !p_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop_front() {
|
2017-03-06 18:01:17 +00:00
|
|
|
if (*p_coro) {
|
|
|
|
p_item = (*p_coro)();
|
2017-03-05 23:47:15 +00:00
|
|
|
} else {
|
|
|
|
p_item = std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reference front() const {
|
|
|
|
return p_item.value();
|
|
|
|
}
|
|
|
|
|
2017-03-06 18:01:17 +00:00
|
|
|
bool equals_front(coroutine_range const &g) {
|
|
|
|
return p_coro == g.p_coro;
|
2017-03-05 23:47:15 +00:00
|
|
|
}
|
|
|
|
private:
|
2017-03-06 18:01:17 +00:00
|
|
|
coroutine<T()> *p_coro;
|
2017-03-05 23:47:15 +00:00
|
|
|
mutable std::optional<T> p_item;
|
|
|
|
};
|
|
|
|
|
2017-03-06 18:01:17 +00:00
|
|
|
namespace detail {
|
|
|
|
template<typename R>
|
|
|
|
coroutine_range<R> coro_base<R>::iter() {
|
|
|
|
return coroutine_range<R>{static_cast<coroutine<R()> &>(*this)};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-04 17:25:09 +00:00
|
|
|
} /* namespace ostd */
|
|
|
|
|
|
|
|
#endif
|