From 9bb1b2b4b53625a131ab452505f09b395a4cacce Mon Sep 17 00:00:00 2001 From: Panferov Ivan Date: Mon, 25 Aug 2025 17:46:25 +0800 Subject: [PATCH] sync coroutines code in master branch Issue: https://gitcode.com/openharmony/arkcompiler_runtime_core/issues/8105 Signed-off-by: Panferov Ivan --- .../ets_environment/src/ets_environment.cpp | 46 +++++++++++++++++-- .../interfaces/inner_api/ets_environment.h | 4 +- .../interfaces/inner_api/ets_interface.h | 3 +- .../ets_environment_test.cpp | 3 +- frameworks/native/runtime/ets_runtime.cpp | 2 +- 5 files changed, 50 insertions(+), 8 deletions(-) diff --git a/ets_environment/frameworks/ets_environment/src/ets_environment.cpp b/ets_environment/frameworks/ets_environment/src/ets_environment.cpp index 3f7c9ca92a..72a70b5669 100644 --- a/ets_environment/frameworks/ets_environment/src/ets_environment.cpp +++ b/ets_environment/frameworks/ets_environment/src/ets_environment.cpp @@ -69,6 +69,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_ {}; @@ -527,8 +528,11 @@ bool ETSEnvironment::FinishPreload() { bool 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()) { @@ -549,6 +553,9 @@ bool ETSEnvironment::PostFork(void *napiEnv, const std::string &aotPath, appInnerHspPathList_ = appInnerHspPathList; commonHspBundleInfos_ = commonHspBundleInfos; + + PostCoroutineScheduleTask(); + return true; } @@ -604,8 +611,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); @@ -707,7 +716,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); } } @@ -883,6 +892,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 54ce7956b8..2f14b83523 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); bool FinishPreload(); bool PostFork(void *napiEnv, const std::string &aotPath, const std::vector& appInnerHspPathList, - const std::vector &commonHspBundleInfos); + const std::vector &commonHspBundleInfos, + const std::shared_ptr &eventRunner); bool 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 5b4e82fb89..123dbf970f 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 (*SetExtensionApiCheckCallback)( std::function &cb) = nullptr; diff --git a/ets_environment/test/unittest/ets_environment_test/ets_environment_test.cpp b/ets_environment/test/unittest/ets_environment_test/ets_environment_test.cpp index 89314833ff..2f1f3c8a98 100644 --- a/ets_environment/test/unittest/ets_environment_test/ets_environment_test.cpp +++ b/ets_environment/test/unittest/ets_environment_test/ets_environment_test.cpp @@ -338,7 +338,8 @@ HWTEST_F(EtsEnvironmentTest, PostFork_0100, TestSize.Level0) std::string aotPath = "aotPath"; std::vector appInnerHspPathList; std::vector commonHspBundleInfos; - auto result = etsEnv->PostFork(napiEnv, aotPath, appInnerHspPathList, commonHspBundleInfos); + std::shared_ptr eventRunner; + auto result = etsEnv->PostFork(napiEnv, aotPath, appInnerHspPathList, commonHspBundleInfos, eventRunner); EXPECT_FALSE(result); } diff --git a/frameworks/native/runtime/ets_runtime.cpp b/frameworks/native/runtime/ets_runtime.cpp index 0ca968a977..5aadcc3a4f 100644 --- a/frameworks/native/runtime/ets_runtime.cpp +++ b/frameworks/native/runtime/ets_runtime.cpp @@ -161,7 +161,7 @@ bool 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); return true; }