From 45d0b38076a88f81b4d061ddae1ebf543e8ffc13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Thu, 19 Jan 2023 13:45:04 +0100 Subject: [PATCH] Enhance thread safety of loaders and importers --- core/io/resource_loader.h | 6 ++++-- editor/editor_node.cpp | 1 + editor/editor_node.h | 1 + editor/import/editor_import_collada.cpp | 7 ------- editor/import/resource_importer_shader_file.cpp | 2 +- 5 files changed, 7 insertions(+), 10 deletions(-) diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h index 21651d3df5b..592befb6039 100644 --- a/core/io/resource_loader.h +++ b/core/io/resource_loader.h @@ -218,18 +218,20 @@ public: static void set_timestamp_on_load(bool p_timestamp) { timestamp_on_load = p_timestamp; } static bool get_timestamp_on_load() { return timestamp_on_load; } + // Loaders can safely use this regardless which thread they are running on. static void notify_load_error(const String &p_err) { if (err_notify) { - err_notify(p_err); + callable_mp_static(err_notify).bind(p_err).call_deferred(); } } static void set_error_notify_func(ResourceLoadErrorNotify p_err_notify) { err_notify = p_err_notify; } + // Loaders can safely use this regardless which thread they are running on. static void notify_dependency_error(const String &p_path, const String &p_dependency, const String &p_type) { if (dep_err_notify) { - dep_err_notify(p_path, p_dependency, p_type); + callable_mp_static(dep_err_notify).bind(p_path, p_dependency, p_type).call_deferred(); } } static void set_dependency_error_notify_func(DependencyErrorNotify p_err_notify) { diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 7d2517180e3..2d9f9645b8b 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -4115,6 +4115,7 @@ void EditorNode::notify_all_debug_sessions_exited() { } void EditorNode::add_io_error(const String &p_error) { + DEV_ASSERT(Thread::get_caller_id() == Thread::get_main_id()); singleton->load_errors->add_image(singleton->gui_base->get_theme_icon(SNAME("Error"), SNAME("EditorIcons"))); singleton->load_errors->add_text(p_error + "\n"); singleton->load_error_dialog->attach_and_popup_centered_ratio(0.5); diff --git a/editor/editor_node.h b/editor/editor_node.h index 28422390e69..221637be1cc 100644 --- a/editor/editor_node.h +++ b/editor/editor_node.h @@ -505,6 +505,7 @@ private: static Vector _init_callbacks; static void _dependency_error_report(const String &p_path, const String &p_dep, const String &p_type) { + DEV_ASSERT(Thread::get_caller_id() == Thread::get_main_id()); if (!singleton->dependency_errors.has(p_path)) { singleton->dependency_errors[p_path] = HashSet(); } diff --git a/editor/import/editor_import_collada.cpp b/editor/import/editor_import_collada.cpp index 1ffede65023..5f714e44885 100644 --- a/editor/import/editor_import_collada.cpp +++ b/editor/import/editor_import_collada.cpp @@ -1782,15 +1782,8 @@ Node *EditorSceneFormatImporterCollada::import_scene(const String &p_path, uint3 ERR_FAIL_COND_V_MSG(err != OK, nullptr, "Cannot load scene from file '" + p_path + "'."); if (state.missing_textures.size()) { - /* - for(int i=0;ipush_back(state.missing_textures[i]); } } diff --git a/editor/import/resource_importer_shader_file.cpp b/editor/import/resource_importer_shader_file.cpp index ba48fc90291..1275e5b85ae 100644 --- a/editor/import/resource_importer_shader_file.cpp +++ b/editor/import/resource_importer_shader_file.cpp @@ -106,7 +106,7 @@ Error ResourceImporterShaderFile::import(const String &p_source_file, const Stri if (err != OK) { if (!ShaderFileEditor::singleton->is_visible_in_tree()) { - EditorNode::get_singleton()->add_io_error(vformat(TTR("Error importing GLSL shader file: '%s'. Open the file in the filesystem dock in order to see the reason."), p_source_file)); + callable_mp_static(&EditorNode::add_io_error).bind(vformat(TTR("Error importing GLSL shader file: '%s'. Open the file in the filesystem dock in order to see the reason."), p_source_file)).call_deferred(); } }