diff options
author | Pedro J. Estébanez <pedrojrulez@gmail.com> | 2020-02-26 11:28:13 +0100 |
---|---|---|
committer | Pedro J. Estébanez <pedrojrulez@gmail.com> | 2020-02-26 20:40:10 +0100 |
commit | 18fbdbb456c07a56b358bea2e392765fbcbb3283 (patch) | |
tree | 737363d20493afe45e75d932e0c1957dd9a79589 /core/io/resource_loader.cpp | |
parent | 1e57b558f215dd4920768e9567b6f55825877c89 (diff) |
Reimplement Mutex with C++'s <mutex>
Main:
- It's now implemented thanks to `<mutex>`. No more platform-specific implementations.
- `BinaryMutex` (non-recursive) is added, as an alternative for special cases.
- Doesn't need allocation/deallocation anymore. It can live in the stack and be part of other classes.
- Because of that, it's methods are now `const` and the inner mutex is `mutable` so it can be easily used in `const` contexts.
- A no-op implementation is provided if `NO_THREADS` is defined. No more need to add `#ifdef NO_THREADS` just for this.
- `MutexLock` now takes a reference. At this point the cases of null `Mutex`es are rare. If you ever need that, just don't use `MutexLock`.
- Thread-safe utilities are therefore simpler now.
Misc.:
- `ScopedMutexLock` is dropped and replaced by `MutexLock`, because they were pretty much the same.
- Every case of lock, do-something, unlock is replaced by `MutexLock` (complex cases where it's not straightfoward are kept as as explicit lock and unlock).
- `ShaderRD` contained an `std::mutex`, which has been replaced by `Mutex`.
Diffstat (limited to 'core/io/resource_loader.cpp')
-rw-r--r-- | core/io/resource_loader.cpp | 31 |
1 files changed, 4 insertions, 27 deletions
diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index 39bbebefa6..b43cd7f1a2 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -288,9 +288,7 @@ RES ResourceLoader::_load(const String &p_path, const String &p_original_path, c bool ResourceLoader::_add_to_loading_map(const String &p_path) { bool success; - if (loading_map_mutex) { - loading_map_mutex->lock(); - } + MutexLock lock(loading_map_mutex); LoadingMapKey key; key.path = p_path; @@ -303,43 +301,27 @@ bool ResourceLoader::_add_to_loading_map(const String &p_path) { success = true; } - if (loading_map_mutex) { - loading_map_mutex->unlock(); - } - return success; } void ResourceLoader::_remove_from_loading_map(const String &p_path) { - if (loading_map_mutex) { - loading_map_mutex->lock(); - } + MutexLock lock(loading_map_mutex); LoadingMapKey key; key.path = p_path; key.thread = Thread::get_caller_id(); loading_map.erase(key); - - if (loading_map_mutex) { - loading_map_mutex->unlock(); - } } void ResourceLoader::_remove_from_loading_map_and_thread(const String &p_path, Thread::ID p_thread) { - if (loading_map_mutex) { - loading_map_mutex->lock(); - } + MutexLock lock(loading_map_mutex); LoadingMapKey key; key.path = p_path; key.thread = p_thread; loading_map.erase(key); - - if (loading_map_mutex) { - loading_map_mutex->unlock(); - } } RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) { @@ -1002,13 +984,10 @@ void ResourceLoader::remove_custom_loaders() { } } -Mutex *ResourceLoader::loading_map_mutex = NULL; +Mutex ResourceLoader::loading_map_mutex; HashMap<ResourceLoader::LoadingMapKey, int, ResourceLoader::LoadingMapKeyHasher> ResourceLoader::loading_map; void ResourceLoader::initialize() { -#ifndef NO_THREADS - loading_map_mutex = Mutex::create(); -#endif } void ResourceLoader::finalize() { @@ -1018,8 +997,6 @@ void ResourceLoader::finalize() { ERR_PRINT("Exited while resource is being loaded: " + K->path); } loading_map.clear(); - memdelete(loading_map_mutex); - loading_map_mutex = NULL; #endif } |