add a way to retrieve the current coroutine context

master
Daniel Kolesa 2017-03-17 03:04:15 +01:00
parent 06fbdc7419
commit 0da22e777d
2 changed files with 21 additions and 2 deletions

View File

@ -15,8 +15,10 @@
#include "ostd/context_stack.hh"
namespace ostd {
namespace detail {
struct coroutine_context;
namespace detail {
/* from boost.fcontext */
using fcontext_t = void *;
@ -40,12 +42,19 @@ namespace detail {
fcontext_t const to, void *vp, transfer_t (*fn)(transfer_t)
);
OSTD_EXPORT extern thread_local coroutine_context *coro_current;
} /* namespace detail */
struct coroutine_context {
static coroutine_context *current() {
return detail::coro_current;
}
protected:
coroutine_context() {}
~coroutine_context() {
/* coroutine context must be polymorphic */
virtual ~coroutine_context() {
unwind();
}
@ -67,7 +76,11 @@ protected:
void call() {
p_state = state::EXEC;
/* switch to new coroutine */
coroutine_context *curr = std::exchange(detail::coro_current, this);
coro_jump();
/* switch back to previous */
detail::coro_current = curr;
if (p_except) {
std::rethrow_exception(std::move(p_except));
}

View File

@ -158,4 +158,10 @@ size_t stack_traits::default_size() noexcept {
#endif
}
struct coroutine_context;
namespace detail {
OSTD_EXPORT thread_local coroutine_context *coro_current = nullptr;
}
} /* namespace ostd */