-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathcoroutine.cc
327 lines (297 loc) · 9.01 KB
/
coroutine.cc
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "coroutine.h"
#include "v8-version.h"
#include <assert.h>
#ifndef WINDOWS
#include <pthread.h>
#else
#include <windows.h>
#include <intrin.h>
// Stub pthreads into Windows approximations
#define pthread_t HANDLE
#define pthread_create(thread, attr, fn, arg) !((*thread)=CreateThread(NULL, 0, &(fn), arg, 0, NULL))
#define pthread_join(thread, arg) WaitForSingleObject((thread), INFINITE)
#define pthread_key_t DWORD
#define pthread_key_create(key, dtor) (*key)=TlsAlloc()
#define pthread_setspecific(key, val) TlsSetValue((key), (val))
#define pthread_getspecific(key) TlsGetValue((key))
#endif
#include <stdexcept>
#include <stack>
#include <vector>
using namespace std;
const size_t v8_tls_keys = 3;
static std::vector<void*> fls_data_pool;
static pthread_key_t coro_thread_key = 0;
static pthread_key_t isolate_key = 0x7777;
static pthread_key_t thread_id_key = 0x7777;
static pthread_key_t thread_data_key = 0x7777;
static size_t stack_size = 0;
static size_t coroutines_created_ = 0;
static vector<Coroutine*> fiber_pool;
static Coroutine* delete_me = NULL;
size_t Coroutine::pool_size = 120;
static bool can_poke(void* addr) {
#ifdef WINDOWS
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(GetCurrentProcess(), addr, &mbi, sizeof(mbi))) {
return false;
}
if (!(mbi.State & MEM_COMMIT)) {
return false;
}
return true;
#else
// TODO?
return addr > (void*)0x1000;
#endif
}
#ifdef USE_V8_SYMBOLS
// ** This no longer works as of v8 7.3.262; `thread_id_key_` moved to a static variable inside a
// function in an anonymous namespace.
// Some distributions of node, most notably Ubuntu, strip the v8 internal symbols and so we don't
// have access to this stuff. In most cases we will use the more complicated `find_thread_id_key`
// below, since it tends to work on more platforms.
namespace v8 {
namespace base {
class Thread {
public: typedef int32_t LocalStorageKey;
};
}
namespace internal {
class Isolate {
public:
static base::Thread::LocalStorageKey isolate_key_;
static base::Thread::LocalStorageKey per_isolate_thread_data_key_;
static base::Thread::LocalStorageKey thread_id_key_;
};
}
}
#endif
#ifndef WINDOWS
static void* find_thread_id_key(void* arg)
#else
static DWORD __stdcall find_thread_id_key(LPVOID arg)
#endif
{
v8::Isolate* isolate = static_cast<v8::Isolate*>(arg);
assert(isolate != NULL);
v8::Locker locker(isolate);
isolate->Enter();
// First pass-- find isolate thread key
#ifdef __MUSL__
// 128 is default max key in musl
for (pthread_key_t ii = 1; ii < 128; ++ii) {
#else
for (pthread_key_t ii = coro_thread_key; ii > 0; --ii) {
#endif
void* tls = pthread_getspecific(ii - 1);
if (tls == isolate) {
isolate_key = ii - 1;
break;
}
}
assert(isolate_key != 0x7777);
// Second pass-- find data key
int thread_id = 0;
#ifdef __MUSL__
for (pthread_key_t ii = 0; ii < 128; ++ii) {
#else
for (pthread_key_t ii = isolate_key + 1; ii < coro_thread_key; ++ii) {
#endif
void* tls = pthread_getspecific(ii);
if (can_poke(tls) && *(void**)tls == isolate) {
// First member of per-thread data is the isolate
thread_data_key = ii;
// Second member is the thread id
thread_id = *(int*)((void**)tls + 1);
break;
}
}
assert(thread_data_key != 0x7777);
// Third pass-- find thread id key
#ifdef __MUSL__
for (pthread_key_t ii = 0; ii < 128; ++ii) {
#else
for (pthread_key_t ii = isolate_key + 1; ii < coro_thread_key; ++ii) {
#endif
int tls = static_cast<int>(reinterpret_cast<intptr_t>(pthread_getspecific(ii)));
if (tls == thread_id) {
thread_id_key = ii;
break;
}
}
assert(thread_id_key != 0x7777);
isolate->Exit();
return NULL;
}
/**
* Coroutine class definition
*/
void Coroutine::init(v8::Isolate* isolate) {
v8::Unlocker unlocker(isolate);
pthread_key_create(&coro_thread_key, NULL);
pthread_setspecific(coro_thread_key, ¤t());
#ifdef USE_V8_SYMBOLS
isolate_key = v8::internal::Isolate::isolate_key_;
thread_data_key = v8::internal::Isolate::per_isolate_thread_data_key_;
thread_id_key = v8::internal::Isolate::thread_id_key_;
#else
pthread_t thread;
pthread_create(&thread, NULL, find_thread_id_key, isolate);
pthread_join(thread, NULL);
#endif
}
Coroutine& Coroutine::current() {
Coroutine* current = static_cast<Coroutine*>(pthread_getspecific(coro_thread_key));
if (!current) {
current = new Coroutine;
pthread_setspecific(coro_thread_key, current);
}
return *current;
}
void Coroutine::set_stack_size(unsigned int size) {
assert(!stack_size);
stack_size = size;
}
size_t Coroutine::coroutines_created() {
return coroutines_created_;
}
void Coroutine::trampoline(void* that) {
#ifdef CORO_PTHREAD
pthread_setspecific(coro_thread_key, that);
#endif
#ifdef CORO_FIBER
// I can't figure out how to get the precise base of the stack in Windows. Since CreateFiber
// creates the stack automatically we don't have access to the base. We can however grab the
// current esp position, and use that as an approximation. Padding is added for safety since the
// base is slightly different.
static_cast<Coroutine*>(that)->stack_base = (size_t*)_AddressOfReturnAddress() - stack_size + 16;
#endif
if (!fls_data_pool.empty()) {
pthread_setspecific(thread_data_key, fls_data_pool.back());
pthread_setspecific(thread_id_key, fls_data_pool[fls_data_pool.size() - 2]);
pthread_setspecific(isolate_key, fls_data_pool[fls_data_pool.size() - 3]);
fls_data_pool.resize(fls_data_pool.size() - 3);
}
while (true) {
static_cast<Coroutine*>(that)->entry(const_cast<void*>(static_cast<Coroutine*>(that)->arg));
}
}
Coroutine::Coroutine() :
fls_data(v8_tls_keys),
entry(NULL),
arg(NULL) {
stack.sptr = NULL;
coro_create(&context, NULL, NULL, NULL, 0);
}
Coroutine::Coroutine(entry_t& entry, void* arg) :
fls_data(v8_tls_keys),
entry(entry),
arg(arg) {
}
Coroutine::~Coroutine() {
if (stack.sptr) {
coro_stack_free(&stack);
}
#ifdef CORO_FIBER
if (context.fiber)
#endif
(void)coro_destroy(&context);
}
Coroutine* Coroutine::create_fiber(entry_t* entry, void* arg) {
if (!fiber_pool.empty()) {
Coroutine* fiber = fiber_pool.back();
fiber_pool.pop_back();
fiber->reset(entry, arg);
return fiber;
}
Coroutine* coro = new Coroutine(*entry, arg);
if (!coro_stack_alloc(&coro->stack, stack_size)) {
delete coro;
return NULL;
}
coro_create(&coro->context, trampoline, coro, coro->stack.sptr, coro->stack.ssze);
#ifdef CORO_FIBER
// Stupid hack. libcoro's project structure combined with Windows's CreateFiber functions makes
// it difficult to catch this error. Sometimes Windows will return `ERROR_NOT_ENOUGH_MEMORY` or
// `ERROR_COMMITMENT_LIMIT` if it can't make any more fibers. However, `coro_stack_alloc` returns
// success unconditionally on Windows so we have to detect the error here, after the call to
// `coro_create`.
if (!coro->context.fiber) {
delete coro;
return NULL;
}
#endif
++coroutines_created_;
return coro;
}
void Coroutine::reset(entry_t* entry, void* arg) {
assert(entry != NULL);
this->entry = entry;
this->arg = arg;
}
void Coroutine::transfer(Coroutine& next) {
assert(this != &next);
#ifndef CORO_PTHREAD
fls_data[0] = pthread_getspecific(isolate_key);
fls_data[1] = pthread_getspecific(thread_id_key);
fls_data[2] = pthread_getspecific(thread_data_key);
pthread_setspecific(isolate_key, next.fls_data[0]);
pthread_setspecific(thread_id_key, next.fls_data[1]);
pthread_setspecific(thread_data_key, next.fls_data[2]);
pthread_setspecific(coro_thread_key, &next);
#endif
coro_transfer(&context, &next.context);
#ifndef CORO_PTHREAD
pthread_setspecific(coro_thread_key, this);
#endif
}
void Coroutine::run() {
Coroutine& current = Coroutine::current();
assert(!delete_me);
assert(¤t != this);
current.transfer(*this);
if (delete_me) {
// This means finish() was called on the coroutine and the pool was full so this coroutine needs
// to be deleted. We can't delete from inside finish(), because that would deallocate the
// current stack. However we CAN delete here, we just have to be very careful.
assert(delete_me == this);
assert(¤t != this);
delete_me = NULL;
delete this;
}
}
void Coroutine::finish(Coroutine& next, v8::Isolate* isolate) {
{
assert(&next != this);
assert(¤t() == this);
if (fiber_pool.size() < pool_size) {
fiber_pool.push_back(this);
} else {
#if V8_MAJOR_VERSION > 4 || (V8_MAJOR_VERSION == 4 && V8_MINOR_VERSION >= 10)
// Clean up isolate data
isolate->DiscardThreadSpecificMetadata();
#else
// If not supported, then we can mitigate v8's leakage by saving these thread locals.
fls_data_pool.reserve(fls_data_pool.size() + 3);
fls_data_pool.push_back(pthread_getspecific(isolate_key));
fls_data_pool.push_back(pthread_getspecific(thread_id_key));
fls_data_pool.push_back(pthread_getspecific(thread_data_key));
#endif
// Can't delete right now because we're currently on this stack!
assert(delete_me == NULL);
delete_me = this;
}
}
this->transfer(next);
}
void* Coroutine::bottom() const {
#ifdef CORO_FIBER
return stack_base;
#else
return stack.sptr;
#endif
}
size_t Coroutine::size() const {
return sizeof(Coroutine) + stack_size * sizeof(void*);
}