Avoid reentrant OBJTYPE_RLOCK in ClassDB

Fixes #43020 when a thread uses ClassDB while main thread calls is_parent_class().
This commit is contained in:
Lyuma 2020-11-01 00:44:15 -07:00
parent 7bc74b0fc8
commit 37b6a36cc4
2 changed files with 22 additions and 9 deletions

View File

@ -242,21 +242,25 @@ HashMap<StringName, ClassDB::ClassInfo> ClassDB::classes;
HashMap<StringName, StringName> ClassDB::resource_base_extensions;
HashMap<StringName, StringName> ClassDB::compat_classes;
bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) {
OBJTYPE_RLOCK;
bool ClassDB::_is_parent_class(const StringName &p_class, const StringName &p_inherits) {
StringName inherits = p_class;
while (inherits.operator String().length()) {
if (inherits == p_inherits) {
return true;
}
inherits = get_parent_class(inherits);
inherits = _get_parent_class(inherits);
}
return false;
}
bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) {
OBJTYPE_RLOCK;
return _is_parent_class(p_class, p_inherits);
}
void ClassDB::get_class_list(List<StringName> *p_classes) {
OBJTYPE_RLOCK;
@ -275,7 +279,7 @@ void ClassDB::get_inheriters_from_class(const StringName &p_class, List<StringNa
const StringName *k = nullptr;
while ((k = classes.next(k))) {
if (*k != p_class && is_parent_class(*k, p_class)) {
if (*k != p_class && _is_parent_class(*k, p_class)) {
p_classes->push_back(*k);
}
}
@ -287,7 +291,7 @@ void ClassDB::get_direct_inheriters_from_class(const StringName &p_class, List<S
const StringName *k = nullptr;
while ((k = classes.next(k))) {
if (*k != p_class && get_parent_class(*k) == p_class) {
if (*k != p_class && _get_parent_class(*k) == p_class) {
p_classes->push_back(*k);
}
}
@ -315,14 +319,18 @@ StringName ClassDB::get_compatibility_remapped_class(const StringName &p_class)
return p_class;
}
StringName ClassDB::get_parent_class(const StringName &p_class) {
OBJTYPE_RLOCK;
StringName ClassDB::_get_parent_class(const StringName &p_class) {
ClassInfo *ti = classes.getptr(p_class);
ERR_FAIL_COND_V_MSG(!ti, StringName(), "Cannot get class '" + String(p_class) + "'.");
return ti->inherits;
}
StringName ClassDB::get_parent_class(const StringName &p_class) {
OBJTYPE_RLOCK;
return _get_parent_class(p_class);
}
ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) {
OBJTYPE_RLOCK;

View File

@ -164,6 +164,11 @@ public:
static HashMap<StringName, HashMap<StringName, Variant>> default_values;
static Set<StringName> default_values_cached;
private:
// Non-locking variants of get_parent_class and is_parent_class.
static StringName _get_parent_class(const StringName &p_class);
static bool _is_parent_class(const StringName &p_class, const StringName &p_inherits);
public:
// DO NOT USE THIS!!!!!! NEEDS TO BE PUBLIC BUT DO NOT USE NO MATTER WHAT!!!
template <class T>