Skip to content

Commit

Permalink
ResourceLoader: Revert workaround resource loading crashes due to bug…
Browse files Browse the repository at this point in the history
…gy TLS

This reverts commit 41c0785.
  • Loading branch information
RandomShaper committed Jul 3, 2024
1 parent 6a13fdc commit 0370ee8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
16 changes: 7 additions & 9 deletions core/io/resource_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ ResourceLoader::LoadToken::~LoadToken() {
Ref<Resource> ResourceLoader::_load(const String &p_path, const String &p_original_path, const String &p_type_hint, ResourceFormatLoader::CacheMode p_cache_mode, Error *r_error, bool p_use_sub_threads, float *r_progress) {
const String &original_path = p_original_path.is_empty() ? p_path : p_original_path;
load_nesting++;
if (load_paths_stack->size()) {
if (load_paths_stack.size()) {
thread_load_mutex.lock();
const String &parent_task_path = load_paths_stack->get(load_paths_stack->size() - 1);
const String &parent_task_path = load_paths_stack.get(load_paths_stack.size() - 1);
HashMap<String, ThreadLoadTask>::Iterator E = thread_load_tasks.find(parent_task_path);
// Avoid double-tracking, for progress reporting, resources that boil down to a remapped path containing the real payload (e.g., imported resources).
bool is_remapped_load = original_path == parent_task_path;
Expand All @@ -256,7 +256,7 @@ Ref<Resource> ResourceLoader::_load(const String &p_path, const String &p_origin
}
thread_load_mutex.unlock();
}
load_paths_stack->push_back(original_path);
load_paths_stack.push_back(original_path);

// Try all loaders and pick the first match for the type hint
bool found = false;
Expand All @@ -272,7 +272,7 @@ Ref<Resource> ResourceLoader::_load(const String &p_path, const String &p_origin
}
}

load_paths_stack->resize(load_paths_stack->size() - 1);
load_paths_stack.resize(load_paths_stack.size() - 1);
res_ref_overrides.erase(load_nesting);
load_nesting--;

Expand Down Expand Up @@ -307,10 +307,8 @@ void ResourceLoader::_thread_load_function(void *p_userdata) {
bool mq_override_present = false;
CallQueue *own_mq_override = nullptr;
if (load_nesting == 0) {
load_paths_stack = memnew(Vector<String>);

if (!load_task.dependent_path.is_empty()) {
load_paths_stack->push_back(load_task.dependent_path);
load_paths_stack.push_back(load_task.dependent_path);
}
if (!Thread::is_main_thread()) {
// Let the caller thread use its own, for added flexibility. Provide one otherwise.
Expand Down Expand Up @@ -405,7 +403,7 @@ void ResourceLoader::_thread_load_function(void *p_userdata) {
MessageQueue::set_thread_singleton_override(nullptr);
memdelete(own_mq_override);
}
memdelete(load_paths_stack);
DEV_ASSERT(load_paths_stack.is_empty());
}
}

Expand Down Expand Up @@ -1293,7 +1291,7 @@ bool ResourceLoader::timestamp_on_load = false;

thread_local int ResourceLoader::load_nesting = 0;
thread_local WorkerThreadPool::TaskID ResourceLoader::caller_task_id = 0;
thread_local Vector<String> *ResourceLoader::load_paths_stack;
thread_local Vector<String> ResourceLoader::load_paths_stack;
thread_local HashMap<int, HashMap<String, Ref<Resource>>> ResourceLoader::res_ref_overrides;

template <>
Expand Down
2 changes: 1 addition & 1 deletion core/io/resource_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class ResourceLoader {
static thread_local int load_nesting;
static thread_local WorkerThreadPool::TaskID caller_task_id;
static thread_local HashMap<int, HashMap<String, Ref<Resource>>> res_ref_overrides; // Outermost key is nesting level.
static thread_local Vector<String> *load_paths_stack; // A pointer to avoid broken TLS implementations from double-running the destructor.
static thread_local Vector<String> load_paths_stack;
static SafeBinaryMutex<BINARY_MUTEX_TAG> thread_load_mutex;
static HashMap<String, ThreadLoadTask> thread_load_tasks;
static bool cleaning_tasks;
Expand Down

0 comments on commit 0370ee8

Please sign in to comment.