From d98c30fdb33cd363e34ef81c9a30b4953da43872 Mon Sep 17 00:00:00 2001 From: Caoruihong Date: Thu, 7 Sep 2023 00:49:27 +0800 Subject: [PATCH] opt: allow only one sync task at a time Signed-off-by: Caoruihong --- frameworks/base/thread/task_executor.h | 9 +++++++++ frameworks/core/pipeline/pipeline_base.cpp | 18 +++--------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/frameworks/base/thread/task_executor.h b/frameworks/base/thread/task_executor.h index ab7351fb891..2b697166aba 100644 --- a/frameworks/base/thread/task_executor.h +++ b/frameworks/base/thread/task_executor.h @@ -244,8 +244,17 @@ protected: #endif private: + mutable std::mutex mutex_; bool PostTaskAndWait(CancelableTask&& task, TaskType type, std::chrono::milliseconds timeoutMs = 0ms) const { + // Allow only one sync task at a time + if (!mutex_.try_lock()) { + LOGW("other sync task is running, execute task directly, type=%{public}d", type); + CancelableTask avatar(task); + task(); + return avatar.WaitUntilComplete(); + } + std::lock_guard lock(mutex_, std::adopt_lock); #ifdef ACE_DEBUG bool result = false; if (OnPreSyncTask(type)) { diff --git a/frameworks/core/pipeline/pipeline_base.cpp b/frameworks/core/pipeline/pipeline_base.cpp index 9670d196993..80201a13ee9 100644 --- a/frameworks/core/pipeline/pipeline_base.cpp +++ b/frameworks/core/pipeline/pipeline_base.cpp @@ -418,11 +418,7 @@ void PipelineBase::UpdateRootSizeAndScale(int32_t width, int32_t height) rootHeight_ = height / viewScale_; rootWidth_ = width / viewScale_; }; - if (!taskExecutor_->PostSyncTaskTimeout(jsTask, TaskExecutor::TaskType::JS, 10)) { // 10ms - // JS thread maybe blocked on waiting UI thread, so try again directly - LOGW("UpdateRootSizeAndScale directly"); - jsTask(); - } + taskExecutor_->PostSyncTask(jsTask, TaskExecutor::TaskType::JS); } void PipelineBase::DumpFrontend() const @@ -433,11 +429,7 @@ void PipelineBase::DumpFrontend() const auto jsTask = [&] { frontend->DumpFrontend(); }; - if (!taskExecutor_->PostSyncTaskTimeout(jsTask, TaskExecutor::TaskType::JS, 10)) { // 10ms - // JS thread maybe blocked on waiting UI thread, so try again directly - LOGW("DumpFrontend directly"); - jsTask(); - } + taskExecutor_->PostSyncTask(jsTask, TaskExecutor::TaskType::JS); } bool PipelineBase::Dump(const std::vector& params) const @@ -680,11 +672,7 @@ RefPtr PipelineBase::GetAccessibilityManager() const auto jsTask = [&] { am = frontend->GetAccessibilityManager(); }; - if (!taskExecutor_->PostSyncTaskTimeout(jsTask, TaskExecutor::TaskType::JS, 10)) { // 10ms - // JS thread maybe blocked on waiting UI thread, so try again directly - LOGW("GetAccessibilityManager directly"); - jsTask(); - } + taskExecutor_->PostSyncTask(jsTask, TaskExecutor::TaskType::JS); return am; } -- Gitee