Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Allow locking/unlocking of MutexLock #93739

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions core/io/ip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ struct _IP_ResolverPrivate {
continue;
}

mutex.lock();
MutexLock lock(mutex);
List<IPAddress> response;
String hostname = queue[i].hostname;
IP::Type type = queue[i].type;
mutex.unlock();
lock.temp_unlock();

// We should not lock while resolving the hostname,
// only when modifying the queue.
IP::get_singleton()->_resolve_hostname(response, hostname, type);

MutexLock lock(mutex);
lock.temp_relock();
// Could have been completed by another function, or deleted.
if (queue[i].status.get() != IP::RESOLVER_STATUS_WAITING) {
continue;
Expand Down Expand Up @@ -131,21 +131,22 @@ PackedStringArray IP::resolve_hostname_addresses(const String &p_hostname, Type
List<IPAddress> res;
String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);

resolver->mutex.lock();
if (resolver->cache.has(key)) {
res = resolver->cache[key];
} else {
// This should be run unlocked so the resolver thread can keep resolving
// other requests.
resolver->mutex.unlock();
_resolve_hostname(res, p_hostname, p_type);
resolver->mutex.lock();
// We might be overriding another result, but we don't care as long as the result is valid.
if (res.size()) {
resolver->cache[key] = res;
{
MutexLock lock(resolver->mutex);
if (resolver->cache.has(key)) {
res = resolver->cache[key];
} else {
// This should be run unlocked so the resolver thread can keep resolving
// other requests.
lock.temp_unlock();
_resolve_hostname(res, p_hostname, p_type);
lock.temp_relock();
// We might be overriding another result, but we don't care as long as the result is valid.
if (res.size()) {
resolver->cache[key] = res;
}
}
}
resolver->mutex.unlock();

PackedStringArray result;
for (const IPAddress &E : res) {
Expand Down
15 changes: 7 additions & 8 deletions core/io/resource_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,10 +690,10 @@ Ref<Resource> ResourceLoader::load_threaded_get(const String &p_path, Error *r_e
if (Thread::is_main_thread() && !load_token->local_path.is_empty()) {
const ThreadLoadTask &load_task = thread_load_tasks[load_token->local_path];
while (load_task.status == THREAD_LOAD_IN_PROGRESS) {
thread_load_lock.~MutexLock();
thread_load_lock.temp_unlock();
bool exit = !_ensure_load_progress();
OS::get_singleton()->delay_usec(1000);
new (&thread_load_lock) MutexLock(thread_load_mutex);
thread_load_lock.temp_relock();
if (exit) {
break;
}
Expand Down Expand Up @@ -754,7 +754,7 @@ Ref<Resource> ResourceLoader::_load_complete_inner(LoadToken &p_load_token, Erro
bool loader_is_wtp = load_task.task_id != 0;
if (loader_is_wtp) {
// Loading thread is in the worker pool.
thread_load_mutex.unlock();
p_thread_load_lock.temp_unlock();

PREPARE_FOR_WTP_WAIT
Error wait_err = WorkerThreadPool::get_singleton()->wait_for_task_completion(load_task.task_id);
Expand All @@ -770,7 +770,7 @@ Ref<Resource> ResourceLoader::_load_complete_inner(LoadToken &p_load_token, Erro
_run_load_task(&load_task);
}

thread_load_mutex.lock();
p_thread_load_lock.temp_relock();
load_task.awaited = true;

DEV_ASSERT(load_task.status == THREAD_LOAD_FAILED || load_task.status == THREAD_LOAD_LOADED);
Expand Down Expand Up @@ -1205,7 +1205,7 @@ void ResourceLoader::clear_translation_remaps() {
void ResourceLoader::clear_thread_load_tasks() {
// Bring the thing down as quickly as possible without causing deadlocks or leaks.

thread_load_mutex.lock();
MutexLock thread_load_lock(thread_load_mutex);
cleaning_tasks = true;

while (true) {
Expand All @@ -1224,9 +1224,9 @@ void ResourceLoader::clear_thread_load_tasks() {
if (none_running) {
break;
}
thread_load_mutex.unlock();
thread_load_lock.temp_unlock();
OS::get_singleton()->delay_usec(1000);
thread_load_mutex.lock();
thread_load_lock.temp_relock();
}

while (user_load_tokens.begin()) {
Expand All @@ -1241,7 +1241,6 @@ void ResourceLoader::clear_thread_load_tasks() {
thread_load_tasks.clear();

cleaning_tasks = false;
thread_load_mutex.unlock();
}

void ResourceLoader::load_path_remaps() {
Expand Down
17 changes: 15 additions & 2 deletions core/os/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class MutexImpl {

template <typename MutexT>
class MutexLock {
THREADING_NAMESPACE::unique_lock<typename MutexT::StdMutexType> lock;
mutable THREADING_NAMESPACE::unique_lock<typename MutexT::StdMutexType> lock;

public:
explicit MutexLock(const MutexT &p_mutex) :
Expand All @@ -82,8 +82,18 @@ class MutexLock {
template <typename T = MutexT>
_ALWAYS_INLINE_ THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &_get_lock(
typename std::enable_if<std::is_same<T, THREADING_NAMESPACE::mutex>::value> * = nullptr) const {
return const_cast<THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &>(lock);
return lock;
}

_ALWAYS_INLINE_ void temp_relock() const {
lock.lock();
}

_ALWAYS_INLINE_ void temp_unlock() const {
lock.unlock();
}

// TODO: Implement a `try_temp_relock` if needed (will also need a dummy method below).
};

using Mutex = MutexImpl<THREADING_NAMESPACE::recursive_mutex>; // Recursive, for general use
Expand All @@ -109,6 +119,9 @@ template <typename MutexT>
class MutexLock {
public:
MutexLock(const MutexT &p_mutex) {}

void temp_relock() const {}
void temp_unlock() const {}
};

using Mutex = MutexImpl;
Expand Down
20 changes: 20 additions & 0 deletions core/os/safe_binary_mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ class MutexLock<SafeBinaryMutex<Tag>> {
~MutexLock() {
mutex.unlock();
}

_ALWAYS_INLINE_ void temp_relock() const {
mutex.lock();
}

_ALWAYS_INLINE_ void temp_unlock() const {
mutex.unlock();
}

// TODO: Implement a `try_temp_relock` if needed (will also need a dummy method below).
};

#else // No threads.
Expand All @@ -119,6 +129,16 @@ class SafeBinaryMutex {
void unlock() const {}
};

template <int Tag>
class MutexLock<SafeBinaryMutex<Tag>> {
public:
MutexLock(const SafeBinaryMutex<Tag> &p_mutex) {}
~MutexLock() {}

void temp_relock() const {}
void temp_unlock() const {}
};

#endif // THREADS_ENABLED

#endif // SAFE_BINARY_MUTEX_H
Loading