forked from microsoft/react-native-macos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimeScheduler_Legacy.cpp
More file actions
289 lines (235 loc) · 8.36 KB
/
Copy pathRuntimeScheduler_Legacy.cpp
File metadata and controls
289 lines (235 loc) · 8.36 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
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "RuntimeScheduler_Legacy.h"
#ifndef RCT_REMOVE_LEGACY_ARCH
#include "SchedulerPriorityUtils.h"
#include <ReactCommon/RuntimeExecutorSyncUIThreadUtils.h>
#include <cxxreact/TraceSection.h>
#include <react/renderer/consistency/ScopedShadowTreeRevisionLock.h>
#include <utility>
namespace facebook::react {
#pragma mark - Public
RuntimeScheduler_Legacy::RuntimeScheduler_Legacy(
RuntimeExecutor runtimeExecutor,
std::function<HighResTimeStamp()> now,
RuntimeSchedulerTaskErrorHandler onTaskError)
: runtimeExecutor_(std::move(runtimeExecutor)),
now_(std::move(now)),
onTaskError_(std::move(onTaskError)) {}
void RuntimeScheduler_Legacy::scheduleWork(RawCallback&& callback) noexcept {
TraceSection s("RuntimeScheduler::scheduleWork");
runtimeAccessRequests_ += 1;
runtimeExecutor_(
[this, callback = std::move(callback)](jsi::Runtime& runtime) {
TraceSection s2("RuntimeScheduler::scheduleWork callback");
runtimeAccessRequests_ -= 1;
{
ScopedShadowTreeRevisionLock revisionLock(
shadowTreeRevisionConsistencyManager_);
callback(runtime);
}
startWorkLoop(runtime);
});
}
std::shared_ptr<Task> RuntimeScheduler_Legacy::scheduleTask(
SchedulerPriority priority,
jsi::Function&& callback) noexcept {
TraceSection s(
"RuntimeScheduler::scheduleTask",
"priority",
serialize(priority),
"callbackType",
"jsi::Function");
auto expirationTime = now_() + timeoutForSchedulerPriority(priority);
auto task =
std::make_shared<Task>(priority, std::move(callback), expirationTime);
taskQueue_.push(task);
scheduleWorkLoopIfNecessary();
return task;
}
std::shared_ptr<Task> RuntimeScheduler_Legacy::scheduleTask(
SchedulerPriority priority,
RawCallback&& callback) noexcept {
TraceSection s(
"RuntimeScheduler::scheduleTask",
"priority",
serialize(priority),
"callbackType",
"RawCallback");
auto expirationTime = now_() + timeoutForSchedulerPriority(priority);
auto task =
std::make_shared<Task>(priority, std::move(callback), expirationTime);
taskQueue_.push(task);
scheduleWorkLoopIfNecessary();
return task;
}
std::shared_ptr<Task> RuntimeScheduler_Legacy::scheduleIdleTask(
jsi::Function&& /*callback*/,
HighResDuration /*timeout*/) noexcept {
// Idle tasks are not supported on Legacy RuntimeScheduler.
// Because the method is `noexcept`, we return `nullptr` here and handle it
// on the caller side.
return nullptr;
}
std::shared_ptr<Task> RuntimeScheduler_Legacy::scheduleIdleTask(
RawCallback&& /*callback*/,
HighResDuration /*timeout*/) noexcept {
// Idle tasks are not supported on Legacy RuntimeScheduler.
// Because the method is `noexcept`, we return `nullptr` here and handle it
// on the caller side.
return nullptr;
}
bool RuntimeScheduler_Legacy::getShouldYield() noexcept {
return runtimeAccessRequests_ > 0;
}
void RuntimeScheduler_Legacy::cancelTask(Task& task) noexcept {
task.callback.reset();
}
SchedulerPriority RuntimeScheduler_Legacy::getCurrentPriorityLevel()
const noexcept {
return currentPriority_;
}
HighResTimeStamp RuntimeScheduler_Legacy::now() const noexcept {
return now_();
}
void RuntimeScheduler_Legacy::executeNowOnTheSameThread(
RawCallback&& callback) {
TraceSection s("RuntimeScheduler::executeNowOnTheSameThread");
static thread_local jsi::Runtime* runtimePtr = nullptr;
if (runtimePtr == nullptr) {
runtimeAccessRequests_ += 1;
executeSynchronouslyOnSameThread_CAN_DEADLOCK(
runtimeExecutor_, [this, &callback](jsi::Runtime& runtime) {
TraceSection s2(
"RuntimeScheduler::executeNowOnTheSameThread callback");
runtimeAccessRequests_ -= 1;
{
ScopedShadowTreeRevisionLock revisionLock(
shadowTreeRevisionConsistencyManager_);
runtimePtr = &runtime;
callback(runtime);
runtimePtr = nullptr;
}
});
} else {
// Protecting against re-entry into `executeNowOnTheSameThread` from within
// `executeNowOnTheSameThread`. Without accounting for re-rentry, a deadlock
// will occur when trying to gain access to the runtime.
return callback(*runtimePtr);
}
// Resume work loop if needed. In synchronous mode
// only expired tasks are executed. Tasks with lower priority
// might be still in the queue.
scheduleWorkLoopIfNecessary();
}
void RuntimeScheduler_Legacy::callExpiredTasks(jsi::Runtime& runtime) {
TraceSection s("RuntimeScheduler::callExpiredTasks");
auto previousPriority = currentPriority_;
try {
while (!taskQueue_.empty()) {
auto topPriorityTask = taskQueue_.top();
auto now = now_();
auto didUserCallbackTimeout = topPriorityTask->expirationTime <= now;
if (!didUserCallbackTimeout) {
break;
}
executeTask(runtime, topPriorityTask, didUserCallbackTimeout);
}
} catch (jsi::JSError& error) {
onTaskError_(runtime, error);
} catch (std::exception& ex) {
jsi::JSError error(runtime, std::string("Non-js exception: ") + ex.what());
onTaskError_(runtime, error);
}
currentPriority_ = previousPriority;
}
void RuntimeScheduler_Legacy::scheduleRenderingUpdate(
SurfaceId /*surfaceId*/,
RuntimeSchedulerRenderingUpdate&& renderingUpdate) {
TraceSection s("RuntimeScheduler::scheduleRenderingUpdate");
if (renderingUpdate != nullptr) {
renderingUpdate();
}
}
void RuntimeScheduler_Legacy::setShadowTreeRevisionConsistencyManager(
ShadowTreeRevisionConsistencyManager*
shadowTreeRevisionConsistencyManager) {
shadowTreeRevisionConsistencyManager_ = shadowTreeRevisionConsistencyManager;
}
void RuntimeScheduler_Legacy::setPerformanceEntryReporter(
PerformanceEntryReporter* /*performanceEntryReporter*/) {
// No-op in the legacy scheduler
}
void RuntimeScheduler_Legacy::setEventTimingDelegate(
RuntimeSchedulerEventTimingDelegate* /*eventTimingDelegate*/) {
// No-op in the legacy scheduler
}
void RuntimeScheduler_Legacy::setIntersectionObserverDelegate(
RuntimeSchedulerIntersectionObserverDelegate*
/*intersectionObserverDelegate*/) {
// No-op in the legacy scheduler
}
#pragma mark - Private
void RuntimeScheduler_Legacy::scheduleWorkLoopIfNecessary() {
if (!isWorkLoopScheduled_ && !isPerformingWork_) {
isWorkLoopScheduled_ = true;
runtimeExecutor_([this](jsi::Runtime& runtime) {
isWorkLoopScheduled_ = false;
startWorkLoop(runtime);
});
}
}
void RuntimeScheduler_Legacy::startWorkLoop(jsi::Runtime& runtime) {
TraceSection s("RuntimeScheduler::startWorkLoop");
auto previousPriority = currentPriority_;
isPerformingWork_ = true;
try {
while (!taskQueue_.empty()) {
auto topPriorityTask = taskQueue_.top();
auto now = now_();
auto didUserCallbackTimeout = topPriorityTask->expirationTime <= now;
if (!didUserCallbackTimeout && getShouldYield()) {
// This currentTask hasn't expired, and we need to yield.
break;
}
executeTask(runtime, topPriorityTask, didUserCallbackTimeout);
}
} catch (jsi::JSError& error) {
onTaskError_(runtime, error);
} catch (std::exception& ex) {
jsi::JSError error(runtime, std::string("Non-js exception: ") + ex.what());
onTaskError_(runtime, error);
}
currentPriority_ = previousPriority;
isPerformingWork_ = false;
}
void RuntimeScheduler_Legacy::executeTask(
jsi::Runtime& runtime,
const std::shared_ptr<Task>& task,
bool didUserCallbackTimeout) {
TraceSection s(
"RuntimeScheduler::executeTask",
"priority",
serialize(task->priority),
"didUserCallbackTimeout",
didUserCallbackTimeout);
currentPriority_ = task->priority;
{
ScopedShadowTreeRevisionLock revisionLock(
shadowTreeRevisionConsistencyManager_);
auto result = task->execute(runtime, didUserCallbackTimeout);
if (result.isObject() && result.getObject(runtime).isFunction(runtime)) {
task->callback = result.getObject(runtime).getFunction(runtime);
} else {
if (taskQueue_.top() == task) {
taskQueue_.pop();
}
}
}
}
} // namespace facebook::react
#endif // RCT_REMOVE_LEGACY_ARCH