mirror of https://github.com/RainMark/cops.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
928 B
42 lines
928 B
#include "coroutine.h"
|
|
#include "event_loop.h"
|
|
|
|
namespace cops{
|
|
|
|
static coro_t def([](){});
|
|
coro_t* current = &def;
|
|
|
|
void main(coro_t* coro, context from) {
|
|
coro->next_->ctx_ = from;
|
|
coro->fn_();
|
|
coro->fut_.set_value(0);
|
|
coro->switch_out();
|
|
}
|
|
|
|
void coro_t::switch_out() {
|
|
current = next_;
|
|
#ifdef SPLIT_STACK
|
|
switch_split_stack_context(stack_.ss_ctx_, stack_.next_);
|
|
#endif
|
|
context from = switch_context(next_, next_->ctx_);
|
|
next_ = static_cast<coro_t*>(from);
|
|
next_->ctx_ = from;
|
|
}
|
|
|
|
void coro_t::switch_in() {
|
|
next_ = current;
|
|
current = this;
|
|
#ifdef SPLIT_STACK
|
|
switch_split_stack_context(stack_.next_, stack_.ss_ctx_);
|
|
#endif
|
|
ctx_ = switch_context(this, ctx_);
|
|
}
|
|
|
|
void coro_t::detach(std::unique_ptr<coro_t>& self, event_loop_t* loop) {
|
|
// make sure destruction after coro execute over
|
|
fut_.set_callback([loop, self = self.release()]() {
|
|
loop->call_soon([self]() { delete self; });
|
|
});
|
|
}
|
|
|
|
}
|
|
|