Repository navigation
Expand file tree
/
Copy pathGraphLoadInternal.h
More file actions
184 lines (159 loc) · 6.73 KB
/
Copy pathGraphLoadInternal.h
File metadata and controls
184 lines (159 loc) · 6.73 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
#ifndef NATIVESCRIPT_COMMON_LOADER_GRAPH_LOAD_INTERNAL_H_
#define NATIVESCRIPT_COMMON_LOADER_GRAPH_LOAD_INTERNAL_H_
#include <v8.h>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
#include "loader/CompilePool.h"
#include "ns/RuntimeLiveness.h"
#include "ns/loader/CodeCache.h"
// The graph walk's per-isolate state.
//
// ScriptStreamingTask::Run() and ConsumeCodeCacheTask::Run() work on the
// isolate's heap from another thread, so the isolate must outlive every Run(),
// and a task that has run must be destroyed on the isolate's thread before
// the isolate is disposed. Hence:
// - every V8 task and StreamedSource is owned here, in GraphLoaderState, and
// is created, finalized and destroyed on the isolate's thread;
// - background threads hold tickets and jobs that own no V8 state, and reach
// the tasks through raw pointers only inside a RuntimeLiveness section;
// - walks and jobs own no V8 state either, so their last reference may drop
// on any thread; a walk's context lives in `walkContexts`.
namespace nativescript::common::loader {
// The whole body in one chunk. Feed() and the GetMoreData() calls Run() makes
// happen on the same thread, Feed() first. V8 takes the chunk and delete[]s
// it.
class BodySourceStream : public v8::ScriptCompiler::ExternalSourceStream {
public:
void Feed(std::unique_ptr<uint8_t[]> chunk, size_t length) {
chunk_ = std::move(chunk);
length_ = length;
}
size_t GetMoreData(const uint8_t** src) override {
if (chunk_ == nullptr) {
return 0;
}
*src = chunk_.release();
return length_;
}
private:
std::unique_ptr<uint8_t[]> chunk_;
size_t length_ = 0;
};
// A streamed compile of an HTTP body.
struct StreamedModuleCompile {
// Owned by `source`.
BodySourceStream* stream = nullptr;
// Declared before `task`, which points into it.
std::unique_ptr<v8::ScriptCompiler::StreamedSource> source;
std::unique_ptr<v8::ScriptCompiler::ScriptStreamingTask> task;
};
// The background threads' view of one StreamedModuleCompile. `stream` and
// `task` may be used only inside a liveness section. `ran` and `ascii` are
// written before the completion is posted and read after it runs.
struct StreamTicket {
uint64_t id = 0;
size_t stackBytes = 0;
BodySourceStream* stream = nullptr;
v8::ScriptCompiler::ScriptStreamingTask* task = nullptr;
bool ran = false;
bool ascii = false;
};
// A file job's V8 side: a streamed compile when the file has no fresh code
// cache, otherwise the cache consumption the pool creates and runs.
struct FileModuleCompile {
// Owned by `source`.
BodySourceStream* stream = nullptr;
// Declared before `streamTask`, which points into it.
std::unique_ptr<v8::ScriptCompiler::StreamedSource> source;
std::unique_ptr<v8::ScriptCompiler::ScriptStreamingTask> streamTask;
std::unique_ptr<v8::ScriptCompiler::ConsumeCodeCacheTask> consumeTask;
};
struct AsyncGraphLoad;
// One file module loading on the compile pool. The pool fills the result
// fields, sets `ready` and then posts the completion; the isolate's thread
// reads them only after `ready` reads true.
struct FileModuleJob {
enum class Mode { kStream, kConsume };
uint64_t id = 0;
std::shared_ptr<RuntimeLiveness> liveness;
std::shared_ptr<FileJobSignal> signal;
Mode mode = Mode::kStream;
std::string path;
std::string key;
std::string query;
// The code cache as of the dispatch-time Stat; nullopt when caching is off.
// kConsume: it was fresh at dispatch.
std::optional<CodeCacheEntry> cache;
// kConsume: the file still matched the dispatch-time Stat after `body` was
// read, so `cache` describes `body`. V8 checks a cache against the source's
// length alone.
bool cacheMatchesBody = false;
// Valid inside the pool's liveness section only.
FileModuleCompile* compile = nullptr;
// Pool results.
std::string body;
bool readOk = false;
bool ascii = false;
// The streamed compile or the cache consumption ran; without it the
// isolate's thread compiles `body` itself.
bool ran = false;
std::atomic<bool> ready{false};
// Isolate thread only. Walks waiting for this module, each holding one
// pendingFetches count for it. Strong: nothing else keeps a walk alive while
// only file loads are outstanding.
std::vector<std::shared_ptr<AsyncGraphLoad>> waiters;
// The result has been registered or dropped.
bool finalized = false;
// Its completion ran without a main context and left the result here: a
// walk that reaches the key finalizes it instead of waiting.
bool delivered = false;
// Removed from the registry while loading: the result is dropped.
bool superseded = false;
};
struct GraphLoaderState {
// Null until LivenessFor first finds a loop.
std::shared_ptr<RuntimeLiveness> liveness;
std::shared_ptr<FileJobSignal> fileJobSignal =
std::make_shared<FileJobSignal>();
// From the installed config's v8Flags, on first use.
bool compileOptionsRead = false;
bool eagerCompile = false;
size_t compileStackBytes = 0;
// Running walks. Weak: a finished walk frees itself.
std::vector<std::weak_ptr<AsyncGraphLoad>> loads;
// Each running walk's context, by its sequence number.
std::unordered_map<uint64_t, v8::Global<v8::Context>> walkContexts;
// The exception a running walk's root failed with (a JSON root's
// SyntaxError), for its completion, by sequence number.
std::unordered_map<uint64_t, v8::Global<v8::Value>> rootExceptions;
std::unordered_map<uint64_t, std::unique_ptr<StreamedModuleCompile>>
streamedCompiles;
uint64_t nextStreamedCompileId = 1;
// File jobs in flight, by registry key, and their V8 side by job id.
std::unordered_map<std::string, std::shared_ptr<FileModuleJob>> fileJobs;
std::unordered_map<uint64_t, std::unique_ptr<FileModuleCompile>> fileCompiles;
uint64_t nextFileJobId = 1;
// SyncModuleLoadScopes open, and the continuations of walks that started
// before the outermost one and whose fetch or file load landed inside it.
int syncLoadDepth = 0;
uint64_t nextWalkSeq = 1;
uint64_t parkWalksBefore = 0;
std::vector<std::function<void()>> parkedWalks;
};
struct ModuleLoaderState;
// The registry dropped `key`: a file job loading it registers nothing when it
// lands.
void SupersedeFileJob(GraphLoaderState& graph, const std::string& key);
// loader::Quiesce's graph half, on the isolate's thread under its Locker:
// stops every walk and file job, closes the liveness token, waits out every
// pool section, and destroys the V8 tasks that will never be finalized.
void QuiesceGraphLoads(v8::Isolate* isolate, ModuleLoaderState& state);
} // namespace nativescript::common::loader
#endif // NATIVESCRIPT_COMMON_LOADER_GRAPH_LOAD_INTERNAL_H_