|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | #include "RuntimeScheduler.h" |
| 9 | +#include "RuntimeScheduler_Legacy.h" |
9 | 10 | #include "SchedulerPriorityUtils.h" |
10 | 11 |
|
11 | 12 | #include <react/renderer/debug/SystraceSection.h> |
|
14 | 15 |
|
15 | 16 | namespace facebook::react { |
16 | 17 |
|
17 | | -#pragma mark - Public |
18 | | - |
19 | 18 | RuntimeScheduler::RuntimeScheduler( |
20 | 19 | RuntimeExecutor runtimeExecutor, |
21 | 20 | std::function<RuntimeSchedulerTimePoint()> now) |
22 | | - : runtimeExecutor_(std::move(runtimeExecutor)), now_(std::move(now)) {} |
| 21 | + : runtimeSchedulerImpl_(std::make_unique<RuntimeScheduler_Legacy>( |
| 22 | + std::move(runtimeExecutor), |
| 23 | + std::move(now))) {} |
23 | 24 |
|
24 | 25 | void RuntimeScheduler::scheduleWork(RawCallback&& callback) const noexcept { |
25 | | - SystraceSection s("RuntimeScheduler::scheduleWork"); |
26 | | - |
27 | | - runtimeAccessRequests_ += 1; |
28 | | - |
29 | | - runtimeExecutor_( |
30 | | - [this, callback = std::move(callback)](jsi::Runtime& runtime) { |
31 | | - SystraceSection s2("RuntimeScheduler::scheduleWork callback"); |
32 | | - runtimeAccessRequests_ -= 1; |
33 | | - callback(runtime); |
34 | | - startWorkLoop(runtime); |
35 | | - }); |
| 26 | + return runtimeSchedulerImpl_->scheduleWork(std::move(callback)); |
36 | 27 | } |
37 | 28 |
|
38 | 29 | std::shared_ptr<Task> RuntimeScheduler::scheduleTask( |
39 | 30 | SchedulerPriority priority, |
40 | 31 | jsi::Function&& callback) noexcept { |
41 | | - auto expirationTime = now_() + timeoutForSchedulerPriority(priority); |
42 | | - auto task = |
43 | | - std::make_shared<Task>(priority, std::move(callback), expirationTime); |
44 | | - taskQueue_.push(task); |
45 | | - |
46 | | - scheduleWorkLoopIfNecessary(); |
47 | | - |
48 | | - return task; |
| 32 | + return runtimeSchedulerImpl_->scheduleTask(priority, std::move(callback)); |
49 | 33 | } |
50 | 34 |
|
51 | 35 | std::shared_ptr<Task> RuntimeScheduler::scheduleTask( |
52 | 36 | SchedulerPriority priority, |
53 | 37 | RawCallback&& callback) noexcept { |
54 | | - auto expirationTime = now_() + timeoutForSchedulerPriority(priority); |
55 | | - auto task = |
56 | | - std::make_shared<Task>(priority, std::move(callback), expirationTime); |
57 | | - taskQueue_.push(task); |
58 | | - |
59 | | - scheduleWorkLoopIfNecessary(); |
60 | | - |
61 | | - return task; |
| 38 | + return runtimeSchedulerImpl_->scheduleTask(priority, std::move(callback)); |
62 | 39 | } |
63 | 40 |
|
64 | 41 | bool RuntimeScheduler::getShouldYield() const noexcept { |
65 | | - return runtimeAccessRequests_ > 0; |
| 42 | + return runtimeSchedulerImpl_->getShouldYield(); |
66 | 43 | } |
67 | 44 |
|
68 | 45 | bool RuntimeScheduler::getIsSynchronous() const noexcept { |
69 | | - return isSynchronous_; |
| 46 | + return runtimeSchedulerImpl_->getIsSynchronous(); |
70 | 47 | } |
71 | 48 |
|
72 | 49 | void RuntimeScheduler::cancelTask(Task& task) noexcept { |
73 | | - task.callback.reset(); |
| 50 | + return runtimeSchedulerImpl_->cancelTask(task); |
74 | 51 | } |
75 | 52 |
|
76 | 53 | SchedulerPriority RuntimeScheduler::getCurrentPriorityLevel() const noexcept { |
77 | | - return currentPriority_; |
| 54 | + return runtimeSchedulerImpl_->getCurrentPriorityLevel(); |
78 | 55 | } |
79 | 56 |
|
80 | 57 | RuntimeSchedulerTimePoint RuntimeScheduler::now() const noexcept { |
81 | | - return now_(); |
| 58 | + return runtimeSchedulerImpl_->now(); |
82 | 59 | } |
83 | 60 |
|
84 | 61 | void RuntimeScheduler::executeNowOnTheSameThread(RawCallback&& callback) { |
85 | | - SystraceSection s("RuntimeScheduler::executeNowOnTheSameThread"); |
86 | | - |
87 | | - runtimeAccessRequests_ += 1; |
88 | | - executeSynchronouslyOnSameThread_CAN_DEADLOCK( |
89 | | - runtimeExecutor_, |
90 | | - [this, callback = std::move(callback)](jsi::Runtime& runtime) { |
91 | | - SystraceSection s2( |
92 | | - "RuntimeScheduler::executeNowOnTheSameThread callback"); |
93 | | - |
94 | | - runtimeAccessRequests_ -= 1; |
95 | | - isSynchronous_ = true; |
96 | | - callback(runtime); |
97 | | - isSynchronous_ = false; |
98 | | - }); |
99 | | - |
100 | | - // Resume work loop if needed. In synchronous mode |
101 | | - // only expired tasks are executed. Tasks with lower priority |
102 | | - // might be still in the queue. |
103 | | - scheduleWorkLoopIfNecessary(); |
| 62 | + return runtimeSchedulerImpl_->executeNowOnTheSameThread(std::move(callback)); |
104 | 63 | } |
105 | 64 |
|
106 | 65 | void RuntimeScheduler::callExpiredTasks(jsi::Runtime& runtime) { |
107 | | - SystraceSection s("RuntimeScheduler::callExpiredTasks"); |
108 | | - |
109 | | - auto previousPriority = currentPriority_; |
110 | | - try { |
111 | | - while (!taskQueue_.empty()) { |
112 | | - auto topPriorityTask = taskQueue_.top(); |
113 | | - auto now = now_(); |
114 | | - auto didUserCallbackTimeout = topPriorityTask->expirationTime <= now; |
115 | | - |
116 | | - if (!didUserCallbackTimeout) { |
117 | | - break; |
118 | | - } |
119 | | - |
120 | | - executeTask(runtime, topPriorityTask, didUserCallbackTimeout); |
121 | | - } |
122 | | - } catch (jsi::JSError& error) { |
123 | | - handleFatalError(runtime, error); |
124 | | - } |
125 | | - |
126 | | - currentPriority_ = previousPriority; |
127 | | -} |
128 | | - |
129 | | -#pragma mark - Private |
130 | | - |
131 | | -void RuntimeScheduler::scheduleWorkLoopIfNecessary() const { |
132 | | - if (!isWorkLoopScheduled_ && !isPerformingWork_) { |
133 | | - isWorkLoopScheduled_ = true; |
134 | | - runtimeExecutor_([this](jsi::Runtime& runtime) { |
135 | | - isWorkLoopScheduled_ = false; |
136 | | - startWorkLoop(runtime); |
137 | | - }); |
138 | | - } |
139 | | -} |
140 | | - |
141 | | -void RuntimeScheduler::startWorkLoop(jsi::Runtime& runtime) const { |
142 | | - SystraceSection s("RuntimeScheduler::startWorkLoop"); |
143 | | - |
144 | | - auto previousPriority = currentPriority_; |
145 | | - isPerformingWork_ = true; |
146 | | - try { |
147 | | - while (!taskQueue_.empty()) { |
148 | | - auto topPriorityTask = taskQueue_.top(); |
149 | | - auto now = now_(); |
150 | | - auto didUserCallbackTimeout = topPriorityTask->expirationTime <= now; |
151 | | - |
152 | | - if (!didUserCallbackTimeout && getShouldYield()) { |
153 | | - // This currentTask hasn't expired, and we need to yield. |
154 | | - break; |
155 | | - } |
156 | | - |
157 | | - executeTask(runtime, topPriorityTask, didUserCallbackTimeout); |
158 | | - } |
159 | | - } catch (jsi::JSError& error) { |
160 | | - handleFatalError(runtime, error); |
161 | | - } |
162 | | - |
163 | | - currentPriority_ = previousPriority; |
164 | | - isPerformingWork_ = false; |
165 | | -} |
166 | | - |
167 | | -void RuntimeScheduler::executeTask( |
168 | | - jsi::Runtime& runtime, |
169 | | - const std::shared_ptr<Task>& task, |
170 | | - bool didUserCallbackTimeout) const { |
171 | | - SystraceSection s( |
172 | | - "RuntimeScheduler::executeTask", |
173 | | - "priority", |
174 | | - serialize(task->priority), |
175 | | - "didUserCallbackTimeout", |
176 | | - didUserCallbackTimeout); |
177 | | - |
178 | | - currentPriority_ = task->priority; |
179 | | - auto result = task->execute(runtime, didUserCallbackTimeout); |
180 | | - |
181 | | - if (result.isObject() && result.getObject(runtime).isFunction(runtime)) { |
182 | | - task->callback = result.getObject(runtime).getFunction(runtime); |
183 | | - } else { |
184 | | - if (taskQueue_.top() == task) { |
185 | | - taskQueue_.pop(); |
186 | | - } |
187 | | - } |
| 66 | + return runtimeSchedulerImpl_->callExpiredTasks(runtime); |
188 | 67 | } |
189 | 68 |
|
190 | 69 | } // namespace facebook::react |
0 commit comments