3131#include " resource_loader.h"
3232
3333#include " core/config/project_settings.h"
34+ #include " core/core_bind.h"
3435#include " core/io/file_access.h"
3536#include " core/io/resource_importer.h"
3637#include " core/object/script_language.h"
@@ -329,6 +330,9 @@ void ResourceLoader::_run_load_task(void *p_userdata) {
329330 }
330331 }
331332
333+ ThreadLoadTask *curr_load_task_backup = curr_load_task;
334+ curr_load_task = &load_task;
335+
332336 // Thread-safe either if it's the current thread or a brand new one.
333337 CallQueue *own_mq_override = nullptr ;
334338 if (load_nesting == 0 ) {
@@ -456,6 +460,8 @@ void ResourceLoader::_run_load_task(void *p_userdata) {
456460 }
457461 DEV_ASSERT (load_paths_stack.is_empty ());
458462 }
463+
464+ curr_load_task = curr_load_task_backup;
459465}
460466
461467static String _validate_local_path (const String &p_path) {
@@ -816,6 +822,39 @@ Ref<Resource> ResourceLoader::_load_complete_inner(LoadToken &p_load_token, Erro
816822 if (r_error) {
817823 *r_error = load_task_ptr->error ;
818824 }
825+
826+ if (resource.is_valid ()) {
827+ if (curr_load_task) {
828+ // A task awaiting another => Let the awaiter accumulate the resource changed connections.
829+ DEV_ASSERT (curr_load_task != load_task_ptr);
830+ for (const ThreadLoadTask::ResourceChangedConnection &rcc : load_task_ptr->resource_changed_connections ) {
831+ curr_load_task->resource_changed_connections .push_back (rcc);
832+ }
833+ } else {
834+ // A leaf task being awaited => Propagate the resource changed connections.
835+ if (Thread::is_main_thread ()) {
836+ // On the main thread it's safe to migrate the connections to the standard signal mechanism.
837+ for (const ThreadLoadTask::ResourceChangedConnection &rcc : load_task_ptr->resource_changed_connections ) {
838+ if (rcc.callable .is_valid ()) {
839+ rcc.source ->connect_changed (rcc.callable , rcc.flags );
840+ }
841+ }
842+ } else {
843+ // On non-main threads, we have to queue and call it done when processed.
844+ if (!load_task_ptr->resource_changed_connections .is_empty ()) {
845+ for (const ThreadLoadTask::ResourceChangedConnection &rcc : load_task_ptr->resource_changed_connections ) {
846+ if (rcc.callable .is_valid ()) {
847+ MessageQueue::get_main_singleton ()->push_callable (callable_mp (rcc.source , &Resource::connect_changed).bind (rcc.callable , rcc.flags ));
848+ }
849+ }
850+ core_bind::Semaphore done;
851+ MessageQueue::get_main_singleton ()->push_callable (callable_mp (&done, &core_bind::Semaphore::post ));
852+ done.wait ();
853+ }
854+ }
855+ }
856+ }
857+
819858 return resource;
820859}
821860
@@ -830,6 +869,50 @@ bool ResourceLoader::_ensure_load_progress() {
830869 return true ;
831870}
832871
872+ void ResourceLoader::resource_changed_connect (Resource *p_source, const Callable &p_callable, uint32_t p_flags) {
873+ print_lt (vformat (" %d\t %ud:%s\t " FUNCTION_STR " \t %d" , Thread::get_caller_id (), p_source->get_instance_id (), p_source->get_class (), p_callable.get_object_id ()));
874+
875+ MutexLock lock (thread_load_mutex);
876+
877+ for (const ThreadLoadTask::ResourceChangedConnection &rcc : curr_load_task->resource_changed_connections ) {
878+ if (unlikely (rcc.source == p_source && rcc.callable == p_callable)) {
879+ return ;
880+ }
881+ }
882+
883+ ThreadLoadTask::ResourceChangedConnection rcc;
884+ rcc.source = p_source;
885+ rcc.callable = p_callable;
886+ rcc.flags = p_flags;
887+ curr_load_task->resource_changed_connections .push_back (rcc);
888+ }
889+
890+ void ResourceLoader::resource_changed_disconnect (Resource *p_source, const Callable &p_callable) {
891+ print_lt (vformat (" %d\t %ud:%s\t " FUNCTION_STR " t%d" , Thread::get_caller_id (), p_source->get_instance_id (), p_source->get_class (), p_callable.get_object_id ()));
892+
893+ MutexLock lock (thread_load_mutex);
894+
895+ for (uint32_t i = 0 ; i < curr_load_task->resource_changed_connections .size (); ++i) {
896+ const ThreadLoadTask::ResourceChangedConnection &rcc = curr_load_task->resource_changed_connections [i];
897+ if (unlikely (rcc.source == p_source && rcc.callable == p_callable)) {
898+ curr_load_task->resource_changed_connections .remove_at_unordered (i);
899+ return ;
900+ }
901+ }
902+ }
903+
904+ void ResourceLoader::resource_changed_emit (Resource *p_source) {
905+ print_lt (vformat (" %d\t %ud:%s\t " FUNCTION_STR, Thread::get_caller_id (), p_source->get_instance_id (), p_source->get_class ()));
906+
907+ MutexLock lock (thread_load_mutex);
908+
909+ for (const ThreadLoadTask::ResourceChangedConnection &rcc : curr_load_task->resource_changed_connections ) {
910+ if (unlikely (rcc.source == p_source)) {
911+ rcc.callable .call ();
912+ }
913+ }
914+ }
915+
833916Ref<Resource> ResourceLoader::ensure_resource_ref_override_for_outer_load (const String &p_path, const String &p_res_type) {
834917 ERR_FAIL_COND_V (load_nesting == 0 , Ref<Resource>()); // It makes no sense to use this from nesting level 0.
835918 const String &local_path = _validate_local_path (p_path);
@@ -1360,6 +1443,7 @@ bool ResourceLoader::timestamp_on_load = false;
13601443thread_local int ResourceLoader::load_nesting = 0 ;
13611444thread_local Vector<String> ResourceLoader::load_paths_stack;
13621445thread_local HashMap<int , HashMap<String, Ref<Resource>>> ResourceLoader::res_ref_overrides;
1446+ thread_local ResourceLoader::ThreadLoadTask *ResourceLoader::curr_load_task = nullptr ;
13631447
13641448SafeBinaryMutex<ResourceLoader::BINARY_MUTEX_TAG> &_get_res_loader_mutex () {
13651449 return ResourceLoader::thread_load_mutex;
0 commit comments