-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule.cpp
More file actions
143 lines (129 loc) · 4.59 KB
/
schedule.cpp
File metadata and controls
143 lines (129 loc) · 4.59 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
#include "schedule.h"
#include <string.h>
#include <stdlib.h>
namespace coroutine {
void coroutineFunc(uint32_t low32, uint32_t high32) {
uintptr_t ptr = static_cast<uintptr_t>(low32) | (static_cast<uintptr_t>(high32) << 32);
Schedule* schedule = reinterpret_cast<Schedule*>(ptr);
int id = schedule->getRunningCoroutineId();
const CoroutinePtr& coroutine = schedule->getCoroutineById(id);
if (coroutine) {
coroutine->start(schedule);
}
coroutine->setState(Coroutine::kDead);
schedule->deleteCoroutineById(id);
schedule->setRunningCoroutineId(-1);
}
const int Schedule::kStackSize;
Schedule::Schedule(int size)
: kCapacity_(size),
running_id_(-1),
flags_(size, 0) {
}
Schedule::~Schedule() {
assert(running_id_ == -1);
}
int Schedule::createCoroutine(const CoroutineFunc& func) {
if (coroutines_.size() < static_cast<size_t>(kCapacity_)) {
for (int i = 0; i < kCapacity_; ++i) {
int id = (static_cast<int>(coroutines_.size()) + i) % kCapacity_;
if (flags_[id] == 0) {
CoroutinePtr new_coroutine(new Coroutine(func, id));
flags_[id] = 1;
coroutines_[id] = new_coroutine;
return id;
}
}
assert(0);
return -1;
} else {
return -1;
}
}
void Schedule::runCoroutineById(int id) {
assert(running_id_ == -1);
assert(0 <= id && id < kCapacity_);
CoroutineMap::const_iterator it = coroutines_.find(id);
if (it != coroutines_.end()) {
const CoroutinePtr& coroutine = it->second;
if (coroutine) {
assert(id == coroutine->id());
Coroutine::State state = coroutine->state();
if (state == Coroutine::kReady) {
getcontext(coroutine->getContextMutable());
coroutine->getContextMutable()->uc_stack.ss_sp = stack_;
coroutine->getContextMutable()->uc_stack.ss_size = kStackSize;
coroutine->getContextMutable()->uc_link = &main_context_;
running_id_ = id;
coroutine->setState(Coroutine::kRunning);
uintptr_t ptr = reinterpret_cast<uintptr_t>(this);
makecontext(coroutine->getContextMutable(),
reinterpret_cast<void (*)()>(coroutineFunc),
2,
static_cast<uint32_t>(ptr),
static_cast<uint32_t>(ptr >> 32));
swapcontext(&main_context_, coroutine->getContextMutable());
} else if (state == Coroutine::kSuspend) {
memcpy(stack_ + kStackSize - coroutine->size(), coroutine->stack(), coroutine->size());
running_id_ = id;
coroutine->setState(Coroutine::kRunning);
swapcontext(&main_context_, coroutine->getContextMutable());
} else {
assert(0);
}
} else {
assert(0);
}
}
}
void Schedule::suspendCurrentCoroutine() {
assert(0 <= running_id_ && running_id_ < kCapacity_);
CoroutineMap::const_iterator it = coroutines_.find(running_id_);
if (it != coroutines_.end()) {
const CoroutinePtr& coroutine = it->second;
if (coroutine) {
assert(running_id_ == coroutine->id());
Coroutine* p = coroutine.get();
assert(reinterpret_cast<char*>(&p) > stack_);
(void)p;
coroutine->saveStack(stack_ + kStackSize);
coroutine->setState(Coroutine::kSuspend);
running_id_ = -1;
swapcontext(coroutine->getContextMutable(), &main_context_);
} else {
assert(0);
}
} else {
assert(0);
}
}
Coroutine::State Schedule::getCoroutineStateById(int id) const {
CoroutineMap::const_iterator it = coroutines_.find(id);
if (it != coroutines_.end()) {
const CoroutinePtr& coroutine = it->second;
if (coroutine) {
assert(id == coroutine->id());
return coroutine->state();
} else {
return Coroutine::kInvalid;
}
} else {
return Coroutine::kInvalid;
}
}
CoroutinePtr Schedule::getCoroutineById(int id) const {
CoroutineMap::const_iterator it = coroutines_.find(id);
if (it != coroutines_.end()) {
return it->second;
} else {
return CoroutinePtr();
}
}
void Schedule::deleteCoroutineById(int id) {
CoroutineMap::iterator it = coroutines_.find(id);
if (it != coroutines_.end()) {
coroutines_.erase(it);
flags_[id] = 0;
}
}
} // namespace coroutine