forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isolate_configuration.cc
294 lines (244 loc) · 9.53 KB
/
isolate_configuration.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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/runtime/isolate_configuration.h"
#include "flutter/fml/make_copyable.h"
#include "flutter/runtime/dart_vm.h"
namespace flutter {
IsolateConfiguration::IsolateConfiguration() = default;
IsolateConfiguration::~IsolateConfiguration() = default;
bool IsolateConfiguration::PrepareIsolate(DartIsolate& isolate) {
if (isolate.GetPhase() != DartIsolate::Phase::LibrariesSetup) {
FML_DLOG(ERROR)
<< "Isolate was in incorrect phase to be prepared for running.";
return false;
}
return DoPrepareIsolate(isolate);
}
class AppSnapshotIsolateConfiguration final : public IsolateConfiguration {
public:
AppSnapshotIsolateConfiguration() = default;
// |IsolateConfiguration|
bool DoPrepareIsolate(DartIsolate& isolate) override {
return isolate.PrepareForRunningFromPrecompiledCode();
}
// |IsolateConfiguration|
bool IsNullSafetyEnabled(const DartSnapshot& snapshot) override {
return snapshot.IsNullSafetyEnabled(nullptr);
}
private:
FML_DISALLOW_COPY_AND_ASSIGN(AppSnapshotIsolateConfiguration);
};
class KernelIsolateConfiguration : public IsolateConfiguration {
public:
explicit KernelIsolateConfiguration(
std::unique_ptr<const fml::Mapping> kernel)
: kernel_(std::move(kernel)) {}
// |IsolateConfiguration|
bool DoPrepareIsolate(DartIsolate& isolate) override {
if (DartVM::IsRunningPrecompiledCode()) {
return false;
}
return isolate.PrepareForRunningFromKernel(std::move(kernel_),
/*child_isolate=*/false,
/*last_piece=*/true);
}
// |IsolateConfiguration|
bool IsNullSafetyEnabled(const DartSnapshot& snapshot) override {
return snapshot.IsNullSafetyEnabled(kernel_.get());
}
private:
std::unique_ptr<const fml::Mapping> kernel_;
FML_DISALLOW_COPY_AND_ASSIGN(KernelIsolateConfiguration);
};
class KernelListIsolateConfiguration final : public IsolateConfiguration {
public:
explicit KernelListIsolateConfiguration(
std::vector<std::future<std::unique_ptr<const fml::Mapping>>>
kernel_pieces)
: kernel_piece_futures_(std::move(kernel_pieces)) {
if (kernel_piece_futures_.empty()) {
FML_LOG(ERROR) << "Attempted to create kernel list configuration without "
"any kernel blobs.";
}
}
// |IsolateConfiguration|
bool DoPrepareIsolate(DartIsolate& isolate) override {
if (DartVM::IsRunningPrecompiledCode()) {
return false;
}
ResolveKernelPiecesIfNecessary();
if (resolved_kernel_pieces_.empty()) {
FML_DLOG(ERROR) << "No kernel pieces provided to prepare this isolate.";
return false;
}
for (size_t i = 0; i < resolved_kernel_pieces_.size(); i++) {
if (!resolved_kernel_pieces_[i]) {
FML_DLOG(ERROR) << "This kernel list isolate configuration was already "
"used to prepare an isolate.";
return false;
}
const bool last_piece = i + 1 == resolved_kernel_pieces_.size();
if (!isolate.PrepareForRunningFromKernel(
std::move(resolved_kernel_pieces_[i]), /*child_isolate=*/false,
last_piece)) {
return false;
}
}
return true;
}
// |IsolateConfiguration|
bool IsNullSafetyEnabled(const DartSnapshot& snapshot) override {
ResolveKernelPiecesIfNecessary();
const auto kernel = resolved_kernel_pieces_.empty()
? nullptr
: resolved_kernel_pieces_.front().get();
return snapshot.IsNullSafetyEnabled(kernel);
}
// This must be call as late as possible before accessing any of the kernel
// pieces. This will delay blocking on the futures for as long as possible. So
// far, only Fuchsia depends on this optimization and only on the non-AOT
// configs.
void ResolveKernelPiecesIfNecessary() {
if (resolved_kernel_pieces_.size() == kernel_piece_futures_.size()) {
return;
}
resolved_kernel_pieces_.clear();
for (auto& piece : kernel_piece_futures_) {
// The get() call will xfer the unique pointer out and leave an empty
// future in the original vector.
resolved_kernel_pieces_.emplace_back(piece.get());
}
}
private:
std::vector<std::future<std::unique_ptr<const fml::Mapping>>>
kernel_piece_futures_;
std::vector<std::unique_ptr<const fml::Mapping>> resolved_kernel_pieces_;
FML_DISALLOW_COPY_AND_ASSIGN(KernelListIsolateConfiguration);
};
static std::vector<std::string> ParseKernelListPaths(
std::unique_ptr<fml::Mapping> kernel_list) {
FML_DCHECK(kernel_list);
std::vector<std::string> kernel_pieces_paths;
const char* kernel_list_str =
reinterpret_cast<const char*>(kernel_list->GetMapping());
size_t kernel_list_size = kernel_list->GetSize();
size_t piece_path_start = 0;
while (piece_path_start < kernel_list_size) {
size_t piece_path_end = piece_path_start;
while ((piece_path_end < kernel_list_size) &&
(kernel_list_str[piece_path_end] != '\n')) {
piece_path_end++;
}
std::string piece_path(&kernel_list_str[piece_path_start],
piece_path_end - piece_path_start);
kernel_pieces_paths.emplace_back(std::move(piece_path));
piece_path_start = piece_path_end + 1;
}
return kernel_pieces_paths;
}
static std::vector<std::future<std::unique_ptr<const fml::Mapping>>>
PrepareKernelMappings(const std::vector<std::string>& kernel_pieces_paths,
const std::shared_ptr<AssetManager>& asset_manager,
const fml::RefPtr<fml::TaskRunner>& io_worker) {
FML_DCHECK(asset_manager);
std::vector<std::future<std::unique_ptr<const fml::Mapping>>> fetch_futures;
for (const auto& kernel_pieces_path : kernel_pieces_paths) {
std::promise<std::unique_ptr<const fml::Mapping>> fetch_promise;
fetch_futures.push_back(fetch_promise.get_future());
auto fetch_task =
fml::MakeCopyable([asset_manager, kernel_pieces_path,
fetch_promise = std::move(fetch_promise)]() mutable {
fetch_promise.set_value(
asset_manager->GetAsMapping(kernel_pieces_path));
});
// Fulfill the promise on the worker if one is available or the current
// thread if one is not.
if (io_worker) {
io_worker->PostTask(fetch_task);
} else {
fetch_task();
}
}
return fetch_futures;
}
std::unique_ptr<IsolateConfiguration> IsolateConfiguration::InferFromSettings(
const Settings& settings,
const std::shared_ptr<AssetManager>& asset_manager,
const fml::RefPtr<fml::TaskRunner>& io_worker) {
// Running in AOT mode.
if (DartVM::IsRunningPrecompiledCode()) {
return CreateForAppSnapshot();
}
if (settings.application_kernels) {
return CreateForKernelList(settings.application_kernels());
}
if (settings.application_kernel_asset.empty() &&
settings.application_kernel_list_asset.empty()) {
FML_DLOG(ERROR) << "application_kernel_asset or "
"application_kernel_list_asset must be set";
return nullptr;
}
if (!asset_manager) {
FML_DLOG(ERROR) << "No asset manager specified when attempting to create "
"isolate configuration.";
return nullptr;
}
// Running from kernel snapshot. Requires asset manager.
{
std::unique_ptr<fml::Mapping> kernel =
asset_manager->GetAsMapping(settings.application_kernel_asset);
if (kernel) {
return CreateForKernel(std::move(kernel));
}
}
// Running from kernel divided into several pieces (for sharing). Requires
// asset manager and io worker.
if (!io_worker) {
FML_DLOG(ERROR) << "No IO worker specified to load kernel pieces.";
return nullptr;
}
{
std::unique_ptr<fml::Mapping> kernel_list =
asset_manager->GetAsMapping(settings.application_kernel_list_asset);
if (!kernel_list) {
FML_LOG(ERROR) << "Failed to load: "
<< settings.application_kernel_list_asset;
return nullptr;
}
auto kernel_pieces_paths = ParseKernelListPaths(std::move(kernel_list));
auto kernel_mappings =
PrepareKernelMappings(kernel_pieces_paths, asset_manager, io_worker);
return CreateForKernelList(std::move(kernel_mappings));
}
return nullptr;
}
std::unique_ptr<IsolateConfiguration>
IsolateConfiguration::CreateForAppSnapshot() {
return std::make_unique<AppSnapshotIsolateConfiguration>();
}
std::unique_ptr<IsolateConfiguration> IsolateConfiguration::CreateForKernel(
std::unique_ptr<const fml::Mapping> kernel) {
return std::make_unique<KernelIsolateConfiguration>(std::move(kernel));
}
std::unique_ptr<IsolateConfiguration> IsolateConfiguration::CreateForKernelList(
std::vector<std::unique_ptr<const fml::Mapping>> kernel_pieces) {
std::vector<std::future<std::unique_ptr<const fml::Mapping>>> pieces;
for (auto& piece : kernel_pieces) {
if (!piece) {
FML_DLOG(ERROR) << "Invalid kernel piece.";
continue;
}
std::promise<std::unique_ptr<const fml::Mapping>> promise;
pieces.push_back(promise.get_future());
promise.set_value(std::move(piece));
}
return CreateForKernelList(std::move(pieces));
}
std::unique_ptr<IsolateConfiguration> IsolateConfiguration::CreateForKernelList(
std::vector<std::future<std::unique_ptr<const fml::Mapping>>>
kernel_pieces) {
return std::make_unique<KernelListIsolateConfiguration>(
std::move(kernel_pieces));
}
} // namespace flutter