diff --git a/packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.cpp b/packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.cpp index 8cc6a6deebb00f..a38be770cb1243 100644 --- a/packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.cpp +++ b/packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.cpp @@ -71,7 +71,7 @@ bool RuntimeScheduler_Modern::getShouldYield() const noexcept { std::shared_lock lock(schedulingMutex_); return syncTaskRequests_ > 0 || - (!taskQueue_.empty() && taskQueue_.top() != currentTask_); + (!taskQueue_.empty() && taskQueue_.top().get() != currentTask_); } void RuntimeScheduler_Modern::cancelTask(Task& task) noexcept { @@ -105,9 +105,8 @@ void RuntimeScheduler_Modern::executeNowOnTheSameThread( auto priority = SchedulerPriority::ImmediatePriority; auto expirationTime = currentTime + timeoutForSchedulerPriority(priority); - auto task = std::make_shared( - priority, std::move(callback), expirationTime); + auto task = Task{priority, std::move(callback), expirationTime}; executeTask(runtime, task, currentTime); }); @@ -207,7 +206,7 @@ void RuntimeScheduler_Modern::startWorkLoop( break; } - executeTask(runtime, topPriorityTask, currentTime); + executeTask(runtime, *topPriorityTask, currentTime); } } catch (jsi::JSError& error) { handleJSError(runtime, error, true); @@ -245,19 +244,19 @@ std::shared_ptr RuntimeScheduler_Modern::selectTask( void RuntimeScheduler_Modern::executeTask( jsi::Runtime& runtime, - const std::shared_ptr& task, + Task& task, RuntimeSchedulerTimePoint currentTime) { - auto didUserCallbackTimeout = task->expirationTime <= currentTime; + auto didUserCallbackTimeout = task.expirationTime <= currentTime; SystraceSection s( "RuntimeScheduler::executeTask", "priority", - serialize(task->priority), + serialize(task.priority), "didUserCallbackTimeout", didUserCallbackTimeout); - currentTask_ = task; - currentPriority_ = task->priority; + currentTask_ = &task; + currentPriority_ = task.priority; { ScopedShadowTreeRevisionLock revisionLock( @@ -275,6 +274,8 @@ void RuntimeScheduler_Modern::executeTask( updateRendering(); } } + + currentTask_ = nullptr; } /** @@ -296,16 +297,16 @@ void RuntimeScheduler_Modern::updateRendering() { void RuntimeScheduler_Modern::executeMacrotask( jsi::Runtime& runtime, - std::shared_ptr task, + Task& task, bool didUserCallbackTimeout) const { SystraceSection s("RuntimeScheduler::executeMacrotask"); - auto result = task->execute(runtime, didUserCallbackTimeout); + auto result = task.execute(runtime, didUserCallbackTimeout); if (result.isObject() && result.getObject(runtime).isFunction(runtime)) { // If the task returned a continuation callback, we re-assign it to the task // and keep the task in the queue. - task->callback = result.getObject(runtime).getFunction(runtime); + task.callback = result.getObject(runtime).getFunction(runtime); } } diff --git a/packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.h b/packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.h index f730509928ba73..fa9b6c8c8e75d3 100644 --- a/packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.h +++ b/packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.h @@ -135,7 +135,7 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase { TaskPriorityComparer> taskQueue_; - std::shared_ptr currentTask_; + Task* currentTask_{}; /** * This protects the access to `taskQueue_` and `isWorkLoopScheduled_`. @@ -162,12 +162,12 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase { */ void executeTask( jsi::Runtime& runtime, - const std::shared_ptr& task, + Task& task, RuntimeSchedulerTimePoint currentTime); void executeMacrotask( jsi::Runtime& runtime, - std::shared_ptr task, + Task& task, bool didUserCallbackTimeout) const; void updateRendering();