1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

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

@@ -101,15 +101,9 @@ GDScriptInstance *GDScript::_create_instance(const Variant **p_args, int p_argco
/* STEP 2, INITIALIZE AND CONSTRUCT */
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->lock();
#endif
GDScriptLanguage::singleton->lock.lock();
instances.insert(instance->owner);
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->unlock();
#endif
GDScriptLanguage::singleton->lock.unlock();
initializer->call(instance, p_args, p_argcount, r_error);
@@ -117,11 +111,11 @@ GDScriptInstance *GDScript::_create_instance(const Variant **p_args, int p_argco
instance->script = Ref<GDScript>();
instance->owner->set_script_instance(NULL);
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->lock();
GDScriptLanguage::singleton->lock.lock();
#endif
instances.erase(p_owner);
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->unlock();
GDScriptLanguage::singleton->lock.unlock();
#endif
ERR_FAIL_COND_V(r_error.error != Variant::CallError::CALL_OK, NULL); //error constructing
@@ -343,14 +337,9 @@ PlaceHolderScriptInstance *GDScript::placeholder_instance_create(Object *p_this)
bool GDScript::instance_has(const Object *p_this) const {
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->lock();
#endif
GDScriptLanguage::singleton->lock.lock();
bool hasit = instances.has((Object *)p_this);
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->unlock();
#endif
GDScriptLanguage::singleton->lock.unlock();
return hasit;
}
@@ -564,14 +553,9 @@ void GDScript::_set_subclass_path(Ref<GDScript> &p_sc, const String &p_path) {
Error GDScript::reload(bool p_keep_state) {
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->lock();
#endif
GDScriptLanguage::singleton->lock.lock();
bool has_instances = instances.size();
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->unlock();
#endif
GDScriptLanguage::singleton->lock.unlock();
ERR_FAIL_COND_V(!p_keep_state && has_instances, ERR_ALREADY_IN_USE);
@@ -926,14 +910,9 @@ GDScript::GDScript() :
#endif
#ifdef DEBUG_ENABLED
if (GDScriptLanguage::get_singleton()->lock) {
GDScriptLanguage::get_singleton()->lock->lock();
}
GDScriptLanguage::get_singleton()->lock.lock();
GDScriptLanguage::get_singleton()->script_list.add(&script_list);
if (GDScriptLanguage::get_singleton()->lock) {
GDScriptLanguage::get_singleton()->lock->unlock();
}
GDScriptLanguage::get_singleton()->lock.unlock();
#endif
}
@@ -970,18 +949,14 @@ void GDScript::_save_orphaned_subclasses() {
GDScript::~GDScript() {
if (GDScriptLanguage::get_singleton()->lock) {
GDScriptLanguage::get_singleton()->lock->lock();
}
GDScriptLanguage::get_singleton()->lock.lock();
while (SelfList<GDScriptFunctionState> *E = pending_func_states.first()) {
// Order matters since clearing the stack may already cause
// the GDSCriptFunctionState to be destroyed and thus removed from the list.
pending_func_states.remove(E);
E->self()->_clear_stack();
}
if (GDScriptLanguage::get_singleton()->lock) {
GDScriptLanguage::get_singleton()->lock->unlock();
}
GDScriptLanguage::get_singleton()->lock.unlock();
for (Map<StringName, GDScriptFunction *>::Element *E = member_functions.front(); E; E = E->next()) {
memdelete(E->get());
@@ -990,14 +965,9 @@ GDScript::~GDScript() {
_save_orphaned_subclasses();
#ifdef DEBUG_ENABLED
if (GDScriptLanguage::get_singleton()->lock) {
GDScriptLanguage::get_singleton()->lock->lock();
}
GDScriptLanguage::get_singleton()->lock.lock();
GDScriptLanguage::get_singleton()->script_list.remove(&script_list);
if (GDScriptLanguage::get_singleton()->lock) {
GDScriptLanguage::get_singleton()->lock->unlock();
}
GDScriptLanguage::get_singleton()->lock.unlock();
#endif
}
@@ -1400,9 +1370,7 @@ GDScriptInstance::GDScriptInstance() {
}
GDScriptInstance::~GDScriptInstance() {
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->lock();
#endif
GDScriptLanguage::singleton->lock.lock();
while (SelfList<GDScriptFunctionState> *E = pending_func_states.first()) {
// Order matters since clearing the stack may already cause
@@ -1415,9 +1383,7 @@ GDScriptInstance::~GDScriptInstance() {
script->instances.erase(owner);
}
#ifndef NO_THREADS
GDScriptLanguage::singleton->lock->unlock();
#endif
GDScriptLanguage::singleton->lock.unlock();
}
/************* SCRIPT LANGUAGE **************/
@@ -1517,9 +1483,7 @@ void GDScriptLanguage::finish() {
void GDScriptLanguage::profiling_start() {
#ifdef DEBUG_ENABLED
if (lock) {
lock->lock();
}
lock.lock();
SelfList<GDScriptFunction> *elem = function_list.first();
while (elem) {
@@ -1536,25 +1500,16 @@ void GDScriptLanguage::profiling_start() {
}
profiling = true;
if (lock) {
lock->unlock();
}
lock.unlock();
#endif
}
void GDScriptLanguage::profiling_stop() {
#ifdef DEBUG_ENABLED
if (lock) {
lock->lock();
}
lock.lock();
profiling = false;
if (lock) {
lock->unlock();
}
lock.unlock();
#endif
}
@@ -1562,9 +1517,7 @@ int GDScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr,
int current = 0;
#ifdef DEBUG_ENABLED
if (lock) {
lock->lock();
}
lock.lock();
SelfList<GDScriptFunction> *elem = function_list.first();
while (elem) {
@@ -1578,10 +1531,7 @@ int GDScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr,
current++;
}
if (lock) {
lock->unlock();
}
lock.unlock();
#endif
return current;
@@ -1592,9 +1542,7 @@ int GDScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_
int current = 0;
#ifdef DEBUG_ENABLED
if (lock) {
lock->lock();
}
lock.lock();
SelfList<GDScriptFunction> *elem = function_list.first();
while (elem) {
@@ -1610,10 +1558,7 @@ int GDScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_
elem = elem->next();
}
if (lock) {
lock->unlock();
}
lock.unlock();
#endif
return current;
@@ -1644,9 +1589,7 @@ void GDScriptLanguage::reload_all_scripts() {
#ifdef DEBUG_ENABLED
print_verbose("GDScript: Reloading all scripts");
if (lock) {
lock->lock();
}
lock.lock();
List<Ref<GDScript> > scripts;
@@ -1659,9 +1602,7 @@ void GDScriptLanguage::reload_all_scripts() {
elem = elem->next();
}
if (lock) {
lock->unlock();
}
lock.unlock();
//as scripts are going to be reloaded, must proceed without locking here
@@ -1680,9 +1621,7 @@ void GDScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_so
#ifdef DEBUG_ENABLED
if (lock) {
lock->lock();
}
lock.lock();
List<Ref<GDScript> > scripts;
@@ -1695,9 +1634,7 @@ void GDScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_so
elem = elem->next();
}
if (lock) {
lock->unlock();
}
lock.unlock();
//when someone asks you why dynamically typed languages are easier to write....
@@ -1816,9 +1753,7 @@ void GDScriptLanguage::frame() {
#ifdef DEBUG_ENABLED
if (profiling) {
if (lock) {
lock->lock();
}
lock.lock();
SelfList<GDScriptFunction> *elem = function_list.first();
while (elem) {
@@ -1831,9 +1766,7 @@ void GDScriptLanguage::frame() {
elem = elem->next();
}
if (lock) {
lock->unlock();
}
lock.unlock();
}
#endif
@@ -2202,11 +2135,6 @@ GDScriptLanguage::GDScriptLanguage() {
_debug_parse_err_line = -1;
_debug_parse_err_file = "";
#ifdef NO_THREADS
lock = NULL;
#else
lock = Mutex::create();
#endif
profiling = false;
script_frame_time = 0;
@@ -2240,10 +2168,6 @@ GDScriptLanguage::GDScriptLanguage() {
GDScriptLanguage::~GDScriptLanguage() {
if (lock) {
memdelete(lock);
lock = NULL;
}
if (_call_stack) {
memdelete_arr(_call_stack);
}