Skip to content

Commit e574279

Browse files
committed
Use BinaryMutex instead of Mutex for StringName.
1 parent 67c96c8 commit e574279

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

core/object/object.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,6 +2226,16 @@ void Object::detach_from_objectdb() {
22262226
}
22272227
}
22282228

2229+
void Object::assign_class_name_static(const Span<char> &p_name, StringName &r_target) {
2230+
static BinaryMutex _mutex;
2231+
MutexLock lock(_mutex);
2232+
if (r_target) {
2233+
// Already assigned while we were waiting for the mutex.
2234+
return;
2235+
}
2236+
r_target = StringName(p_name.ptr(), true);
2237+
}
2238+
22292239
Object::~Object() {
22302240
if (script_instance) {
22312241
memdelete(script_instance);

core/object/object.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ public:
425425
static const StringName &get_class_static() { \
426426
static StringName _class_name_static; \
427427
if (unlikely(!_class_name_static)) { \
428-
StringName::assign_static_unique_class_name(&_class_name_static, #m_class); \
428+
assign_class_name_static(#m_class, _class_name_static); \
429429
} \
430430
return _class_name_static; \
431431
} \
@@ -811,10 +811,12 @@ class Object {
811811
};
812812

813813
/* TYPE API */
814+
static void assign_class_name_static(const Span<char> &p_name, StringName &r_target);
815+
814816
static const StringName &get_class_static() {
815817
static StringName _class_name_static;
816818
if (unlikely(!_class_name_static)) {
817-
StringName::assign_static_unique_class_name(&_class_name_static, "Object");
819+
assign_class_name_static("Object", _class_name_static);
818820
}
819821
return _class_name_static;
820822
}

core/string/string_name.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct StringName::Table {
3939
constexpr static uint32_t TABLE_MASK = TABLE_LEN - 1;
4040

4141
static inline _Data *table[TABLE_LEN];
42-
static inline Mutex mutex;
42+
static inline BinaryMutex mutex;
4343
static inline PagedAllocator<_Data> allocator;
4444
};
4545

@@ -208,13 +208,6 @@ StringName::StringName(const StringName &p_name) {
208208
}
209209
}
210210

211-
void StringName::assign_static_unique_class_name(StringName *ptr, const char *p_name) {
212-
MutexLock lock(Table::mutex);
213-
if (*ptr == StringName()) {
214-
*ptr = StringName(p_name, true);
215-
}
216-
}
217-
218211
StringName::StringName(const char *p_name, bool p_static) {
219212
_data = nullptr;
220213

core/string/string_name.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,6 @@ class StringName {
178178
StringName(const String &p_name, bool p_static = false);
179179
StringName() {}
180180

181-
static void assign_static_unique_class_name(StringName *ptr, const char *p_name);
182-
183181
#ifdef SIZE_EXTRA
184182
_NO_INLINE_
185183
#else

0 commit comments

Comments
 (0)