From e0aa63ca4597427dab3fbefeac2500baaa4f034c Mon Sep 17 00:00:00 2001 From: xiongluo Date: Mon, 8 Apr 2024 11:16:39 +0800 Subject: [PATCH] Add concurrent FinalizeCallback machanism Signed-off-by: xiongluo Change-Id: I82114951fa719f71c03bac723896ae7496bb2f86 --- ecmascript/ecma_vm.cpp | 3 ++- ecmascript/js_native_pointer.h | 2 -- ecmascript/js_thread.cpp | 3 +++ ecmascript/js_thread.h | 6 +++++- ecmascript/napi/include/jsnapi_expo.h | 2 ++ ecmascript/napi/jsnapi_expo.cpp | 5 +++++ 6 files changed, 17 insertions(+), 4 deletions(-) diff --git a/ecmascript/ecma_vm.cpp b/ecmascript/ecma_vm.cpp index a69c382fbc..997014b027 100644 --- a/ecmascript/ecma_vm.cpp +++ b/ecmascript/ecma_vm.cpp @@ -475,6 +475,7 @@ void EcmaVM::ProcessNativeDelete(const WeakRootVisitor &visitor) { if (!heap_->IsYoungGC()) { auto iter = nativePointerList_.begin(); + ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "ProcessNativeDeleteNum:" + std::to_string(nativePointerList_.size())); while (iter != nativePointerList_.end()) { JSNativePointer *object = *iter; auto fwd = visitor(reinterpret_cast(object)); @@ -529,7 +530,7 @@ void EcmaVM::ProcessReferences(const WeakRootVisitor &visitor) heap_->ResetNativeBindingSize(); // array buffer auto iter = nativePointerList_.begin(); - ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "ProcessNativeDeleteNum:" + std::to_string(nativePointerList_.size())); + ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "ProcessReferencesNum:" + std::to_string(nativePointerList_.size())); while (iter != nativePointerList_.end()) { JSNativePointer *object = *iter; auto fwd = visitor(reinterpret_cast(object)); diff --git a/ecmascript/js_native_pointer.h b/ecmascript/js_native_pointer.h index ce0981f6f2..646c8cfe6c 100644 --- a/ecmascript/js_native_pointer.h +++ b/ecmascript/js_native_pointer.h @@ -70,8 +70,6 @@ private: void *externalPointer = GetExternalPointer(); DeleteEntryPoint deleter = GetDeleter(); if (deleter != nullptr) { - ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "ExternalDelete" - + std::to_string(reinterpret_cast(deleter))); deleter(externalPointer, GetData()); } } diff --git a/ecmascript/js_thread.cpp b/ecmascript/js_thread.cpp index 34f257528a..1d554bcbc2 100644 --- a/ecmascript/js_thread.cpp +++ b/ecmascript/js_thread.cpp @@ -244,6 +244,9 @@ void JSThread::InvokeWeakNodeNativeFinalizeCallback() auto callback = callbackPair.first; (*callback)(callbackPair.second); } + if (finalizeTaskCallback_ != nullptr) { + finalizeTaskCallback_(); + } runningNativeFinalizeCallbacks_ = false; } diff --git a/ecmascript/js_thread.h b/ecmascript/js_thread.h index 446569a59e..fead853ff8 100644 --- a/ecmascript/js_thread.h +++ b/ecmascript/js_thread.h @@ -1198,7 +1198,10 @@ public: MutatorLock::MutatorLockState GetMutatorLockState() const; void SetMutatorLockState(MutatorLock::MutatorLockState newState); #endif - + void SetWeakFinalizeTaskCallback(const WeakFinalizeTaskCallback &callback) + { + finalizeTaskCallback_ = callback; + } private: NO_COPY_SEMANTIC(JSThread); NO_MOVE_SEMANTIC(JSThread); @@ -1267,6 +1270,7 @@ private: WeakClearCallback nativeFinalizeCallBack)> setWeak_; std::function clearWeak_; std::function isWeak_; + WeakFinalizeTaskCallback finalizeTaskCallback_ {nullptr}; uint32_t globalNumberCount_ {0}; // Run-time state diff --git a/ecmascript/napi/include/jsnapi_expo.h b/ecmascript/napi/include/jsnapi_expo.h index d06c985ed0..7ab98f3376 100644 --- a/ecmascript/napi/include/jsnapi_expo.h +++ b/ecmascript/napi/include/jsnapi_expo.h @@ -89,6 +89,7 @@ struct HmsMap { using Deleter = void (*)(void *nativePointer, void *data); using WeakRefClearCallBack = void (*)(void *); +using WeakFinalizeTaskCallback = std::function; using EcmaVM = ecmascript::EcmaVM; using EcmaContext = ecmascript::EcmaContext; using JSThread = ecmascript::JSThread; @@ -1358,6 +1359,7 @@ public: static void SetpkgContextInfoList(EcmaVM *vm, const std::map>> &list); static void SetLoop(EcmaVM *vm, void *loop); + static void SetWeakFinalizeTaskCallback(EcmaVM *vm, const WeakFinalizeTaskCallback &callback); static std::string GetAssetPath(EcmaVM *vm); static bool InitForConcurrentThread(EcmaVM *vm, ConcurrentCallback cb, void *data); static bool InitForConcurrentFunction(EcmaVM *vm, Local func, void *taskInfo); diff --git a/ecmascript/napi/jsnapi_expo.cpp b/ecmascript/napi/jsnapi_expo.cpp index b01bd37a98..e65b66c4fc 100644 --- a/ecmascript/napi/jsnapi_expo.cpp +++ b/ecmascript/napi/jsnapi_expo.cpp @@ -2866,6 +2866,11 @@ void JSNApi::SetLoop(EcmaVM *vm, void *loop) vm->SetLoop(loop); } +void JSNApi::SetWeakFinalizeTaskCallback(EcmaVM *vm, const WeakFinalizeTaskCallback &callback) +{ + vm->GetAssociatedJSThread()->SetWeakFinalizeTaskCallback(callback); +} + std::string JSNApi::GetAssetPath(EcmaVM *vm) { return vm->GetAssetPath().c_str();