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-01-27 10:43:02 +01:00
parent b450036120
commit 4ddcdc031b
99 changed files with 472 additions and 1391 deletions

View File

@ -109,7 +109,7 @@ void EditorResourcePreview::_thread_func(void *ud) {
void EditorResourcePreview::_preview_ready(const String &p_str, const Ref<Texture> &p_texture, const Ref<Texture> &p_small_texture, ObjectID id, const StringName &p_func, const Variant &p_ud) {
preview_mutex->lock();
preview_mutex.lock();
String path = p_str;
uint32_t hash = 0;
@ -131,7 +131,7 @@ void EditorResourcePreview::_preview_ready(const String &p_str, const Ref<Textur
cache[path] = item;
preview_mutex->unlock();
preview_mutex.unlock();
MessageQueue::get_singleton()->push_call(id, p_func, path, p_texture, p_small_texture, p_ud);
}
@ -221,7 +221,7 @@ void EditorResourcePreview::_thread() {
while (!exit) {
preview_sem->wait();
preview_mutex->lock();
preview_mutex.lock();
if (queue.size()) {
@ -237,10 +237,10 @@ void EditorResourcePreview::_thread() {
_preview_ready(path, cache[item.path].preview, cache[item.path].small_preview, item.id, item.function, item.userdata);
preview_mutex->unlock();
preview_mutex.unlock();
} else {
preview_mutex->unlock();
preview_mutex.unlock();
Ref<ImageTexture> texture;
Ref<ImageTexture> small_texture;
@ -347,7 +347,7 @@ void EditorResourcePreview::_thread() {
}
} else {
preview_mutex->unlock();
preview_mutex.unlock();
}
}
exited = true;
@ -358,7 +358,7 @@ void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource> &p
ERR_FAIL_NULL(p_receiver);
ERR_FAIL_COND(!p_res.is_valid());
preview_mutex->lock();
preview_mutex.lock();
String path_id = "ID:" + itos(p_res->get_instance_id());
@ -366,7 +366,7 @@ void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource> &p
cache[path_id].order = order++;
p_receiver->call(p_receiver_func, path_id, cache[path_id].preview, cache[path_id].small_preview, p_userdata);
preview_mutex->unlock();
preview_mutex.unlock();
return;
}
@ -380,18 +380,18 @@ void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource> &p
item.userdata = p_userdata;
queue.push_back(item);
preview_mutex->unlock();
preview_mutex.unlock();
preview_sem->post();
}
void EditorResourcePreview::queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) {
ERR_FAIL_NULL(p_receiver);
preview_mutex->lock();
preview_mutex.lock();
if (cache.has(p_path)) {
cache[p_path].order = order++;
p_receiver->call(p_receiver_func, p_path, cache[p_path].preview, cache[p_path].small_preview, p_userdata);
preview_mutex->unlock();
preview_mutex.unlock();
return;
}
@ -402,7 +402,7 @@ void EditorResourcePreview::queue_resource_preview(const String &p_path, Object
item.userdata = p_userdata;
queue.push_back(item);
preview_mutex->unlock();
preview_mutex.unlock();
preview_sem->post();
}
@ -436,7 +436,7 @@ void EditorResourcePreview::_bind_methods() {
void EditorResourcePreview::check_for_invalidation(const String &p_path) {
preview_mutex->lock();
preview_mutex.lock();
bool call_invalidated = false;
if (cache.has(p_path)) {
@ -448,7 +448,7 @@ void EditorResourcePreview::check_for_invalidation(const String &p_path) {
}
}
preview_mutex->unlock();
preview_mutex.unlock();
if (call_invalidated) { //do outside mutex
call_deferred("emit_signal", "preview_invalidated", p_path);
@ -477,7 +477,6 @@ void EditorResourcePreview::stop() {
EditorResourcePreview::EditorResourcePreview() {
thread = NULL;
singleton = this;
preview_mutex = Mutex::create();
preview_sem = Semaphore::create();
order = 0;
exit = false;
@ -487,6 +486,5 @@ EditorResourcePreview::EditorResourcePreview() {
EditorResourcePreview::~EditorResourcePreview() {
stop();
memdelete(preview_mutex);
memdelete(preview_sem);
}