executor/fibers/scheduler/exe/coroutine/impl.cpp

41 lines
856 B
C++
Raw Normal View History

2024-04-22 15:02:31 +00:00
#include <exe/coroutine/impl.hpp>
#include <wheels/support/assert.hpp>
#include <wheels/support/compiler.hpp>
namespace exe::coroutine {
CoroutineImpl::CoroutineImpl(Routine routine, wheels::MutableMemView stack)
: routine_(std::move(routine)) {
coroutine_context_.Setup(stack, this);
}
void CoroutineImpl::Run() {
try {
routine_();
} catch (...) {
exception_ptr_ = std::current_exception();
}
is_complete_ = true;
coroutine_context_.SwitchTo(save_previous_context_);
abort();
}
void CoroutineImpl::Resume() {
save_previous_context_.SwitchTo(coroutine_context_);
if (exception_ptr_) {
rethrow_exception(exception_ptr_);
}
}
void CoroutineImpl::Suspend() {
coroutine_context_.SwitchTo(save_previous_context_);
}
bool CoroutineImpl::IsCompleted() const {
return is_complete_;
}
} // namespace exe::coroutine