-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcoroutine.cc
More file actions
218 lines (164 loc) · 4.57 KB
/
coroutine.cc
File metadata and controls
218 lines (164 loc) · 4.57 KB
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
212
213
214
215
216
217
#include "coroutine.h"
#include <map>
#include <stdlib.h>
#include <assert.h>
#include <ucontext.h>
using namespace std;
struct coroutine
{
void* arg;
int status;
ucontext_t cxt;
uintptr_t yield;
CoroutineScheduler::CoFunc func;
char stack[0];
};
class CoroutineScheduler::SchedulerImpl
{
public:
explicit SchedulerImpl(int stacksize);
~SchedulerImpl();
int CreateCoroutine(CoroutineScheduler::CoFunc func, void* arg);
int DestroyCoroutine(int id);
uintptr_t Yield(uintptr_t);
uintptr_t ResumeCoroutine(int id, uintptr_t);
bool IsCoroutineAlive(int id) const;
private:
SchedulerImpl(const SchedulerImpl&);
SchedulerImpl& operator=(const SchedulerImpl&);
static void Schedule(void* arg);
private:
int index_;
int running_;
const int stacksize_;
ucontext_t mainContext_;
map<int, coroutine*> id2routine_;
coroutine** routine_;
};
CoroutineScheduler::SchedulerImpl::SchedulerImpl(int stacksize)
:index_(0), running_(-1), stacksize_(stacksize)
{
}
CoroutineScheduler::SchedulerImpl::~SchedulerImpl()
{
map<int, coroutine*>::iterator it = id2routine_.begin();
while (it != id2routine_.end())
{
if (it->second) free (it->second);
}
}
bool CoroutineScheduler::SchedulerImpl::IsCoroutineAlive(int id) const
{
map<int, coroutine*>::const_iterator it = id2routine_.find(id);
if (it == id2routine_.end()) return false;
return it->second;
}
int CoroutineScheduler::SchedulerImpl::DestroyCoroutine(int id)
{
coroutine* cor = id2routine_[id];
if (!cor) return -1;
free(cor);
id2routine_.erase(id);
return id;
}
// static function
void CoroutineScheduler::SchedulerImpl::Schedule(void* arg)
{
assert(arg);
SchedulerImpl* sched = (SchedulerImpl*) arg;
int running = sched->running_;
coroutine* cor = sched->id2routine_[running];
assert(cor);
cor->yield = cor->func(cor->arg);
sched->running_ = -1;
cor->status = CO_FINISHED;
}
int CoroutineScheduler::SchedulerImpl::CreateCoroutine(CoroutineScheduler::CoFunc func, void* arg)
{
coroutine* cor = (coroutine*)malloc(sizeof(coroutine) + stacksize_);
if (cor == NULL) return -1;
cor->arg = arg;
cor->func = func;
cor->yield = 0;
cor->status = CO_READY;
int index = index_++;
id2routine_[index] = cor;
return index;
}
uintptr_t CoroutineScheduler::SchedulerImpl::ResumeCoroutine(int id, uintptr_t y)
{
coroutine* cor = id2routine_[id];
if (cor == NULL || cor->status == CO_RUNNING) return 0;
cor->yield = y;
switch (cor->status)
{
case CO_READY:
{
getcontext(&cor->cxt);
cor->status = CO_RUNNING;
cor->cxt.uc_stack.ss_sp = cor->stack;
cor->cxt.uc_stack.ss_size = stacksize_;
// sucessor context.
cor->cxt.uc_link = &mainContext_;
running_ = id;
// setup coroutine context
makecontext(&cor->cxt, (void (*)())Schedule, 1, this);
swapcontext(&mainContext_, &cor->cxt);
}
break;
case CO_SUSPENDED:
{
running_ = id;
cor->status = CO_RUNNING;
swapcontext(&mainContext_, &cor->cxt);
}
break;
default:
assert(0);
}
uintptr_t ret = cor->yield;
cor->yield = 0;
if (running_ == -1 && cor->status == CO_FINISHED) DestroyCoroutine(id);
return ret;
}
uintptr_t CoroutineScheduler::SchedulerImpl::Yield(uintptr_t y)
{
if (running_ < 0) return 0;
int cur = running_;
running_ = -1;
coroutine* cor = id2routine_[cur];
cor->yield = y;
cor->status = CO_SUSPENDED;
swapcontext(&cor->cxt, &mainContext_);
uintptr_t ret = cor->yield;
cor->yield = 0;
return ret;
}
CoroutineScheduler::CoroutineScheduler(int stacksize)
:impl_(new SchedulerImpl(stacksize))
{
}
CoroutineScheduler::~CoroutineScheduler()
{
delete impl_;
}
int CoroutineScheduler::CreateCoroutine(CoFunc func, void* arg)
{
return impl_->CreateCoroutine(func, arg);
}
int CoroutineScheduler::DestroyCoroutine(int id)
{
return impl_->DestroyCoroutine(id);
}
uintptr_t CoroutineScheduler::ResumeCoroutine(int id, uintptr_t y)
{
return impl_->ResumeCoroutine(id, y);
}
uintptr_t CoroutineScheduler::Yield(uintptr_t y)
{
return impl_->Yield(y);
}
bool CoroutineScheduler::IsCoroutineAlive(int id) const
{
return impl_->IsCoroutineAlive(id);
}