赞
踩
本文主要分析ArkUI中涉及的线程和看门狗机制。
应用Ability首次创建界面的流程大致如下:
说明:
• AceContainer是一个容器类,由前端、任务执行器、资源管理器、渲染管线、视图等聚合而成,提供了生命周期对接、功能调度接口和UI渲染的各项能力。
• Ability在FA模型中实际为AceAbility,和AceContainer容器类搭配管理界面。在AceAbility的生命周期函数AceAbility::OnStart(const Want& want)中创建AceContainer实例。
• 对于Stage模型,在UIContentImpl::CommonInitialize()函数中创建AceContainer实例。AceContainer在构造函数中创建任务执行器,用于执行ArkUI相关任务。
- void AceContainer::InitializeTask()
- {
- auto flutterTaskExecutor = Referenced::MakeRefPtr<FlutterTaskExecutor>();
- flutterTaskExecutor->InitPlatformThread(useCurrentEventRunner_);
- taskExecutor_ = flutterTaskExecutor;
- // No need to create JS Thread for DECLARATIVE_JS
- if (type_ == FrontendType::DECLARATIVE_JS) {
- GetSettings().useUIAsJSThread = true;
- } else {
- flutterTaskExecutor->InitJsThread();
- }
- }
任务有如下几种类型,每种类型(BACKGROUND任务除外)的任务会由一个fml::TaskRunner去执行。TaskRunner代码在三方库third_party\flutter\engine\flutter\common\task_runners.h中,实现原理和EventRunner,EventHandler机制相似。
- enum class TaskType : uint32_t {
- PLATFORM = 0,
- UI,
- IO,
- GPU,
- JS,
- BACKGROUND,
- UNKNOWN,
- };
FlutterTaskExecutor类图如下:
说明:
• 任务执行器可以用于执行异步(PostTask)和同步(PostSyncTask)任务。
• 异步任务:把任务丢给指定类型的线程处理,不会阻塞当前线程。
• 同步任务:把任务丢给指定类型的线程处理并阻塞当前线程,直到任务执行完后继续当前线程。
• 比如触摸事件的处理,会以异步任务的形式被丢到UI线程中处理。
- auto&& touchEventCallback = [context = pipelineContext_, id = instanceId_](
- const TouchEvent& event, const std::function<void()>& markProcess) {
- ContainerScope scope(id);
- context->GetTaskExecutor()->PostTask(
- [context, event, markProcess]() {
- context->OnTouchEvent(event);
- CHECK_NULL_VOID_NOLOG(markProcess);
- markProcess();
- },
- TaskExecutor::TaskType::UI);
- };
1. platformRunner_
在InitPlatformThread函数中初始化。
- void FlutterTaskExecutor::InitPlatformThread(bool useCurrentEventRunner)
- {
- #ifdef OHOS_STANDARD_SYSTEM
- platformRunner_ = flutter::PlatformTaskRunner::CurrentTaskRunner(useCurrentEventRunner);
- #else
- fml::MessageLoop::EnsureInitializedForCurrentThread();
- platformRunner_ = fml::MessageLoop::GetCurrent().GetTaskRunner();
- #endif
-
- FillTaskTypeTable(TaskType::PLATFORM);
- }
对于标准OHOS,platformRunner_实际为
flutter::PlatformTaskRunner::CurrentTaskRunner(useCurrentEventRunner)
看下具体实现:
- fml::RefPtr<fml::TaskRunner> PlatformTaskRunner::CurrentTaskRunner(bool useCurrentEventRunner)
- {
- return PlatformTaskRunnerAdapter::CurrentTaskRunner(useCurrentEventRunner);
- }
- fml::RefPtr<fml::TaskRunner> PlatformTaskRunnerAdapter::CurrentTaskRunner(bool useCurrentEventRunner)
- {
- if (useCurrentEventRunner) {
- return fml::MakeRefCounted<PlatformTaskRunnerAdapter>(useCurrentEventRunner);
- }
- if (taskRunner_) {
- return taskRunner_;
- }
- taskRunner_ = fml::MakeRefCounted<PlatformTaskRunnerAdapter>(useCurrentEventRunner);
- return taskRunner_;
- }
说明:
platformRunner实际类型为PlatformTaskRunnerAdapter。
PlatformTaskRunnerAdapter继承自fml::TaskRunner,实现了virtual void PostTask(fml::closure task)等接口函数。实际是在EventRunner,EventHandler机制基础上又做了层封装。代码中useCurrentEventRunner实参为false。意味着platformRunner实际是把任务丢给主线程去做的。(MainEventRunner对应的线程为主线程,MainEventRunner的初始化在Ability框架MainThread::Start()函数中)
- PlatformTaskRunnerAdapter::PlatformTaskRunnerAdapter(bool useCurrentEventRunner)
- : fml::TaskRunner(nullptr)
- {
- if (useCurrentEventRunner) {
- eventRunner_ = OHOS::AppExecFwk::EventRunner::Current();
- } else {
- eventRunner_ = OHOS::AppExecFwk::EventRunner::GetMainEventRunner();
- }
- eventHandler_ = std::make_shared<OHOS::AppExecFwk::EventHandler>(eventRunner_);
- }
-
- void PlatformTaskRunnerAdapter::PostTask(fml::closure task)
- {
- eventHandler_->PostTask(std::move(task));
- }
2. uiRunner, ioRunner, gpuRunner_
这三种类型的TaskRunner初始化都在FlutterTaskExecutor::InitOtherThreads函数中。
- void FlutterTaskExecutor::InitOtherThreads(const flutter::TaskRunners& taskRunners)
- {
- uiRunner_ = taskRunners.GetUITaskRunner();
- ioRunner_ = taskRunners.GetIOTaskRunner();
- #ifdef NG_BUILD
- gpuRunner_ = taskRunners.GetRasterTaskRunner();
- #else
- gpuRunner_ = taskRunners.GetGPUTaskRunner();
- #endif
-
- //...此处省略若干行
- }
FlutterTaskExecutor::InitOtherThreads函数的参数 taskRunners从哪来?
FlutterAceView::CreateView()函数中会初始化一些配置项,然后创建flutter::OhosShellHolder对象。
FlutterAceView* FlutterAceView::CreateView(int32_t instanceId, bool useCurrentEventRunner, bool usePlatformThread) { FlutterAceView* aceSurface = new Platform::FlutterAceView(instanceId); if (aceSurface != nullptr) { aceSurface->IncRefCount(); } flutter::Settings settings; settings.instanceId = instanceId; settings.platform = flutter::AcePlatform::ACE_PLATFORM_OHOS; #ifndef GPU_DISABLED settings.enable_software_rendering = false; #else settings.enable_software_rendering = true; #endif #ifdef ENABLE_ROSEN_BACKEND settings.use_system_render_thread = SystemProperties::GetRosenBackendEnabled(); #endif settings.platform_as_ui_thread = usePlatformThread; settings.use_current_event_runner = useCurrentEventRunner; // ...此处省略若干行 auto shell_holder = std::make_unique<flutter::OhosShellHolder>(settings, false); if (aceSurface != nullptr) { aceSurface->SetShellHolder(std::move(shell_holder)); } return aceSurface; }
OhosShellHolder构造函数中会根据传入的参数创建flutter::TaskRunners。
OhosShellHolder::OhosShellHolder( flutter::Settings settings, bool is_background_view) : settings_(std::move(settings)) { // ...此处省略若干行 // The current thread will be used as the platform thread. Ensure that the // message loop is initialized. fml::MessageLoop::EnsureInitializedForCurrentThread(); fml::RefPtr<fml::TaskRunner> gpu_runner; fml::RefPtr<fml::TaskRunner> ui_runner; fml::RefPtr<fml::TaskRunner> io_runner; fml::RefPtr<fml::TaskRunner> platform_runner = PlatformTaskRunnerAdapter::CurrentTaskRunner(settings_.use_current_event_runner); if (is_background_view) { auto single_task_runner = thread_host_.ui_thread->GetTaskRunner(); gpu_runner = single_task_runner; ui_runner = single_task_runner; io_runner = single_task_runner; } else { if (settings_.platform_as_ui_thread) { ui_runner = platform_runner; } else { ui_runner = thread_host_.ui_thread->GetTaskRunner(); } if (!settings_.use_system_render_thread) { gpu_runner = thread_host_.gpu_thread->GetTaskRunner(); } else { gpu_runner = ui_runner; } if (settings_.use_io_thread) { io_runner = thread_host_.io_thread->GetTaskRunner(); } else { io_runner = ui_runner; } } flutter::TaskRunners task_runners(thread_label, // label platform_runner, // platform gpu_runner, // gpu ui_runner, // ui io_runner // io );
说明:目前OHOS上,配置的参数如下:
对照上面的代码段,实际gpu_runner,ui_runner,io_runner是同一个,任务都在UI线程执行。另外对于Stage模型,ui_runner和platform_runner又是同一个,所以对Stage模型来说,TaskType::UI,TaskType::IO,TaskType::GPU,TaskType::PLATFORM类型的任务实际都是由主线程来执行的。
3. jsRunner_
初始化在FlutterTaskExecutor::InitJsThread(bool newThread)函数中。
- void FlutterTaskExecutor::InitJsThread(bool newThread)
- {
- if (newThread) {
- jsThread_ = std::make_unique<fml::Thread>(GenJsThreadName());
- jsRunner_ = jsThread_->GetTaskRunner();
- } else {
- jsRunner_ = uiRunner_;
- }
-
- PostTaskToTaskRunner(
- jsRunner_, [weak = AceType::WeakClaim(this)] { FillTaskTypeTable(weak, TaskType::JS); }, 0);
- }
说明:对于声明式前端,newThread参数为false; JS前端为true。所以声明式前端JS线程实际为UI线程;而对于JS前端,会起独立的JS线程来处理JS相关的任务。
4. TaskType::BACKGROUND类型的任务如何执行?
TaskType::BACKGROUND类型的任务会由单例BackgroundTaskExecutor去执行。BackgroundTaskExecutor中维护了一个8个线程的线程池,用来处理后台耗时操作。线程名以"ace.bg."开头。比如RosenFontLoader在加载网络字体的时候,下载操作会放到后台任务线程里去做。
void RosenFontLoader::LoadFromNetwork(const OHOS::Ace::RefPtr<OHOS::Ace::PipelineBase>& context) { auto weakContext = AceType::WeakClaim(AceType::RawPtr(context)); context->GetTaskExecutor()->PostTask( [weak = AceType::WeakClaim(this), weakContext] { auto fontLoader = weak.Upgrade(); auto context = weakContext.Upgrade(); if (!fontLoader || !context) { return; } std::vector<uint8_t> fontData; if (!DownloadManager::GetInstance().Download(fontLoader->familySrc_, fontData) || fontData.empty()) { return; } //...此处省略若干行 }, TaskExecutor::TaskType::BACKGROUND); }
综上:在ArkUI中,会为每个带界面的Ability创建一个AceContainer,每个AceContainer中会创建一个FlutterTaskExecutor用于处理该Ability ArkUI相关的任务。根据不同的模型,ArkUI创建出来的线程会有所不同:
• 对于Stage模型的应用,ui线程复用了主线程,并且Stage模型应用目前都是声明式前端,导致js线程又复用了ui线程。所以ArkUI只需另外创建名字以“ace.bg.”开头的八个后台任务线程。
• 对于FA模型的应用,除了八个后台任务线程,根据Ability的数量会创建若干个名字以“.ui”结尾的线程。如果是JS前端,还会创建若干个名字以“jsThread-”开头的线程。
AceEngine是单例,全局唯一。AceEngine的构造函数中会创建WatchDog实例。对于FA模型的应用,AceContainer::AttachView()函数中通过调用
AceEngine::Get().RegisterToWatchDog(instanceId, taskExecutor_,
GetSettings().useUIAsJSThread);
把持有的FlutterTaskExecutor注册到看门狗中看护。
看门狗只看护FlutterTaskExecutor中的UI线程和JS线程。Stage模型的应用由于UI线程和JS线程实际是复用的主线程,所以不需要在ArkUI中看护。Ability框架中有看门狗专门看护主线程。如果线程中有任务处理超过了3s,会上报RawEventType::WARNING对应的系统事件给hiview插件平台;如果任务处理超过了5s,会上报RawEventType::FREEZE对应的系统事件给hiview插件平台,hiview插件平台会生成appfreeze的dump文件。
为了防止主线程和ui线程卡住引起appfreeze,做应用开发的时候,不要在Ability生命周期函数或者控件点击事件等回调函数中做耗时操作。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。