|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "RuntimeScheduler_Modern.h" |
| 9 | +#include "SchedulerPriorityUtils.h" |
| 10 | + |
| 11 | +#include <react/renderer/debug/SystraceSection.h> |
| 12 | +#include <utility> |
| 13 | +#include "ErrorUtils.h" |
| 14 | + |
| 15 | +namespace facebook::react { |
| 16 | + |
| 17 | +#pragma mark - Public |
| 18 | + |
| 19 | +RuntimeScheduler_Modern::RuntimeScheduler_Modern( |
| 20 | + RuntimeExecutor runtimeExecutor, |
| 21 | + std::function<RuntimeSchedulerTimePoint()> now) |
| 22 | + : runtimeExecutor_(std::move(runtimeExecutor)), now_(std::move(now)) {} |
| 23 | + |
| 24 | +void RuntimeScheduler_Modern::scheduleWork( |
| 25 | + RawCallback&& callback) const noexcept { |
| 26 | + SystraceSection s("RuntimeScheduler::scheduleWork"); |
| 27 | + scheduleTask(SchedulerPriority::ImmediatePriority, std::move(callback)); |
| 28 | +} |
| 29 | + |
| 30 | +std::shared_ptr<Task> RuntimeScheduler_Modern::scheduleTask( |
| 31 | + SchedulerPriority priority, |
| 32 | + jsi::Function&& callback) const noexcept { |
| 33 | + SystraceSection s( |
| 34 | + "RuntimeScheduler::scheduleTask", |
| 35 | + "priority", |
| 36 | + serialize(priority), |
| 37 | + "callbackType", |
| 38 | + "jsi::Function"); |
| 39 | + |
| 40 | + auto expirationTime = now_() + timeoutForSchedulerPriority(priority); |
| 41 | + auto task = |
| 42 | + std::make_shared<Task>(priority, std::move(callback), expirationTime); |
| 43 | + |
| 44 | + scheduleTask(task); |
| 45 | + |
| 46 | + return task; |
| 47 | +} |
| 48 | + |
| 49 | +std::shared_ptr<Task> RuntimeScheduler_Modern::scheduleTask( |
| 50 | + SchedulerPriority priority, |
| 51 | + RawCallback&& callback) const noexcept { |
| 52 | + SystraceSection s( |
| 53 | + "RuntimeScheduler::scheduleTask", |
| 54 | + "priority", |
| 55 | + serialize(priority), |
| 56 | + "callbackType", |
| 57 | + "RawCallback"); |
| 58 | + |
| 59 | + auto expirationTime = now_() + timeoutForSchedulerPriority(priority); |
| 60 | + auto task = |
| 61 | + std::make_shared<Task>(priority, std::move(callback), expirationTime); |
| 62 | + |
| 63 | + scheduleTask(task); |
| 64 | + |
| 65 | + return task; |
| 66 | +} |
| 67 | + |
| 68 | +bool RuntimeScheduler_Modern::getShouldYield() const noexcept { |
| 69 | + std::shared_lock lock(taskQueueMutex_); |
| 70 | + |
| 71 | + return syncTaskRequests_ > 0 || |
| 72 | + (!taskQueue_.empty() && taskQueue_.top() != currentTask_); |
| 73 | +} |
| 74 | + |
| 75 | +bool RuntimeScheduler_Modern::getIsSynchronous() const noexcept { |
| 76 | + return isSynchronous_; |
| 77 | +} |
| 78 | + |
| 79 | +void RuntimeScheduler_Modern::cancelTask(Task& task) noexcept { |
| 80 | + task.callback.reset(); |
| 81 | +} |
| 82 | + |
| 83 | +SchedulerPriority RuntimeScheduler_Modern::getCurrentPriorityLevel() |
| 84 | + const noexcept { |
| 85 | + return currentPriority_; |
| 86 | +} |
| 87 | + |
| 88 | +RuntimeSchedulerTimePoint RuntimeScheduler_Modern::now() const noexcept { |
| 89 | + return now_(); |
| 90 | +} |
| 91 | + |
| 92 | +void RuntimeScheduler_Modern::executeNowOnTheSameThread( |
| 93 | + RawCallback&& callback) { |
| 94 | + SystraceSection s("RuntimeScheduler::executeNowOnTheSameThread"); |
| 95 | + |
| 96 | + syncTaskRequests_++; |
| 97 | + |
| 98 | + executeSynchronouslyOnSameThread_CAN_DEADLOCK( |
| 99 | + runtimeExecutor_, |
| 100 | + [this, callback = std::move(callback)](jsi::Runtime& runtime) mutable { |
| 101 | + SystraceSection s2( |
| 102 | + "RuntimeScheduler::executeNowOnTheSameThread callback"); |
| 103 | + |
| 104 | + syncTaskRequests_--; |
| 105 | + |
| 106 | + isSynchronous_ = true; |
| 107 | + |
| 108 | + auto currentTime = now_(); |
| 109 | + auto priority = SchedulerPriority::ImmediatePriority; |
| 110 | + auto expirationTime = |
| 111 | + currentTime + timeoutForSchedulerPriority(priority); |
| 112 | + auto task = std::make_shared<Task>( |
| 113 | + priority, std::move(callback), expirationTime); |
| 114 | + |
| 115 | + executeTask(runtime, task, currentTime); |
| 116 | + |
| 117 | + isSynchronous_ = false; |
| 118 | + }); |
| 119 | + |
| 120 | + bool shouldScheduleWorkLoop = false; |
| 121 | + |
| 122 | + { |
| 123 | + std::unique_lock lock(taskQueueMutex_); |
| 124 | + |
| 125 | + if (!taskQueue_.empty() && !isWorkLoopScheduled_) { |
| 126 | + isWorkLoopScheduled_ = true; |
| 127 | + shouldScheduleWorkLoop = true; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (shouldScheduleWorkLoop) { |
| 132 | + scheduleWorkLoop(); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// This will be replaced by microtasks |
| 137 | +void RuntimeScheduler_Modern::callExpiredTasks(jsi::Runtime& runtime) { |
| 138 | + SystraceSection s("RuntimeScheduler::callExpiredTasks"); |
| 139 | + startWorkLoop(runtime, true); |
| 140 | +} |
| 141 | + |
| 142 | +#pragma mark - Private |
| 143 | + |
| 144 | +void RuntimeScheduler_Modern::scheduleTask(std::shared_ptr<Task> task) const { |
| 145 | + bool shouldScheduleWorkLoop = false; |
| 146 | + |
| 147 | + { |
| 148 | + std::unique_lock lock(taskQueueMutex_); |
| 149 | + |
| 150 | + if (taskQueue_.empty() && !isWorkLoopScheduled_) { |
| 151 | + isWorkLoopScheduled_ = true; |
| 152 | + shouldScheduleWorkLoop = true; |
| 153 | + } |
| 154 | + |
| 155 | + taskQueue_.push(task); |
| 156 | + } |
| 157 | + |
| 158 | + if (shouldScheduleWorkLoop) { |
| 159 | + scheduleWorkLoop(); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +void RuntimeScheduler_Modern::scheduleWorkLoop() const { |
| 164 | + runtimeExecutor_( |
| 165 | + [this](jsi::Runtime& runtime) { startWorkLoop(runtime, false); }); |
| 166 | +} |
| 167 | + |
| 168 | +void RuntimeScheduler_Modern::startWorkLoop( |
| 169 | + jsi::Runtime& runtime, |
| 170 | + bool onlyExpired) const { |
| 171 | + SystraceSection s("RuntimeScheduler::startWorkLoop"); |
| 172 | + |
| 173 | + auto previousPriority = currentPriority_; |
| 174 | + |
| 175 | + try { |
| 176 | + while (syncTaskRequests_ == 0) { |
| 177 | + auto currentTime = now_(); |
| 178 | + auto topPriorityTask = selectTask(currentTime, onlyExpired); |
| 179 | + |
| 180 | + if (!topPriorityTask) { |
| 181 | + // No pending work to do. |
| 182 | + // Events will restart the loop when necessary. |
| 183 | + break; |
| 184 | + } |
| 185 | + |
| 186 | + executeTask(runtime, topPriorityTask, currentTime); |
| 187 | + } |
| 188 | + } catch (jsi::JSError& error) { |
| 189 | + handleFatalError(runtime, error); |
| 190 | + } |
| 191 | + |
| 192 | + currentPriority_ = previousPriority; |
| 193 | +} |
| 194 | + |
| 195 | +std::shared_ptr<Task> RuntimeScheduler_Modern::selectTask( |
| 196 | + RuntimeSchedulerTimePoint currentTime, |
| 197 | + bool onlyExpired) const { |
| 198 | + std::unique_lock lock(taskQueueMutex_); |
| 199 | + |
| 200 | + // It's safe to reset the flag here, as its access is also synchronized with |
| 201 | + // the access to the task queue. |
| 202 | + isWorkLoopScheduled_ = false; |
| 203 | + |
| 204 | + // Skip executed tasks |
| 205 | + while (!taskQueue_.empty() && !taskQueue_.top()->callback) { |
| 206 | + taskQueue_.pop(); |
| 207 | + } |
| 208 | + |
| 209 | + if (!taskQueue_.empty()) { |
| 210 | + auto task = taskQueue_.top(); |
| 211 | + auto didUserCallbackTimeout = task->expirationTime <= currentTime; |
| 212 | + if (!onlyExpired || didUserCallbackTimeout) { |
| 213 | + return task; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + return nullptr; |
| 218 | +} |
| 219 | + |
| 220 | +void RuntimeScheduler_Modern::executeTask( |
| 221 | + jsi::Runtime& runtime, |
| 222 | + const std::shared_ptr<Task>& task, |
| 223 | + RuntimeSchedulerTimePoint currentTime) const { |
| 224 | + auto didUserCallbackTimeout = task->expirationTime <= currentTime; |
| 225 | + |
| 226 | + SystraceSection s( |
| 227 | + "RuntimeScheduler::executeTask", |
| 228 | + "priority", |
| 229 | + serialize(task->priority), |
| 230 | + "didUserCallbackTimeout", |
| 231 | + didUserCallbackTimeout); |
| 232 | + |
| 233 | + currentTask_ = task; |
| 234 | + currentPriority_ = task->priority; |
| 235 | + |
| 236 | + auto result = task->execute(runtime, didUserCallbackTimeout); |
| 237 | + |
| 238 | + if (result.isObject() && result.getObject(runtime).isFunction(runtime)) { |
| 239 | + // If the task returned a continuation callback, we re-assign it to the task |
| 240 | + // and keep the task in the queue. |
| 241 | + task->callback = result.getObject(runtime).getFunction(runtime); |
| 242 | + } |
| 243 | + |
| 244 | + // TODO execute microtasks |
| 245 | + // TODO report long tasks |
| 246 | + // TODO update rendering |
| 247 | +} |
| 248 | + |
| 249 | +} // namespace facebook::react |
0 commit comments