diff options
Diffstat (limited to 'modules/gdscript/gdscript_cache.cpp')
-rw-r--r-- | modules/gdscript/gdscript_cache.cpp | 72 |
1 files changed, 59 insertions, 13 deletions
diff --git a/modules/gdscript/gdscript_cache.cpp b/modules/gdscript/gdscript_cache.cpp index ac6f5f05c6..2b1184bcb9 100644 --- a/modules/gdscript/gdscript_cache.cpp +++ b/modules/gdscript/gdscript_cache.cpp @@ -42,6 +42,10 @@ GDScriptParserRef::Status GDScriptParserRef::get_status() const { return status; } +String GDScriptParserRef::get_path() const { + return path; +} + uint32_t GDScriptParserRef::get_source_hash() const { return source_hash; } @@ -91,12 +95,8 @@ Error GDScriptParserRef::raise_status(Status p_new_status) { result = get_analyzer()->resolve_interface(); } break; case INTERFACE_SOLVED: { - status = BODY_SOLVED; - result = get_analyzer()->resolve_body(); - } break; - case BODY_SOLVED: { status = FULLY_SOLVED; - result = get_analyzer()->resolve_dependencies(); + result = get_analyzer()->resolve_body(); } break; case FULLY_SOLVED: { return result; @@ -135,9 +135,9 @@ void GDScriptParserRef::clear() { GDScriptParserRef::~GDScriptParserRef() { clear(); - - MutexLock lock(GDScriptCache::singleton->mutex); - GDScriptCache::singleton->parser_map.erase(path); + if (!abandoned) { + GDScriptCache::remove_parser(path); + } } GDScriptCache *GDScriptCache::singleton = nullptr; @@ -158,6 +158,11 @@ void GDScriptCache::move_script(const String &p_from, const String &p_to) { } singleton->parser_map.erase(p_from); + if (singleton->parser_inverse_dependencies.has(p_from) && !p_from.is_empty()) { + singleton->parser_inverse_dependencies[p_to] = singleton->parser_inverse_dependencies[p_from]; + } + singleton->parser_inverse_dependencies.erase(p_from); + if (singleton->shallow_gdscript_cache.has(p_from) && !p_from.is_empty()) { singleton->shallow_gdscript_cache[p_to] = singleton->shallow_gdscript_cache[p_from]; } @@ -180,14 +185,23 @@ void GDScriptCache::remove_script(const String &p_path) { return; } + if (HashMap<String, Vector<ObjectID>>::Iterator E = singleton->abandoned_parser_map.find(p_path)) { + for (ObjectID parser_ref_id : E->value) { + Ref<GDScriptParserRef> parser_ref{ ObjectDB::get_instance(parser_ref_id) }; + if (parser_ref.is_valid()) { + parser_ref->clear(); + } + } + } + + singleton->abandoned_parser_map.erase(p_path); + if (singleton->parser_map.has(p_path)) { - // Keep a local reference until it goes out of scope. - // Clearing it can trigger a reference to itself to go out of scope, destructing it before clear finishes. - Ref<GDScriptParserRef> parser_ref = singleton->parser_map[p_path]; - singleton->parser_map.erase(p_path); - parser_ref->clear(); + singleton->parser_map[p_path]->clear(); } + remove_parser(p_path); + singleton->dependencies.erase(p_path); singleton->shallow_gdscript_cache.erase(p_path); singleton->full_gdscript_cache.erase(p_path); @@ -198,6 +212,7 @@ Ref<GDScriptParserRef> GDScriptCache::get_parser(const String &p_path, GDScriptP Ref<GDScriptParserRef> ref; if (!p_owner.is_empty()) { singleton->dependencies[p_owner].insert(p_path); + singleton->parser_inverse_dependencies[p_path].insert(p_owner); } if (singleton->parser_map.has(p_path)) { ref = Ref<GDScriptParserRef>(singleton->parser_map[p_path]); @@ -227,8 +242,22 @@ bool GDScriptCache::has_parser(const String &p_path) { void GDScriptCache::remove_parser(const String &p_path) { MutexLock lock(singleton->mutex); + + if (singleton->parser_map.has(p_path)) { + GDScriptParserRef *parser_ref = singleton->parser_map[p_path]; + parser_ref->abandoned = true; + singleton->abandoned_parser_map[p_path].push_back(parser_ref->get_instance_id()); + } + // Can't clear the parser because some other parser might be currently using it in the chain of calls. singleton->parser_map.erase(p_path); + + // Have to copy while iterating, because parser_inverse_dependencies is modified. + HashSet<String> ideps = singleton->parser_inverse_dependencies[p_path]; + singleton->parser_inverse_dependencies.erase(p_path); + for (String idep_path : ideps) { + remove_parser(idep_path); + } } String GDScriptCache::get_source_code(const String &p_path) { @@ -344,7 +373,11 @@ Ref<GDScript> GDScriptCache::get_full_script(const String &p_path, Error &r_erro } } + // Allowing lifting the lock might cause a script to be reloaded multiple times, + // which, as a last resort deadlock prevention strategy, is a good tradeoff. + uint32_t allowance_id = WorkerThreadPool::thread_enter_unlock_allowance_zone(&singleton->mutex); r_error = script->reload(true); + WorkerThreadPool::thread_exit_unlock_allowance_zone(allowance_id); if (r_error) { return script; } @@ -417,6 +450,19 @@ void GDScriptCache::clear() { } singleton->cleared = true; + singleton->parser_inverse_dependencies.clear(); + + for (const KeyValue<String, Vector<ObjectID>> &KV : singleton->abandoned_parser_map) { + for (ObjectID parser_ref_id : KV.value) { + Ref<GDScriptParserRef> parser_ref{ ObjectDB::get_instance(parser_ref_id) }; + if (parser_ref.is_valid()) { + parser_ref->clear(); + } + } + } + + singleton->abandoned_parser_map.clear(); + RBSet<Ref<GDScriptParserRef>> parser_map_refs; for (KeyValue<String, GDScriptParserRef *> &E : singleton->parser_map) { parser_map_refs.insert(E.value); |