From daa29d1007f1f3ab630be7f412aa09c714cdf037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Mon, 20 Feb 2023 18:57:31 +0100 Subject: [PATCH 1/3] Implement ConditionVariable --- core/os/condition_variable.h | 60 +++++++++++++++++++++++++++++++ core/os/mutex.h | 69 ++++++++++++++++++++++++++++++++---- 2 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 core/os/condition_variable.h diff --git a/core/os/condition_variable.h b/core/os/condition_variable.h new file mode 100644 index 00000000000..6037ff327dd --- /dev/null +++ b/core/os/condition_variable.h @@ -0,0 +1,60 @@ +/**************************************************************************/ +/* condition_variable.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#ifndef CONDITION_VARIABLE_H +#define CONDITION_VARIABLE_H + +#include + +// An object one or multiple threads can wait on a be notified by some other. +// Normally, you want to use a semaphore for such scenarios, but when the +// condition is something different than a count being greater than zero +// (which is the built-in logic in a semaphore) or you want to provide your +// own mutex to tie the wait-notify to some other behavior, you need to use this. + +class ConditionVariable { + mutable std::condition_variable condition; + +public: + template + _ALWAYS_INLINE_ void wait(const MutexLock &p_lock) const { + condition.wait(const_cast &>(p_lock.lock)); + } + + _ALWAYS_INLINE_ void notify_one() const { + condition.notify_one(); + } + + _ALWAYS_INLINE_ void notify_all() const { + condition.notify_all(); + } +}; + +#endif // CONDITION_VARIABLE_H diff --git a/core/os/mutex.h b/core/os/mutex.h index ceedcb235a2..90cc1632e8f 100644 --- a/core/os/mutex.h +++ b/core/os/mutex.h @@ -31,12 +31,20 @@ #ifndef MUTEX_H #define MUTEX_H +#include "core/error/error_macros.h" #include "core/typedefs.h" #include +template +class MutexLock; + template class MutexImpl { + friend class MutexLock>; + + using StdMutexType = StdMutexT; + mutable StdMutexT mutex; public: @@ -53,18 +61,65 @@ public: } }; +// A very special kind of mutex, used in scenarios where these +// requirements hold at the same time: +// - Must be used with a condition variable (only binary mutexes are suitable). +// - Must have recursive semnantics (or simulate, as this one does). +// The implementation keeps the lock count in TS. Therefore, only +// one object of each version of the template can exists; hence the Tag argument. +// Tags must be unique across the Godot codebase. +// Also, don't forget to declare the thread_local variable on each use. +template +class SafeBinaryMutex { + friend class MutexLock; + + using StdMutexType = std::mutex; + + mutable std::mutex mutex; + static thread_local uint32_t count; + +public: + _ALWAYS_INLINE_ void lock() const { + if (++count == 1) { + mutex.lock(); + } + } + + _ALWAYS_INLINE_ void unlock() const { + DEV_ASSERT(count); + if (--count == 0) { + mutex.unlock(); + } + } + + _ALWAYS_INLINE_ bool try_lock() const { + if (count) { + count++; + return true; + } else { + if (mutex.try_lock()) { + count++; + return true; + } else { + return false; + } + } + } + + ~SafeBinaryMutex() { + DEV_ASSERT(!count); + } +}; + template class MutexLock { - const MutexT &mutex; + friend class ConditionVariable; + + std::unique_lock lock; public: _ALWAYS_INLINE_ explicit MutexLock(const MutexT &p_mutex) : - mutex(p_mutex) { - mutex.lock(); - } - - _ALWAYS_INLINE_ ~MutexLock() { - mutex.unlock(); + lock(p_mutex.mutex) { } }; From 618bb173baee926f000bf5611c6aef5d597eeb22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Mon, 20 Feb 2023 19:00:26 +0100 Subject: [PATCH 2/3] Fix race condition in resource loader when a load task is reused --- core/io/resource_loader.cpp | 42 ++++++++++++++++--------------------- core/io/resource_loader.h | 9 +++++--- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index fc3547261bd..041758b1262 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()) { @@ -373,7 +372,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 +437,8 @@ ResourceLoader::ThreadLoadStatus ResourceLoader::load_threaded_get_status(const Ref 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 +447,10 @@ Ref 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++; - + //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 +472,13 @@ Ref 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(); + do { + load_task.cond_var->wait(thread_load_lock); + } while (load_task.cond_var); // In case of spurious wakeup. 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 (r_error) { *r_error = ERR_INVALID_PARAMETER; } @@ -507,8 +501,6 @@ Ref 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 +1059,7 @@ void ResourceLoader::remove_custom_loaders() { } void ResourceLoader::initialize() { - thread_load_mutex = memnew(Mutex); + thread_load_mutex = memnew(SafeBinaryMutex); thread_load_max = OS::get_singleton()->get_processor_count(); thread_loading_count = 0; thread_waiting_count = 0; @@ -1090,7 +1082,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::count = 0; +SafeBinaryMutex *ResourceLoader::thread_load_mutex = nullptr; HashMap ResourceLoader::thread_load_tasks; Semaphore *ResourceLoader::thread_load_semaphore = nullptr; diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h index c47b6c950a6..72c1f906531 100644 --- a/core/io/resource_loader.h +++ b/core/io/resource_loader.h @@ -37,6 +37,8 @@ #include "core/os/semaphore.h" #include "core/os/thread.h" +class ConditionVariable; + class ResourceFormatLoader : public RefCounted { GDCLASS(ResourceFormatLoader, RefCounted); @@ -105,6 +107,8 @@ public: THREAD_LOAD_LOADED }; + static const int BINARY_MUTEX_TAG = 1; + private: static Ref loader[MAX_LOADERS]; static int loader_count; @@ -136,7 +140,7 @@ private: struct ThreadLoadTask { Thread *thread = nullptr; Thread::ID loader_id = 0; - Semaphore *semaphore = nullptr; + ConditionVariable *cond_var = nullptr; String local_path; String remapped_path; String type_hint; @@ -149,12 +153,11 @@ private: bool use_sub_threads = false; bool start_next = true; int requests = 0; - int poll_requests = 0; HashSet sub_tasks; }; static void _thread_load_function(void *p_userdata); - static Mutex *thread_load_mutex; + static SafeBinaryMutex *thread_load_mutex; static HashMap thread_load_tasks; static Semaphore *thread_load_semaphore; static int thread_waiting_count; From b862fc8c9b0c2bf7a2f49a472bf7e2b3ce5925df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Mon, 20 Feb 2023 19:32:04 +0100 Subject: [PATCH 3/3] Fix cases of resource load tasks not being awaitable --- core/io/resource_loader.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index 041758b1262..22bf8571bf0 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -447,6 +447,17 @@ Ref ResourceLoader::load_threaded_get(const String &p_path, Error *r_e ThreadLoadTask &load_task = thread_load_tasks[local_path]; + if (!load_task.cond_var && load_task.status == THREAD_LOAD_IN_PROGRESS) { + // A condition variable was never created for this task. + // 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) { {