diff options
author | Juan Linietsky <reduzio@gmail.com> | 2018-09-02 13:59:33 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2018-09-02 14:00:16 -0300 |
commit | 19d57894d85b53218f1e4aa3cd5867ef4c5a0289 (patch) | |
tree | 0170d4e0712393e81bfb98030d81f58943ac0a24 | |
parent | 8af9d1fc03c49cef38efc4381a1f2ff3c921c520 (diff) |
Change ResourceLoader::load to make it more thread safe.
-rw-r--r-- | core/io/resource_loader.cpp | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index c01aff9144..66dc9730c5 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -202,12 +202,32 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p else local_path = ProjectSettings::get_singleton()->localize_path(p_path); - if (!p_no_cache && ResourceCache::has(local_path)) { + if (!p_no_cache) { + //lock first if possible + if (ResourceCache::lock) { + ResourceCache::lock->read_lock(); + } - print_verbose("Loading resource: " + local_path + " (cached)"); - if (r_error) - *r_error = OK; - return RES(ResourceCache::get(local_path)); + //get ptr + Resource **rptr = ResourceCache::resources.getptr(local_path); + + if (rptr) { + RES res(*rptr); + //it is possible this resource was just freed in a thread. If so, this referencing will not work and resource is considered not cached + if (res.is_valid()) { + //referencing is fine + if (r_error) + *r_error = OK; + if (ResourceCache::lock) { + ResourceCache::lock->read_unlock(); + } + print_verbose("Loading resource: " + local_path + " (cached)"); + return res; + } + } + if (ResourceCache::lock) { + ResourceCache::lock->read_unlock(); + } } bool xl_remapped = false; |