-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiber.h
More file actions
71 lines (57 loc) · 1.53 KB
/
fiber.h
File metadata and controls
71 lines (57 loc) · 1.53 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
#ifndef FIBER_H
#define FIBER_H
#ifdef _WIN32
// FIX: Include winsock2.h before windows.h to prevent redefinition warnings.
#include <winsock2.h>
#include <windows.h>
#else
#define _XOPEN_SOURCE
#include <ucontext.h>
#endif
#include "v8-persistent-handle.h"
#include "v8.h"
#include "uv.h"
#ifdef _WIN32
using platform_context_t = LPVOID;
#else
using platform_context_t = ucontext_t*;
#endif
class Fiber {
public:
enum State { NEW, RUNNING, SUSPENDED, DONE };
static void init(v8::Isolate* isolate, uv_loop_t* loop);
static void set_main_context(v8::Local<v8::Context> context);
static Fiber* get_current();
static uv_loop_t* get_loop();
Fiber(v8::Isolate* isolate, v8::Local<v8::Function> func);
~Fiber();
static void yield();
static void resume(Fiber* fiber);
State state() const { return state_; }
v8::Isolate* isolate() const { return isolate_; }
v8::Persistent<v8::Value> resume_value;
v8::Persistent<v8::Object> js_object;
v8::Local<v8::Context> context() const { return v8_context_.Get(isolate_); }
private:
Fiber(v8::Isolate* isolate);
void run();
#ifdef _WIN32
static VOID CALLBACK fiber_entry(PVOID lpParameter);
#else
static void fiber_entry();
#endif
v8::Isolate* isolate_;
v8::Persistent<v8::Function> func_;
v8::Persistent<v8::Context> v8_context_;
State state_;
platform_context_t context_;
#ifndef _WIN32
ucontext_t uctx_;
char* stack_ = nullptr;
#endif
static Fiber* current_fiber_;
static Fiber* main_fiber_;
static uv_loop_t* event_loop_;
static const int STACK_SIZE = 1024 * 1024;
};
#endif // FIBER_H