@@ -234,17 +234,22 @@ void ResourceLoader::LoadToken::clear() {
234234 // User-facing tokens shouldn't be deleted until completely claimed.
235235 DEV_ASSERT (user_rc == 0 && user_path.is_empty ());
236236
237- if (!local_path.is_empty ()) { // Empty is used for the special case where the load task is not registered.
238- DEV_ASSERT (thread_load_tasks.has (local_path));
239- ThreadLoadTask &load_task = thread_load_tasks[local_path];
240- if (load_task.task_id && !load_task.awaited ) {
241- task_to_await = load_task.task_id ;
237+ if (!local_path.is_empty ()) {
238+ if (task_if_unregistered) {
239+ memdelete (task_if_unregistered);
240+ task_if_unregistered = nullptr ;
241+ } else {
242+ DEV_ASSERT (thread_load_tasks.has (local_path));
243+ ThreadLoadTask &load_task = thread_load_tasks[local_path];
244+ if (load_task.task_id && !load_task.awaited ) {
245+ task_to_await = load_task.task_id ;
246+ }
247+ // Removing a task which is still in progress would be catastrophic.
248+ // Tokens must be alive until the task thread function is done.
249+ DEV_ASSERT (load_task.status == THREAD_LOAD_FAILED || load_task.status == THREAD_LOAD_LOADED);
250+ thread_load_tasks.erase (local_path);
242251 }
243- // Removing a task which is still in progress would be catastrophic.
244- // Tokens must be alive until the task thread function is done.
245- DEV_ASSERT (load_task.status == THREAD_LOAD_FAILED || load_task.status == THREAD_LOAD_LOADED);
246- thread_load_tasks.erase (local_path);
247- local_path.clear ();
252+ local_path.clear (); // Mark as already cleared.
248253 }
249254 }
250255
@@ -521,9 +526,7 @@ Ref<ResourceLoader::LoadToken> ResourceLoader::_load_start(const String &p_path,
521526
522527 Ref<LoadToken> load_token;
523528 bool must_not_register = false ;
524- ThreadLoadTask unregistered_load_task; // Once set, must be valid up to the call to do the load.
525529 ThreadLoadTask *load_task_ptr = nullptr ;
526- bool run_on_current_thread = false ;
527530 {
528531 MutexLock thread_load_lock (thread_load_mutex);
529532
@@ -578,22 +581,19 @@ Ref<ResourceLoader::LoadToken> ResourceLoader::_load_start(const String &p_path,
578581 }
579582 }
580583
581- // If we want to ignore cache, but there's another task loading it, we can't add this one to the map and we also have to finish within scope .
584+ // If we want to ignore cache, but there's another task loading it, we can't add this one to the map.
582585 must_not_register = ignoring_cache && thread_load_tasks.has (local_path);
583586 if (must_not_register) {
584- load_token->local_path .clear ();
585- unregistered_load_task = load_task;
586- load_task_ptr = &unregistered_load_task;
587+ load_token->task_if_unregistered = memnew (ThreadLoadTask (load_task));
588+ load_task_ptr = load_token->task_if_unregistered ;
587589 } else {
588590 DEV_ASSERT (!thread_load_tasks.has (local_path));
589591 HashMap<String, ResourceLoader::ThreadLoadTask>::Iterator E = thread_load_tasks.insert (local_path, load_task);
590592 load_task_ptr = &E->value ;
591593 }
592594 }
593595
594- run_on_current_thread = must_not_register || p_thread_mode == LOAD_THREAD_FROM_CURRENT;
595-
596- if (run_on_current_thread) {
596+ if (p_thread_mode == LOAD_THREAD_FROM_CURRENT) {
597597 // The current thread may happen to be a thread from the pool.
598598 WorkerThreadPool::TaskID tid = WorkerThreadPool::get_singleton ()->get_caller_task_id ();
599599 if (tid != WorkerThreadPool::INVALID_TASK_ID) {
@@ -606,11 +606,8 @@ Ref<ResourceLoader::LoadToken> ResourceLoader::_load_start(const String &p_path,
606606 }
607607 } // MutexLock(thread_load_mutex).
608608
609- if (run_on_current_thread ) {
609+ if (p_thread_mode == LOAD_THREAD_FROM_CURRENT ) {
610610 _run_load_task (load_task_ptr);
611- if (must_not_register) {
612- load_token->res_if_unregistered = load_task_ptr->resource ;
613- }
614611 }
615612
616613 return load_token;
@@ -738,7 +735,10 @@ Ref<Resource> ResourceLoader::_load_complete_inner(LoadToken &p_load_token, Erro
738735 *r_error = OK;
739736 }
740737
741- if (!p_load_token.local_path .is_empty ()) {
738+ ThreadLoadTask *load_task_ptr = nullptr ;
739+ if (p_load_token.task_if_unregistered ) {
740+ load_task_ptr = p_load_token.task_if_unregistered ;
741+ } else {
742742 if (!thread_load_tasks.has (p_load_token.local_path )) {
743743 if (r_error) {
744744 *r_error = ERR_BUG;
@@ -809,22 +809,14 @@ Ref<Resource> ResourceLoader::_load_complete_inner(LoadToken &p_load_token, Erro
809809 load_task.error = FAILED;
810810 }
811811
812- Ref<Resource> resource = load_task.resource ;
813- if (r_error) {
814- *r_error = load_task.error ;
815- }
816- return resource;
817- } else {
818- // Special case of an unregistered task.
819- // The resource should have been loaded by now.
820- Ref<Resource> resource = p_load_token.res_if_unregistered ;
821- if (!resource.is_valid ()) {
822- if (r_error) {
823- *r_error = FAILED;
824- }
825- }
826- return resource;
812+ load_task_ptr = &load_task;
813+ }
814+
815+ Ref<Resource> resource = load_task_ptr->resource ;
816+ if (r_error) {
817+ *r_error = load_task_ptr->error ;
827818 }
819+ return resource;
828820}
829821
830822bool ResourceLoader::_ensure_load_progress () {
0 commit comments