mirror of
https://github.com/godotengine/godot.git
synced 2026-01-03 18:11:19 +03:00
Reimplement Mutex with C++'s <mutex>
Main: - It's now implemented thanks to `<mutex>`. No more platform-specific implementations. - `BinaryMutex` (non-recursive) is added, as an alternative for special cases. - Doesn't need allocation/deallocation anymore. It can live in the stack and be part of other classes. - Because of that, it's methods are now `const` and the inner mutex is `mutable` so it can be easily used in `const` contexts. - A no-op implementation is provided if `NO_THREADS` is defined. No more need to add `#ifdef NO_THREADS` just for this. - `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`. - Thread-safe utilities are therefore simpler now. Misc.: - `ScopedMutexLock` is dropped and replaced by `MutexLock`, because they were pretty much the same. - Every case of lock, do-something, unlock is replaced by `MutexLock` (complex cases where it's not straightfoward are kept as as explicit lock and unlock). - `ShaderRD` contained an `std::mutex`, which has been replaced by `Mutex`.
This commit is contained in:
@@ -70,7 +70,7 @@ struct _IP_ResolverPrivate {
|
||||
return IP::RESOLVER_INVALID_ID;
|
||||
}
|
||||
|
||||
Mutex *mutex;
|
||||
Mutex mutex;
|
||||
SemaphoreOld *sem;
|
||||
|
||||
Thread *thread;
|
||||
@@ -100,9 +100,8 @@ struct _IP_ResolverPrivate {
|
||||
|
||||
ipr->sem->wait();
|
||||
|
||||
ipr->mutex->lock();
|
||||
MutexLock lock(ipr->mutex);
|
||||
ipr->resolve_queues();
|
||||
ipr->mutex->unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,30 +114,27 @@ struct _IP_ResolverPrivate {
|
||||
|
||||
IP_Address IP::resolve_hostname(const String &p_hostname, IP::Type p_type) {
|
||||
|
||||
resolver->mutex->lock();
|
||||
MutexLock lock(resolver->mutex);
|
||||
|
||||
String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
|
||||
if (resolver->cache.has(key) && resolver->cache[key].is_valid()) {
|
||||
IP_Address res = resolver->cache[key];
|
||||
resolver->mutex->unlock();
|
||||
return res;
|
||||
}
|
||||
|
||||
IP_Address res = _resolve_hostname(p_hostname, p_type);
|
||||
resolver->cache[key] = res;
|
||||
resolver->mutex->unlock();
|
||||
return res;
|
||||
}
|
||||
|
||||
IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Type p_type) {
|
||||
|
||||
resolver->mutex->lock();
|
||||
MutexLock lock(resolver->mutex);
|
||||
|
||||
ResolverID id = resolver->find_empty_id();
|
||||
|
||||
if (id == RESOLVER_INVALID_ID) {
|
||||
WARN_PRINT("Out of resolver queries");
|
||||
resolver->mutex->unlock();
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -157,7 +153,6 @@ IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Typ
|
||||
resolver->resolve_queues();
|
||||
}
|
||||
|
||||
resolver->mutex->unlock();
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -165,50 +160,43 @@ IP::ResolverStatus IP::get_resolve_item_status(ResolverID p_id) const {
|
||||
|
||||
ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP::RESOLVER_STATUS_NONE);
|
||||
|
||||
resolver->mutex->lock();
|
||||
MutexLock lock(resolver->mutex);
|
||||
|
||||
if (resolver->queue[p_id].status == IP::RESOLVER_STATUS_NONE) {
|
||||
ERR_PRINT("Condition status == IP::RESOLVER_STATUS_NONE");
|
||||
resolver->mutex->unlock();
|
||||
resolver->mutex.unlock();
|
||||
return IP::RESOLVER_STATUS_NONE;
|
||||
}
|
||||
IP::ResolverStatus res = resolver->queue[p_id].status;
|
||||
|
||||
resolver->mutex->unlock();
|
||||
return res;
|
||||
return resolver->queue[p_id].status;
|
||||
}
|
||||
|
||||
IP_Address IP::get_resolve_item_address(ResolverID p_id) const {
|
||||
|
||||
ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP_Address());
|
||||
|
||||
resolver->mutex->lock();
|
||||
MutexLock lock(resolver->mutex);
|
||||
|
||||
if (resolver->queue[p_id].status != IP::RESOLVER_STATUS_DONE) {
|
||||
ERR_PRINT("Resolve of '" + resolver->queue[p_id].hostname + "'' didn't complete yet.");
|
||||
resolver->mutex->unlock();
|
||||
resolver->mutex.unlock();
|
||||
return IP_Address();
|
||||
}
|
||||
|
||||
IP_Address res = resolver->queue[p_id].response;
|
||||
|
||||
resolver->mutex->unlock();
|
||||
return res;
|
||||
return resolver->queue[p_id].response;
|
||||
}
|
||||
|
||||
void IP::erase_resolve_item(ResolverID p_id) {
|
||||
|
||||
ERR_FAIL_INDEX(p_id, IP::RESOLVER_MAX_QUERIES);
|
||||
|
||||
resolver->mutex->lock();
|
||||
MutexLock lock(resolver->mutex);
|
||||
|
||||
resolver->queue[p_id].status = IP::RESOLVER_STATUS_NONE;
|
||||
|
||||
resolver->mutex->unlock();
|
||||
}
|
||||
|
||||
void IP::clear_cache(const String &p_hostname) {
|
||||
|
||||
resolver->mutex->lock();
|
||||
MutexLock lock(resolver->mutex);
|
||||
|
||||
if (p_hostname.empty()) {
|
||||
resolver->cache.clear();
|
||||
@@ -218,8 +206,6 @@ void IP::clear_cache(const String &p_hostname) {
|
||||
resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV6));
|
||||
resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_ANY));
|
||||
}
|
||||
|
||||
resolver->mutex->unlock();
|
||||
}
|
||||
|
||||
Array IP::_get_local_addresses() const {
|
||||
@@ -315,7 +301,6 @@ IP::IP() {
|
||||
singleton = this;
|
||||
resolver = memnew(_IP_ResolverPrivate);
|
||||
resolver->sem = NULL;
|
||||
resolver->mutex = Mutex::create();
|
||||
|
||||
#ifndef NO_THREADS
|
||||
|
||||
@@ -349,6 +334,5 @@ IP::~IP() {
|
||||
|
||||
#endif
|
||||
|
||||
memdelete(resolver->mutex);
|
||||
memdelete(resolver);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user