forked from OctaForge/libostd
add a way to retrieve the current coroutine context
This commit is contained in:
parent
06fbdc7419
commit
0da22e777d
|
@ -15,8 +15,10 @@
|
||||||
#include "ostd/context_stack.hh"
|
#include "ostd/context_stack.hh"
|
||||||
|
|
||||||
namespace ostd {
|
namespace ostd {
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
|
struct coroutine_context;
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
/* from boost.fcontext */
|
/* from boost.fcontext */
|
||||||
using fcontext_t = void *;
|
using fcontext_t = void *;
|
||||||
|
|
||||||
|
@ -40,12 +42,19 @@ namespace detail {
|
||||||
fcontext_t const to, void *vp, transfer_t (*fn)(transfer_t)
|
fcontext_t const to, void *vp, transfer_t (*fn)(transfer_t)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
OSTD_EXPORT extern thread_local coroutine_context *coro_current;
|
||||||
} /* namespace detail */
|
} /* namespace detail */
|
||||||
|
|
||||||
struct coroutine_context {
|
struct coroutine_context {
|
||||||
|
static coroutine_context *current() {
|
||||||
|
return detail::coro_current;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
coroutine_context() {}
|
coroutine_context() {}
|
||||||
~coroutine_context() {
|
|
||||||
|
/* coroutine context must be polymorphic */
|
||||||
|
virtual ~coroutine_context() {
|
||||||
unwind();
|
unwind();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +76,11 @@ protected:
|
||||||
|
|
||||||
void call() {
|
void call() {
|
||||||
p_state = state::EXEC;
|
p_state = state::EXEC;
|
||||||
|
/* switch to new coroutine */
|
||||||
|
coroutine_context *curr = std::exchange(detail::coro_current, this);
|
||||||
coro_jump();
|
coro_jump();
|
||||||
|
/* switch back to previous */
|
||||||
|
detail::coro_current = curr;
|
||||||
if (p_except) {
|
if (p_except) {
|
||||||
std::rethrow_exception(std::move(p_except));
|
std::rethrow_exception(std::move(p_except));
|
||||||
}
|
}
|
||||||
|
|
|
@ -158,4 +158,10 @@ size_t stack_traits::default_size() noexcept {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct coroutine_context;
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
OSTD_EXPORT thread_local coroutine_context *coro_current = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
} /* namespace ostd */
|
} /* namespace ostd */
|
||||||
|
|
Loading…
Reference in a new issue