summaryrefslogtreecommitdiffstats
path: root/modules/gdscript/gdscript_cache.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gdscript/gdscript_cache.cpp')
-rw-r--r--modules/gdscript/gdscript_cache.cpp80
1 files changed, 64 insertions, 16 deletions
diff --git a/modules/gdscript/gdscript_cache.cpp b/modules/gdscript/gdscript_cache.cpp
index ac6f5f05c6..b3c0744bdf 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;
@@ -136,12 +136,22 @@ void GDScriptParserRef::clear() {
GDScriptParserRef::~GDScriptParserRef() {
clear();
- MutexLock lock(GDScriptCache::singleton->mutex);
- GDScriptCache::singleton->parser_map.erase(path);
+ if (!abandoned) {
+ MutexLock lock(GDScriptCache::singleton->mutex);
+ GDScriptCache::singleton->parser_map.erase(path);
+ }
}
GDScriptCache *GDScriptCache::singleton = nullptr;
+SafeBinaryMutex<GDScriptCache::BINARY_MUTEX_TAG> &_get_gdscript_cache_mutex() {
+ return GDScriptCache::mutex;
+}
+
+template <>
+thread_local SafeBinaryMutex<GDScriptCache::BINARY_MUTEX_TAG>::TLSData SafeBinaryMutex<GDScriptCache::BINARY_MUTEX_TAG>::tls_data(_get_gdscript_cache_mutex());
+SafeBinaryMutex<GDScriptCache::BINARY_MUTEX_TAG> GDScriptCache::mutex;
+
void GDScriptCache::move_script(const String &p_from, const String &p_to) {
if (singleton == nullptr || p_from == p_to) {
return;
@@ -153,10 +163,7 @@ void GDScriptCache::move_script(const String &p_from, const String &p_to) {
return;
}
- if (singleton->parser_map.has(p_from) && !p_from.is_empty()) {
- singleton->parser_map[p_to] = singleton->parser_map[p_from];
- }
- singleton->parser_map.erase(p_from);
+ remove_parser(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 +187,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 +214,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 +244,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 +375,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 +452,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);