From 670fa66b079727434ddaa1b46a9d34f7747f8b8d Mon Sep 17 00:00:00 2001 From: ilikethese Date: Tue, 17 Oct 2023 14:43:56 +0800 Subject: [PATCH] fix(dom): fix docallback multi thread conflict --- .../cpp/src/renderer/native_render_jni.cc | 54 ++++++++++--------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/renderer/native/android/src/main/cpp/src/renderer/native_render_jni.cc b/renderer/native/android/src/main/cpp/src/renderer/native_render_jni.cc index aae9ee40716..d82e958ecac 100644 --- a/renderer/native/android/src/main/cpp/src/renderer/native_render_jni.cc +++ b/renderer/native/android/src/main/cpp/src/renderer/native_render_jni.cc @@ -251,41 +251,47 @@ void DoCallBack(JNIEnv *j_env, jobject j_object, return; } - auto& root_map = RootNode::PersistentMap(); - std::shared_ptr root_node; uint32_t root_id = footstone::check::checked_numeric_cast(j_root_id); - ret = root_map.Find(root_id, root_node); - if (!ret) { - FOOTSTONE_DLOG(WARNING) << "DoCallBack root_node is nullptr"; - return; - } - - auto node = dom_manager->GetNode(root_node, - footstone::check::checked_numeric_cast(j_node_id)); - if (node == nullptr) { - FOOTSTONE_DLOG(WARNING) << "DoCallBack DomNode not found for id: " << j_node_id; - return; - } - + uint32_t node_id = footstone::check::checked_numeric_cast(j_node_id); + uint32_t cb_id = footstone::check::checked_numeric_cast(j_cb_id); jboolean is_copy = JNI_TRUE; - const char* func_name = j_env->GetStringUTFChars(j_func_name, &is_copy); - auto callback = node->GetCallback(func_name, - footstone::check::checked_numeric_cast(j_cb_id)); - if (callback == nullptr) { - FOOTSTONE_DLOG(WARNING) << "DoCallBack Callback not found for func_name: " << func_name; - return; - } + const char* c = j_env->GetStringUTFChars(j_func_name, &is_copy); + std::string func_name(c); std::shared_ptr params = std::make_shared(); if (j_buffer != nullptr && j_length > 0) { jbyte params_buffer[j_length]; j_env->GetByteArrayRegion(j_buffer, j_offset, j_length, params_buffer); footstone::value::Deserializer deserializer((const uint8_t*) params_buffer, - footstone::check::checked_numeric_cast(j_length)); + footstone::check::checked_numeric_cast(j_length)); deserializer.ReadHeader(); deserializer.ReadValue(*params); } - callback(std::make_shared(*params)); + + std::vector> ops = {[root_id, node_id, cb_id, func_name, dom_manager, params]{ + auto& root_map = RootNode::PersistentMap(); + std::shared_ptr root_node; + bool ret = root_map.Find(root_id, root_node); + if (!ret) { + FOOTSTONE_DLOG(WARNING) << "DoCallBack root_node is nullptr"; + return; + } + + auto node = dom_manager->GetNode(root_node, node_id); + if (node == nullptr) { + FOOTSTONE_DLOG(WARNING) << "DoCallBack DomNode not found for id: " << node_id; + return; + } + + auto callback = node->GetCallback(func_name, cb_id); + if (callback == nullptr) { + FOOTSTONE_DLOG(WARNING) << "DoCallBack Callback not found for func_name: " << func_name; + return; + } + + callback(std::make_shared(*params)); + }}; + dom_manager->PostTask(Scene(std::move(ops))); } void OnReceivedEvent(JNIEnv* j_env, jobject j_object, jint j_render_manager_id, jint j_root_id, jint j_dom_id, jstring j_event_name,