Skip to content

Commit 74b9c38

Browse files
committed
ResourceLoader: Add thread-aware resource changed mechanism
1 parent c450f4d commit 74b9c38

File tree

3 files changed

+107
-11
lines changed

3 files changed

+107
-11
lines changed

core/io/resource.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
#include <stdio.h>
4141

4242
void Resource::emit_changed() {
43-
if (ResourceLoader::is_within_load() && MessageQueue::get_main_singleton() != MessageQueue::get_singleton() && !MessageQueue::get_singleton()->is_flushing()) {
44-
// Let the connection happen on the call queue, later, since signals are not thread-safe.
45-
call_deferred("emit_signal", CoreStringName(changed));
46-
} else {
47-
emit_signal(CoreStringName(changed));
43+
if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) {
44+
ResourceLoader::resource_changed_emit(this);
45+
return;
4846
}
47+
48+
emit_signal(CoreStringName(changed));
4949
}
5050

5151
void Resource::_resource_path_changed() {
@@ -166,22 +166,22 @@ bool Resource::editor_can_reload_from_file() {
166166
}
167167

168168
void Resource::connect_changed(const Callable &p_callable, uint32_t p_flags) {
169-
if (ResourceLoader::is_within_load() && MessageQueue::get_main_singleton() != MessageQueue::get_singleton() && !MessageQueue::get_singleton()->is_flushing()) {
170-
// Let the check and connection happen on the call queue, later, since signals are not thread-safe.
171-
callable_mp(this, &Resource::connect_changed).call_deferred(p_callable, p_flags);
169+
if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) {
170+
ResourceLoader::resource_changed_connect(this, p_callable, p_flags);
172171
return;
173172
}
173+
174174
if (!is_connected(CoreStringName(changed), p_callable) || p_flags & CONNECT_REFERENCE_COUNTED) {
175175
connect(CoreStringName(changed), p_callable, p_flags);
176176
}
177177
}
178178

179179
void Resource::disconnect_changed(const Callable &p_callable) {
180-
if (ResourceLoader::is_within_load() && MessageQueue::get_main_singleton() != MessageQueue::get_singleton() && !MessageQueue::get_singleton()->is_flushing()) {
181-
// Let the check and disconnection happen on the call queue, later, since signals are not thread-safe.
182-
callable_mp(this, &Resource::disconnect_changed).call_deferred(p_callable);
180+
if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) {
181+
ResourceLoader::resource_changed_disconnect(this, p_callable);
183182
return;
184183
}
184+
185185
if (is_connected(CoreStringName(changed), p_callable)) {
186186
disconnect(CoreStringName(changed), p_callable);
187187
}

core/io/resource_loader.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
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

461467
static 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+
833916
Ref<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;
13601443
thread_local int ResourceLoader::load_nesting = 0;
13611444
thread_local Vector<String> ResourceLoader::load_paths_stack;
13621445
thread_local HashMap<int, HashMap<String, Ref<Resource>>> ResourceLoader::res_ref_overrides;
1446+
thread_local ResourceLoader::ThreadLoadTask *ResourceLoader::curr_load_task = nullptr;
13631447

13641448
SafeBinaryMutex<ResourceLoader::BINARY_MUTEX_TAG> &_get_res_loader_mutex() {
13651449
return ResourceLoader::thread_load_mutex;

core/io/resource_loader.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,21 @@ class ResourceLoader {
189189
Ref<Resource> resource;
190190
bool use_sub_threads = false;
191191
HashSet<String> sub_tasks;
192+
193+
struct ResourceChangedConnection {
194+
Resource *source = nullptr;
195+
Callable callable;
196+
uint32_t flags = 0;
197+
};
198+
LocalVector<ResourceChangedConnection> resource_changed_connections;
192199
};
193200

194201
static void _run_load_task(void *p_userdata);
195202

196203
static thread_local int load_nesting;
197204
static thread_local HashMap<int, HashMap<String, Ref<Resource>>> res_ref_overrides; // Outermost key is nesting level.
198205
static thread_local Vector<String> load_paths_stack;
206+
static thread_local ThreadLoadTask *curr_load_task;
199207

200208
static SafeBinaryMutex<BINARY_MUTEX_TAG> thread_load_mutex;
201209
friend SafeBinaryMutex<BINARY_MUTEX_TAG> &_get_res_loader_mutex();
@@ -216,6 +224,10 @@ class ResourceLoader {
216224

217225
static bool is_within_load() { return load_nesting > 0; };
218226

227+
static void resource_changed_connect(Resource *p_source, const Callable &p_callable, uint32_t p_flags);
228+
static void resource_changed_disconnect(Resource *p_source, const Callable &p_callable);
229+
static void resource_changed_emit(Resource *p_source);
230+
219231
static Ref<Resource> load(const String &p_path, const String &p_type_hint = "", ResourceFormatLoader::CacheMode p_cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE, Error *r_error = nullptr);
220232
static bool exists(const String &p_path, const String &p_type_hint = "");
221233

0 commit comments

Comments
 (0)