From 18fbdbb456c07a56b358bea2e392765fbcbb3283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Wed, 26 Feb 2020 11:28:13 +0100 Subject: Reimplement Mutex with C++'s Main: - It's now implemented thanks to ``. 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`. --- modules/visual_script/visual_script.cpp | 26 ++++++++------------------ modules/visual_script/visual_script.h | 2 +- 2 files changed, 9 insertions(+), 19 deletions(-) (limited to 'modules/visual_script') diff --git a/modules/visual_script/visual_script.cpp b/modules/visual_script/visual_script.cpp index 70f650cfd3..5036840413 100644 --- a/modules/visual_script/visual_script.cpp +++ b/modules/visual_script/visual_script.cpp @@ -928,13 +928,11 @@ ScriptInstance *VisualScript::instance_create(Object *p_this) { VisualScriptInstance *instance = memnew(VisualScriptInstance); instance->create(Ref(this), p_this); - if (VisualScriptLanguage::singleton->lock) - VisualScriptLanguage::singleton->lock->lock(); - - instances[p_this] = instance; + { + MutexLock lock(VisualScriptLanguage::singleton->lock); - if (VisualScriptLanguage::singleton->lock) - VisualScriptLanguage::singleton->lock->unlock(); + instances[p_this] = instance; + } return instance; } @@ -2391,13 +2389,11 @@ VisualScriptInstance::VisualScriptInstance() { VisualScriptInstance::~VisualScriptInstance() { - if (VisualScriptLanguage::singleton->lock) - VisualScriptLanguage::singleton->lock->lock(); - - script->instances.erase(owner); + { + MutexLock lock(VisualScriptLanguage::singleton->lock); - if (VisualScriptLanguage::singleton->lock) - VisualScriptLanguage::singleton->lock->unlock(); + script->instances.erase(owner); + } for (Map::Element *E = instances.front(); E; E = E->next()) { memdelete(E->get()); @@ -2836,9 +2832,6 @@ VisualScriptLanguage::VisualScriptLanguage() { _step = "_step"; _subcall = "_subcall"; singleton = this; -#ifndef NO_THREADS - lock = Mutex::create(); -#endif _debug_parse_err_node = -1; _debug_parse_err_file = ""; @@ -2859,9 +2852,6 @@ VisualScriptLanguage::VisualScriptLanguage() { VisualScriptLanguage::~VisualScriptLanguage() { - if (lock) - memdelete(lock); - if (_call_stack) { memdelete_arr(_call_stack); } diff --git a/modules/visual_script/visual_script.h b/modules/visual_script/visual_script.h index 08b84bce1d..0a6daba64f 100644 --- a/modules/visual_script/visual_script.h +++ b/modules/visual_script/visual_script.h @@ -530,7 +530,7 @@ public: static VisualScriptLanguage *singleton; - Mutex *lock; + Mutex lock; bool debug_break(const String &p_error, bool p_allow_continue = true); bool debug_break_parse(const String &p_file, int p_node, const String &p_error); -- cgit v1.2.3