-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfastcoroutine.h
228 lines (186 loc) · 5.05 KB
/
fastcoroutine.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2013 A. Douglas Gale
// Permission is granted to use the fastcoroutine implementation
// for any use (including commercial use), provided this copyright
// notice is present in your product's source code.
#include <cstdint>
#include <stdexcept>
#include <functional>
namespace FastCoroutine
{
struct SavedContextFrame;
class Task
{
enum State
{
INVALID,
ROOT,
CREATED,
EXITED,
};
SavedContextFrame *rsp;
Task *owner;
void *stack;
size_t stacksize;
State state;
public:
Task(Task *owner);
~Task();
private:
void GetTibStackRange();
void SetTibStackRange();
public:
void CreateContext(void (*f)(void*,void*,void*,void*),
void *a0 = nullptr, void *a1 = nullptr,
void *a2 = nullptr, void *a3 = nullptr);
void SwitchTo(Task &outgoing_task);
};
class CoroutineCanceled
: public std::exception
{
public:
CoroutineCanceled();
const char *what();
};
template<typename Y>
class Enumerator;
// Holds the data being yielded while doing context switch
template<typename Y>
class YieldBuffer
{
Enumerator<Y> &owner;
Y buffer;
friend class Enumerator<Y>;
YieldBuffer(Enumerator<Y> &owner);
public:
// YieldReturn is called from the Enumerator to deliver
// a result and switch context back to the caller
template<typename R>
typename std::enable_if<!std::is_convertible<R,Y>::value>::type
YieldReturn(R &&result) = delete;
template<typename R>
typename std::enable_if<std::is_convertible<R,Y>::value>::type
YieldReturn(R &&result);
};
// Enumerate objects of type Y by invoking Troutine as a coroutine
template<typename Y>
class Enumerator
{
private:
typedef typename std::function<void(YieldBuffer<Y>&)> RoutineType;
YieldBuffer<Y> buffer;
RoutineType routine;
bool started, done, cancel;
Task workerTask, selfTask;
// Thunk converts C interface back to object reference
static void StartupThunk(void *a, void *, void *, void *);
template<typename R>
Enumerator(R &&routine,
typename std::enable_if<
!std::is_convertible<R,RoutineType>::value
>::type * = nullptr)
= delete;
public:
template<typename R>
Enumerator(R &&routine,
typename std::enable_if<
std::is_convertible<R,RoutineType>::value
>::type * = nullptr);
~Enumerator();
void ReturnToOwner();
void ReturnToCoroutine();
// Returns true if the coroutine is NOT finished
bool Next();
Y &GetYield();
};
}
// ============================================================================
// YieldBuffer<T>
// ============================================================================
template<typename Y>
FastCoroutine::YieldBuffer<Y>::YieldBuffer(Enumerator<Y> &owner)
: owner(owner)
{
}
template<typename Y> template<typename R>
typename std::enable_if<std::is_convertible<R,Y>::value>::type
FastCoroutine::YieldBuffer<Y>::YieldReturn(R &&result)
{
buffer = std::forward<R>(result);
owner.ReturnToOwner();
}
// ============================================================================
// Enumerator<Y>
// ============================================================================
// Thunk converts C interface back to object reference
template<typename Y>
void FastCoroutine::Enumerator<Y>::StartupThunk(void *a, void *, void *, void *)
{
Enumerator &self = *reinterpret_cast<Enumerator*>(a);
try
{
self.routine(self.buffer);
}
catch (CoroutineCanceled)
{
}
self.done = true;
self.ReturnToOwner();
}
template<typename Y>
template<typename R>
FastCoroutine::Enumerator<Y>::Enumerator(R &&routine,
typename std::enable_if<
std::is_convertible<R,RoutineType>::value
>::type *)
: buffer(*this)
, routine(std::forward<R>(routine))
, started(false)
, done(false)
, cancel(false)
, workerTask(0)
, selfTask(&workerTask)
{
// Create a task for the routine
// We pass a function pointer to the compiler generated thunk for this yield type
workerTask.CreateContext(StartupThunk, this, nullptr, nullptr, nullptr);
}
template<typename Y>
FastCoroutine::Enumerator<Y>::~Enumerator()
{
if (!done && started)
{
// Need to force coroutine to exit
// Setting the cancel flag causes an exception to be thrown
// in the coroutine context.
cancel = true;
// The CoroutineCanceled exception is caught and execution is
// resumed and the ReturnToCoroutine returns
ReturnToCoroutine();
}
}
template<typename Y>
void FastCoroutine::Enumerator<Y>::ReturnToOwner()
{
selfTask.SwitchTo(workerTask);
if (cancel)
throw CoroutineCanceled();
}
template<typename Y>
void FastCoroutine::Enumerator<Y>::ReturnToCoroutine()
{
workerTask.SwitchTo(selfTask);
}
// Returns true if the coroutine is NOT finished
template<typename Y>
bool FastCoroutine::Enumerator<Y>::Next()
{
// Switch to the coroutine and execute
// until it yields something or returns
ReturnToCoroutine();
return !done;
}
template<typename Y>
Y &FastCoroutine::Enumerator<Y>::GetYield()
{
return buffer.buffer;
}