From 30925fcfb07d7c0b523761572a2da1865b55c86c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Mon, 3 Jul 2023 12:27:57 +0200 Subject: [PATCH] Fix management of safe-for-nodes flag in ResourceLoader and WorkerThreadPool --- core/io/resource_loader.cpp | 4 ++++ core/object/worker_thread_pool.cpp | 7 ++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index 525c41cf872..4e001efcf8c 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -309,6 +309,10 @@ void ResourceLoader::_thread_load_function(void *p_userdata) { } // -- + if (!Thread::is_main_thread()) { + set_current_thread_safe_for_nodes(true); + } + Ref res = _load(load_task.remapped_path, load_task.remapped_path != load_task.local_path ? load_task.local_path : String(), load_task.type_hint, load_task.cache_mode, &load_task.error, load_task.use_sub_threads, &load_task.progress); if (mq_override) { mq_override->flush(); diff --git a/core/object/worker_thread_pool.cpp b/core/object/worker_thread_pool.cpp index d285be3e70c..095c02811b7 100644 --- a/core/object/worker_thread_pool.cpp +++ b/core/object/worker_thread_pool.cpp @@ -56,6 +56,8 @@ void WorkerThreadPool::_process_task(Task *p_task) { Task *prev_low_prio_task = nullptr; // In case this is recursively called. if (!use_native_low_priority_threads) { + // Tasks must start with this unset. They are free to set-and-forget otherwise. + set_current_thread_safe_for_nodes(false); pool_thread_index = thread_ids[Thread::get_caller_id()]; ThreadData &curr_thread = threads[pool_thread_index]; task_mutex.lock(); @@ -179,9 +181,6 @@ void WorkerThreadPool::_process_task(Task *p_task) { if (post) { task_available_semaphore.post(); } - - // Engine/user tasks can set-and-forget, so we must be sure it's back to normal by the end of the task. - set_current_thread_safe_for_nodes(false); } } @@ -371,7 +370,9 @@ Error WorkerThreadPool::wait_for_task_completion(TaskID p_task_id) { must_exit = true; } else { // Solve tasks while they are around. + bool safe_for_nodes_backup = is_current_thread_safe_for_nodes(); _process_task_queue(); + set_current_thread_safe_for_nodes(safe_for_nodes_backup); continue; } } else if (!use_native_low_priority_threads && task->low_priority) {