Modernize Mutex

- Based on C++11's `mutex`
- No more need to allocate-deallocate or check for null
- No pointer anymore, just a member variable
- Platform-specific implementations no longer needed
- Simpler for `NO_THREADS`
- `BinaryMutex` added for special cases as the non-recursive version
- `MutexLock` now takes a reference. At this point the cases of null `Mutex`es are rare. If you ever need that, just don't use `MutexLock`.
- `ScopedMutexLock` is dropped and replaced by `MutexLock`, because they were pretty much the same.
This commit is contained in:
Pedro J. Estébanez
2021-02-18 11:58:08 +01:00
parent b450036120
commit 4ddcdc031b
99 changed files with 472 additions and 1391 deletions
+23 -27
View File
@@ -47,12 +47,10 @@ StringName _scs_create(const char *p_chr) {
}
bool StringName::configured = false;
Mutex *StringName::lock = NULL;
Mutex StringName::lock;
void StringName::setup() {
lock = Mutex::create();
ERR_FAIL_COND(configured);
for (int i = 0; i < STRING_TABLE_LEN; i++) {
@@ -63,7 +61,7 @@ void StringName::setup() {
void StringName::cleanup() {
lock->lock();
lock.lock();
int lost_strings = 0;
for (int i = 0; i < STRING_TABLE_LEN; i++) {
@@ -87,9 +85,7 @@ void StringName::cleanup() {
if (lost_strings) {
print_verbose("StringName: " + itos(lost_strings) + " unclaimed string names at exit.");
}
lock->unlock();
memdelete(lock);
lock.unlock();
}
void StringName::unref() {
@@ -98,7 +94,7 @@ void StringName::unref() {
if (_data && _data->refcount.unref()) {
lock->lock();
lock.lock();
if (_data->prev) {
_data->prev->next = _data->next;
@@ -113,7 +109,7 @@ void StringName::unref() {
_data->next->prev = _data->prev;
}
memdelete(_data);
lock->unlock();
lock.unlock();
}
_data = NULL;
@@ -184,7 +180,7 @@ StringName::StringName(const char *p_name) {
if (!p_name || p_name[0] == 0)
return; //empty, ignore
lock->lock();
lock.lock();
uint32_t hash = String::hash(p_name);
@@ -203,7 +199,7 @@ StringName::StringName(const char *p_name) {
if (_data) {
if (_data->refcount.ref()) {
// exists
lock->unlock();
lock.unlock();
return;
}
}
@@ -220,7 +216,7 @@ StringName::StringName(const char *p_name) {
_table[idx]->prev = _data;
_table[idx] = _data;
lock->unlock();
lock.unlock();
}
StringName::StringName(const StaticCString &p_static_string) {
@@ -231,7 +227,7 @@ StringName::StringName(const StaticCString &p_static_string) {
ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]);
lock->lock();
lock.lock();
uint32_t hash = String::hash(p_static_string.ptr);
@@ -250,7 +246,7 @@ StringName::StringName(const StaticCString &p_static_string) {
if (_data) {
if (_data->refcount.ref()) {
// exists
lock->unlock();
lock.unlock();
return;
}
}
@@ -267,7 +263,7 @@ StringName::StringName(const StaticCString &p_static_string) {
_table[idx]->prev = _data;
_table[idx] = _data;
lock->unlock();
lock.unlock();
}
StringName::StringName(const String &p_name) {
@@ -279,7 +275,7 @@ StringName::StringName(const String &p_name) {
if (p_name == String())
return;
lock->lock();
lock.lock();
uint32_t hash = p_name.hash();
@@ -297,7 +293,7 @@ StringName::StringName(const String &p_name) {
if (_data) {
if (_data->refcount.ref()) {
// exists
lock->unlock();
lock.unlock();
return;
}
}
@@ -314,7 +310,7 @@ StringName::StringName(const String &p_name) {
_table[idx]->prev = _data;
_table[idx] = _data;
lock->unlock();
lock.unlock();
}
StringName StringName::search(const char *p_name) {
@@ -325,7 +321,7 @@ StringName StringName::search(const char *p_name) {
if (!p_name[0])
return StringName();
lock->lock();
lock.lock();
uint32_t hash = String::hash(p_name);
@@ -342,12 +338,12 @@ StringName StringName::search(const char *p_name) {
}
if (_data && _data->refcount.ref()) {
lock->unlock();
lock.unlock();
return StringName(_data);
}
lock->unlock();
lock.unlock();
return StringName(); //does not exist
}
@@ -359,7 +355,7 @@ StringName StringName::search(const CharType *p_name) {
if (!p_name[0])
return StringName();
lock->lock();
lock.lock();
uint32_t hash = String::hash(p_name);
@@ -376,18 +372,18 @@ StringName StringName::search(const CharType *p_name) {
}
if (_data && _data->refcount.ref()) {
lock->unlock();
lock.unlock();
return StringName(_data);
}
lock->unlock();
lock.unlock();
return StringName(); //does not exist
}
StringName StringName::search(const String &p_name) {
ERR_FAIL_COND_V(p_name == "", StringName());
lock->lock();
lock.lock();
uint32_t hash = p_name.hash();
@@ -404,11 +400,11 @@ StringName StringName::search(const String &p_name) {
}
if (_data && _data->refcount.ref()) {
lock->unlock();
lock.unlock();
return StringName(_data);
}
lock->unlock();
lock.unlock();
return StringName(); //does not exist
}