summaryrefslogtreecommitdiffstats
path: root/core/io/resource_loader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/io/resource_loader.cpp')
-rw-r--r--core/io/resource_loader.cpp78
1 files changed, 47 insertions, 31 deletions
diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp
index fc3547261b..a46fac4e7a 100644
--- a/core/io/resource_loader.cpp
+++ b/core/io/resource_loader.cpp
@@ -33,6 +33,7 @@
#include "core/config/project_settings.h"
#include "core/io/file_access.h"
#include "core/io/resource_importer.h"
+#include "core/os/condition_variable.h"
#include "core/os/os.h"
#include "core/string/print_string.h"
#include "core/string/translation.h"
@@ -233,7 +234,7 @@ void ResourceLoader::_thread_load_function(void *p_userdata) {
ThreadLoadTask &load_task = *(ThreadLoadTask *)p_userdata;
load_task.loader_id = Thread::get_caller_id();
- if (load_task.semaphore) {
+ if (load_task.cond_var) {
//this is an actual thread, so wait for Ok from semaphore
thread_load_semaphore->wait(); //wait until its ok to start loading
}
@@ -247,7 +248,7 @@ void ResourceLoader::_thread_load_function(void *p_userdata) {
} else {
load_task.status = THREAD_LOAD_LOADED;
}
- if (load_task.semaphore) {
+ if (load_task.cond_var) {
if (load_task.start_next && thread_waiting_count > 0) {
thread_waiting_count--;
//thread loading count remains constant, this ends but another one begins
@@ -258,11 +259,9 @@ void ResourceLoader::_thread_load_function(void *p_userdata) {
print_lt("END: load count: " + itos(thread_loading_count) + " / wait count: " + itos(thread_waiting_count) + " / suspended count: " + itos(thread_suspended_count) + " / active: " + itos(thread_loading_count - thread_suspended_count));
- for (int i = 0; i < load_task.poll_requests; i++) {
- load_task.semaphore->post();
- }
- memdelete(load_task.semaphore);
- load_task.semaphore = nullptr;
+ load_task.cond_var->notify_all();
+ memdelete(load_task.cond_var);
+ load_task.cond_var = nullptr;
}
if (load_task.resource.is_valid()) {
@@ -311,12 +310,6 @@ Error ResourceLoader::load_threaded_request(const String &p_path, const String &
thread_load_mutex->unlock();
ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "There is no thread loading source resource '" + p_source_resource + "'.");
}
- //must be loading from this thread
- if (thread_load_tasks[p_source_resource].loader_id != Thread::get_caller_id()) {
- thread_load_mutex->unlock();
- ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Threading loading resource'" + local_path + " failed: Source specified: '" + p_source_resource + "' but was not called by it.");
- }
-
//must not be already added as s sub tasks
if (thread_load_tasks[p_source_resource].sub_tasks.has(local_path)) {
thread_load_mutex->unlock();
@@ -373,7 +366,7 @@ Error ResourceLoader::load_threaded_request(const String &p_path, const String &
if (load_task.resource.is_null()) { //needs to be loaded in thread
- load_task.semaphore = memnew(Semaphore);
+ load_task.cond_var = memnew(ConditionVariable);
if (thread_loading_count < thread_load_max) {
thread_loading_count++;
thread_load_semaphore->post(); //we have free threads, so allow one
@@ -438,9 +431,8 @@ ResourceLoader::ThreadLoadStatus ResourceLoader::load_threaded_get_status(const
Ref<Resource> ResourceLoader::load_threaded_get(const String &p_path, Error *r_error) {
String local_path = _validate_local_path(p_path);
- thread_load_mutex->lock();
+ MutexLock thread_load_lock(*thread_load_mutex);
if (!thread_load_tasks.has(local_path)) {
- thread_load_mutex->unlock();
if (r_error) {
*r_error = ERR_INVALID_PARAMETER;
}
@@ -449,13 +441,30 @@ Ref<Resource> ResourceLoader::load_threaded_get(const String &p_path, Error *r_e
ThreadLoadTask &load_task = thread_load_tasks[local_path];
- //semaphore still exists, meaning it's still loading, request poll
- Semaphore *semaphore = load_task.semaphore;
- if (semaphore) {
- load_task.poll_requests++;
+ if (load_task.status == THREAD_LOAD_IN_PROGRESS) {
+ if (load_task.loader_id == Thread::get_caller_id()) {
+ // Load is in progress, but it's precisely this thread the one in charge.
+ // That means this is a cyclic load.
+ if (r_error) {
+ *r_error = ERR_BUSY;
+ }
+ return Ref<Resource>();
+ } else if (!load_task.cond_var) {
+ // Load is in progress, but a condition variable was never created for it.
+ // That happens when a load has been initiated with subthreads disabled,
+ // but now another load thread needs to interact with this one (either
+ // because of subthreads being used this time, or because it's simply a
+ // threaded load running on a different thread).
+ // Since we want to be notified when the load ends, we must create the
+ // condition variable now.
+ load_task.cond_var = memnew(ConditionVariable);
+ }
+ }
+ //cond var still exists, meaning it's still loading, request poll
+ if (load_task.cond_var) {
{
- // As we got a semaphore, this means we are going to have to wait
+ // As we got a cond var, this means we are going to have to wait
// until the sub-resource is done loading
//
// As this thread will become 'blocked' we should "exchange" its
@@ -477,14 +486,21 @@ Ref<Resource> ResourceLoader::load_threaded_get(const String &p_path, Error *r_e
print_lt("GET: load count: " + itos(thread_loading_count) + " / wait count: " + itos(thread_waiting_count) + " / suspended count: " + itos(thread_suspended_count) + " / active: " + itos(thread_loading_count - thread_suspended_count));
}
- thread_load_mutex->unlock();
- semaphore->wait();
- thread_load_mutex->lock();
+ bool still_valid = true;
+ bool was_thread = load_task.thread;
+ do {
+ load_task.cond_var->wait(thread_load_lock);
+ if (!thread_load_tasks.has(local_path)) { //may have been erased during unlock and this was always an invalid call
+ still_valid = false;
+ break;
+ }
+ } while (load_task.cond_var); // In case of spurious wakeup.
- thread_suspended_count--;
+ if (was_thread) {
+ thread_suspended_count--;
+ }
- if (!thread_load_tasks.has(local_path)) { //may have been erased during unlock and this was always an invalid call
- thread_load_mutex->unlock();
+ if (!still_valid) {
if (r_error) {
*r_error = ERR_INVALID_PARAMETER;
}
@@ -507,8 +523,6 @@ Ref<Resource> ResourceLoader::load_threaded_get(const String &p_path, Error *r_e
thread_load_tasks.erase(local_path);
}
- thread_load_mutex->unlock();
-
return resource;
}
@@ -1067,7 +1081,7 @@ void ResourceLoader::remove_custom_loaders() {
}
void ResourceLoader::initialize() {
- thread_load_mutex = memnew(Mutex);
+ thread_load_mutex = memnew(SafeBinaryMutex<BINARY_MUTEX_TAG>);
thread_load_max = OS::get_singleton()->get_processor_count();
thread_loading_count = 0;
thread_waiting_count = 0;
@@ -1090,7 +1104,9 @@ bool ResourceLoader::create_missing_resources_if_class_unavailable = false;
bool ResourceLoader::abort_on_missing_resource = true;
bool ResourceLoader::timestamp_on_load = false;
-Mutex *ResourceLoader::thread_load_mutex = nullptr;
+template <>
+thread_local uint32_t SafeBinaryMutex<ResourceLoader::BINARY_MUTEX_TAG>::count = 0;
+SafeBinaryMutex<ResourceLoader::BINARY_MUTEX_TAG> *ResourceLoader::thread_load_mutex = nullptr;
HashMap<String, ResourceLoader::ThreadLoadTask> ResourceLoader::thread_load_tasks;
Semaphore *ResourceLoader::thread_load_semaphore = nullptr;