From f0d7550d8b81bada94ef3caf940fe0719b376685 Mon Sep 17 00:00:00 2001 From: Panferov Ivan Date: Mon, 25 Aug 2025 17:46:25 +0800 Subject: [PATCH] post coro scheduling in ETS runtime init Issue: #ICUM1H Signed-off-by: Panferov Ivan --- .../ets_environment/src/ets_environment.cpp | 44 +++++++++++++++++-- .../interfaces/inner_api/ets_environment.h | 4 +- .../interfaces/inner_api/ets_interface.h | 3 +- frameworks/native/runtime/ets_runtime.cpp | 2 +- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/ets_environment/frameworks/ets_environment/src/ets_environment.cpp b/ets_environment/frameworks/ets_environment/src/ets_environment.cpp index 9b1a7982c79..1b3fd511457 100644 --- a/ets_environment/frameworks/ets_environment/src/ets_environment.cpp +++ b/ets_environment/frameworks/ets_environment/src/ets_environment.cpp @@ -67,6 +67,7 @@ const char ETS_SYS_NSNAME[] = "ets_system"; constexpr const char* CLASSNAME_STRING = "Lstd/core/String;"; constexpr const char* CLASSNAME_LINKER = "Lstd/core/AbcRuntimeLinker;"; +constexpr const char* CLASSNAME_COROUTINE = "std.core.Coroutine"; } // namespace ETSRuntimeAPI ETSEnvironment::lazyApis_ {}; @@ -529,8 +530,11 @@ void ETSEnvironment::FinishPreload() { void ETSEnvironment::PostFork(void *napiEnv, const std::string &aotPath, const std::vector &appInnerHspPathList, - const std::vector &commonHspBundleInfos) + const std::vector &commonHspBundleInfos, + const std::shared_ptr &eventRunner) { + InitEventHandler(eventRunner); + std::vector options; std::string aotPathString = ""; if (!aotPath.empty()) { @@ -550,6 +554,7 @@ void ETSEnvironment::PostFork(void *napiEnv, const std::string &aotPath, ark::ets::ETSAni::Postfork(env, options); appInnerHspPathList_ = appInnerHspPathList; commonHspBundleInfos_ = commonHspBundleInfos; + PostCoroutineScheduleTask(); } void ETSEnvironment::PreloadSystemClass(const char *className) @@ -603,8 +608,10 @@ ETSEnvFuncs *ETSEnvironment::RegisterFuncs() ETSEnvironment::GetInstance()->FinishPreload(); }, .PostFork = [](void *napiEnv, const std::string &aotPath, const std::vector &appInnerHspPathList, - const std::vector &commonHspBundleInfos) { - ETSEnvironment::GetInstance()->PostFork(napiEnv, aotPath, appInnerHspPathList, commonHspBundleInfos); + const std::vector &commonHspBundleInfos, + const std::shared_ptr &eventRunner) { + ETSEnvironment::GetInstance()->PostFork( + napiEnv, aotPath, appInnerHspPathList, commonHspBundleInfos, eventRunner); }, .PreloadSystemClass = [](const char *className) { ETSEnvironment::GetInstance()->PreloadSystemClass(className); @@ -697,7 +704,7 @@ int32_t ETSEnvironment::ParseHdcRegisterOption(std::string& option) void ETSEnvironment::InitEventHandler(const std::shared_ptr &eventRunner) { TAG_LOGD(AAFwkTag::ETSRUNTIME, "InitEventHandler called"); - if (eventRunner != nullptr) { + if (eventRunner != nullptr && eventHandler_ == nullptr) { eventHandler_ = std::make_shared(eventRunner); } } @@ -873,6 +880,35 @@ ani_object ETSEnvironment::CreateRuntimeLinker( return object; } + +static void ScheduleCoroutine(ani_env *aniEnv) +{ + ani_class cls = nullptr; + if (aniEnv->FindClass(CLASSNAME_COROUTINE, &cls) != ANI_OK) { + TAG_LOGE(AAFwkTag::ETSRUNTIME, "FindClass std.core.Coroutine Failed"); + return; + } + ani_static_method schedule {}; + if (aniEnv->Class_FindStaticMethod(cls, "Schedule", ":", &schedule) != ANI_OK) { + TAG_LOGE(AAFwkTag::ETSRUNTIME, "Class_FindStaticMethod Schedule failed"); + return; + } + if (aniEnv->Class_CallStaticMethod_Void(cls, schedule) != ANI_OK) { + TAG_LOGE(AAFwkTag::ETSRUNTIME, "Class_CallStaticMethod_Void Schedule failed"); + return; + } +} + +void ETSEnvironment::PostCoroutineScheduleTask() +{ + auto scheduleTask = []() { + auto &etsEnv = GetInstance(); + ScheduleCoroutine(etsEnv->GetAniEnv()); + etsEnv->PostCoroutineScheduleTask(); + }; + PostTask(scheduleTask, "ScheduleCoroutine", 1); +} + } // namespace EtsEnv } // namespace OHOS diff --git a/ets_environment/interfaces/inner_api/ets_environment.h b/ets_environment/interfaces/inner_api/ets_environment.h index 2e9a19aea07..e8155c75829 100644 --- a/ets_environment/interfaces/inner_api/ets_environment.h +++ b/ets_environment/interfaces/inner_api/ets_environment.h @@ -61,7 +61,8 @@ public: void *&obj, void *&ref); void FinishPreload(); void PostFork(void *napiEnv, const std::string &aotPath, const std::vector& appInnerHspPathList, - const std::vector &commonHspBundleInfos); + const std::vector &commonHspBundleInfos, + const std::shared_ptr &eventRunner); void PreloadSystemClass(const char *className); void RemoveInstance(uint32_t instanceId); @@ -99,6 +100,7 @@ private: bool LoadAbcLinker(ani_env *env, const std::string &modulePath, ani_class &abcCls, ani_object &abcObj); void InitEventHandler(const std::shared_ptr &eventRunner); int32_t ParseHdcRegisterOption(std::string& option); + void PostCoroutineScheduleTask(); static ETSRuntimeAPI lazyApis_; VMEntry vmEntry_; ETSUncaughtExceptionInfo uncaughtExceptionInfo_; diff --git a/ets_environment/interfaces/inner_api/ets_interface.h b/ets_environment/interfaces/inner_api/ets_interface.h index 935a46ef708..23c967c8394 100644 --- a/ets_environment/interfaces/inner_api/ets_interface.h +++ b/ets_environment/interfaces/inner_api/ets_interface.h @@ -50,7 +50,8 @@ struct ETSEnvFuncs { void (*FinishPreload)() = nullptr; void (*PostFork)(void *napiEnv, const std::string &aotPath, const std::vector &appInnerHspPathList, - const std::vector &commonHspBundleInfos) = nullptr; + const std::vector &commonHspBundleInfos, + const std::shared_ptr &eventRunner) = nullptr; void (*PreloadSystemClass)(const char *className) = nullptr; void (*RemoveInstance)(uint32_t instanceId) = nullptr; void (*StopDebugMode)(void *jsVm) = nullptr; diff --git a/frameworks/native/runtime/ets_runtime.cpp b/frameworks/native/runtime/ets_runtime.cpp index 98ed7221404..4ecc9227706 100644 --- a/frameworks/native/runtime/ets_runtime.cpp +++ b/frameworks/native/runtime/ets_runtime.cpp @@ -279,7 +279,7 @@ void ETSRuntime::PostFork(const Options &options, std::unique_ptr &js } napi_env napiEnv = static_cast(jsRuntime_.get())->GetNapiEnv(); g_etsEnvFuncs->PostFork(reinterpret_cast(napiEnv), aotFilePath, options.appInnerHspPathList, - options.commonHspBundleInfos); + options.commonHspBundleInfos, options.eventRunner); } std::unique_ptr ETSRuntime::Create(const Options &options, std::unique_ptr &jsRuntime) -- Gitee