-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoroutine.cpp
More file actions
42 lines (35 loc) · 928 Bytes
/
coroutine.cpp
File metadata and controls
42 lines (35 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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; });
});
}
}