-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoroutine.h
211 lines (176 loc) · 3.69 KB
/
coroutine.h
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*********************************************************
File Name: coroutine.h
Author: Abby Cin
Mail: [email protected]
Created Time: Sun 03 Jun 2018 10:24:50 AM CST
**********************************************************/
#ifndef CO_ROUTINE_H_
#define CO_ROUTINE_H_
#include <cstdlib>
#include <cstring>
#include <optional>
#include "../meta/function_traits.h"
namespace nm
{
extern "C" {
void *switch_stack(void *obj, void *context, void *arg);
}
template<typename T>
class Coroutine;
template<typename T>
void co_call_member_function(Coroutine<T> *obj, void *ctx);
template<typename T>
class Coroutine {
public:
template<typename Type>
friend void co_call_member_function(Coroutine<Type> *, void *);
enum {
Fixed_Stack_Size = 8 * 1024
};
class Iterator {
template<typename Type>
friend class Coroutine;
public:
~Iterator()
{
}
Iterator(const Iterator &) = delete;
Iterator &operator=(const Iterator &) = delete;
Iterator(Iterator &&rhs) : co_ { rhs.co_ }
{
rhs.co_ = nullptr;
}
Iterator &operator=(Iterator &&rhs)
{
this->co_ = rhs.co_;
rhs.co_ = nullptr;
return *this;
}
bool operator==(const Iterator &rhs)
{
return this->co_ == rhs.co_;
}
bool operator!=(const Iterator &rhs)
{
return this->co_ != rhs.co_;
}
Iterator &operator++()
{
co_->resume();
if (co_->done()) {
co_ = nullptr;
}
return *this;
}
T &operator*()
{
return co_->data();
}
private:
Coroutine<T> *co_;
Iterator() : co_ { nullptr }
{
}
Iterator(Coroutine *co) : co_ { co }
{
}
};
Coroutine() = default;
~Coroutine()
{
free(stack_);
stack_ = nullptr;
if (deleter_) {
deleter_(arg_);
arg_ = nullptr;
}
}
Coroutine(const Coroutine &) = delete;
Coroutine(Coroutine &&) = delete;
Coroutine &operator=(const Coroutine &) = delete;
Coroutine &operator=(Coroutine &&) = delete;
template<typename F, typename... Args>
void start(F &&f, Args &&...args)
{
this->~Coroutine();
stack_ = malloc(Fixed_Stack_Size);
memset(stack_, 0, Fixed_Stack_Size);
// call pop in `switch_stack` will grow rsp, here I reserve 4k
// space for push and I don't check alignment, you can adjust by
// using std::align since C++11
new_sp_ = (char *)stack_ + 4096;
this->done_ = false;
using arg_t = typename meta::Inspector<F>::arg_t;
arg_ = new arg_t { std::forward<Args>(args)... };
deleter_ = [](void *data)
{ delete static_cast<arg_t *>(data); };
fp_ = [f = std::forward<F>(f)](void *arg)
{
arg_t *param = static_cast<arg_t *>(arg);
std::apply(f, *param);
};
// skip 6 registers r12~r15 and rbx rbp, assign next instruction
// to rip
*((void **)((char *)new_sp_ + 6 * 8)) =
(void *)co_call_member_function<T>;
new_sp_ = switch_stack(this, new_sp_, arg_);
}
void yield(const T &t)
{
data_ = t;
this->yield();
}
void yield()
{
new_sp_ = switch_stack(nullptr, new_sp_, nullptr);
}
Iterator begin()
{
if (new_sp_) {
this->resume();
if (this->done()) {
return { nullptr };
}
}
return { this };
}
Iterator end()
{
return { nullptr };
}
private:
void *new_sp_ { nullptr };
void *stack_ { nullptr };
bool done_ { false };
std::function<void(void *)> fp_;
std::function<void(void *)> deleter_;
void *arg_ { nullptr };
std::optional<T> data_ {};
bool done()
{
return done_;
}
T &data()
{
return data_.value(); // let it throw
}
void resume()
{
new_sp_ = switch_stack(nullptr, new_sp_, nullptr);
}
void agent(void *ctx)
{
new_sp_ = switch_stack(nullptr, ctx, nullptr);
fp_(arg_);
done_ = true;
data_.reset();
this->yield();
}
};
template<typename T>
void co_call_member_function(Coroutine<T> *obj, void *ctx)
{
obj->agent(ctx);
}
}
#endif