mirror of
https://github.com/godotengine/godot.git
synced 2025-10-19 16:03:29 +00: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:
parent
1e57b558f2
commit
18fbdbb456
98 changed files with 739 additions and 1754 deletions
|
@ -222,15 +222,11 @@ ScriptInstance *NativeScript::instance_create(Object *p_this) {
|
|||
nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
|
||||
#endif
|
||||
|
||||
#ifndef NO_THREADS
|
||||
owners_lock->lock();
|
||||
#endif
|
||||
{
|
||||
MutexLock lock(owners_lock);
|
||||
|
||||
instance_owners.insert(p_this);
|
||||
|
||||
#ifndef NO_THREADS
|
||||
owners_lock->unlock();
|
||||
#endif
|
||||
instance_owners.insert(p_this);
|
||||
}
|
||||
|
||||
return nsi;
|
||||
}
|
||||
|
@ -782,17 +778,10 @@ NativeScript::NativeScript() {
|
|||
library = Ref<GDNative>();
|
||||
lib_path = "";
|
||||
class_name = "";
|
||||
#ifndef NO_THREADS
|
||||
owners_lock = Mutex::create();
|
||||
#endif
|
||||
}
|
||||
|
||||
NativeScript::~NativeScript() {
|
||||
NSL->unregister_script(this);
|
||||
|
||||
#ifndef NO_THREADS
|
||||
memdelete(owners_lock);
|
||||
#endif
|
||||
}
|
||||
|
||||
#define GET_SCRIPT_DESC() script->get_script_desc()
|
||||
|
@ -1140,16 +1129,9 @@ NativeScriptInstance::~NativeScriptInstance() {
|
|||
script_data->destroy_func.destroy_func((godot_object *)owner, script_data->destroy_func.method_data, userdata);
|
||||
|
||||
if (owner) {
|
||||
|
||||
#ifndef NO_THREADS
|
||||
script->owners_lock->lock();
|
||||
#endif
|
||||
MutexLock lock(script->owners_lock);
|
||||
|
||||
script->instance_owners.erase(owner);
|
||||
|
||||
#ifndef NO_THREADS
|
||||
script->owners_lock->unlock();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1246,7 +1228,6 @@ NativeScriptLanguage::NativeScriptLanguage() {
|
|||
NativeScriptLanguage::singleton = this;
|
||||
#ifndef NO_THREADS
|
||||
has_objects_to_register = false;
|
||||
mutex = Mutex::create();
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_ENABLED
|
||||
|
@ -1283,10 +1264,6 @@ NativeScriptLanguage::~NativeScriptLanguage() {
|
|||
NSL->library_classes.clear();
|
||||
NSL->library_gdnatives.clear();
|
||||
NSL->library_script_users.clear();
|
||||
|
||||
#ifndef NO_THREADS
|
||||
memdelete(mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
String NativeScriptLanguage::get_name() const {
|
||||
|
@ -1413,9 +1390,7 @@ void NativeScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_
|
|||
|
||||
void NativeScriptLanguage::profiling_start() {
|
||||
#ifdef DEBUG_ENABLED
|
||||
#ifndef NO_THREADS
|
||||
MutexLock lock(mutex);
|
||||
#endif
|
||||
|
||||
profile_data.clear();
|
||||
profiling = true;
|
||||
|
@ -1424,9 +1399,7 @@ void NativeScriptLanguage::profiling_start() {
|
|||
|
||||
void NativeScriptLanguage::profiling_stop() {
|
||||
#ifdef DEBUG_ENABLED
|
||||
#ifndef NO_THREADS
|
||||
MutexLock lock(mutex);
|
||||
#endif
|
||||
|
||||
profiling = false;
|
||||
#endif
|
||||
|
@ -1434,9 +1407,8 @@ void NativeScriptLanguage::profiling_stop() {
|
|||
|
||||
int NativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
|
||||
#ifdef DEBUG_ENABLED
|
||||
#ifndef NO_THREADS
|
||||
MutexLock lock(mutex);
|
||||
#endif
|
||||
|
||||
int current = 0;
|
||||
|
||||
for (Map<StringName, ProfileData>::Element *d = profile_data.front(); d; d = d->next()) {
|
||||
|
@ -1458,9 +1430,8 @@ int NativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_a
|
|||
|
||||
int NativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
|
||||
#ifdef DEBUG_ENABLED
|
||||
#ifndef NO_THREADS
|
||||
MutexLock lock(mutex);
|
||||
#endif
|
||||
|
||||
int current = 0;
|
||||
|
||||
for (Map<StringName, ProfileData>::Element *d = profile_data.front(); d; d = d->next()) {
|
||||
|
@ -1484,9 +1455,7 @@ int NativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, in
|
|||
|
||||
void NativeScriptLanguage::profiling_add_data(StringName p_signature, uint64_t p_time) {
|
||||
#ifdef DEBUG_ENABLED
|
||||
#ifndef NO_THREADS
|
||||
MutexLock lock(mutex);
|
||||
#endif
|
||||
|
||||
Map<StringName, ProfileData>::Element *d = profile_data.find(p_signature);
|
||||
if (d) {
|
||||
|
@ -1705,9 +1674,8 @@ void NativeScriptLanguage::defer_init_library(Ref<GDNativeLibrary> lib, NativeSc
|
|||
#endif
|
||||
|
||||
void NativeScriptLanguage::init_library(const Ref<GDNativeLibrary> &lib) {
|
||||
#ifndef NO_THREADS
|
||||
MutexLock lock(mutex);
|
||||
#endif
|
||||
|
||||
// See if this library was "registered" already.
|
||||
const String &lib_path = lib->get_current_library_path();
|
||||
ERR_FAIL_COND_MSG(lib_path.length() == 0, lib->get_name() + " does not have a library for the current platform.");
|
||||
|
@ -1743,16 +1711,14 @@ void NativeScriptLanguage::init_library(const Ref<GDNativeLibrary> &lib) {
|
|||
}
|
||||
|
||||
void NativeScriptLanguage::register_script(NativeScript *script) {
|
||||
#ifndef NO_THREADS
|
||||
MutexLock lock(mutex);
|
||||
#endif
|
||||
|
||||
library_script_users[script->lib_path].insert(script);
|
||||
}
|
||||
|
||||
void NativeScriptLanguage::unregister_script(NativeScript *script) {
|
||||
#ifndef NO_THREADS
|
||||
MutexLock lock(mutex);
|
||||
#endif
|
||||
|
||||
Map<String, Set<NativeScript *> >::Element *S = library_script_users.find(script->lib_path);
|
||||
if (S) {
|
||||
S->get().erase(script);
|
||||
|
@ -1803,9 +1769,7 @@ void NativeScriptLanguage::frame() {
|
|||
|
||||
#ifdef DEBUG_ENABLED
|
||||
{
|
||||
#ifndef NO_THREADS
|
||||
MutexLock lock(mutex);
|
||||
#endif
|
||||
|
||||
for (Map<StringName, ProfileData>::Element *d = profile_data.front(); d; d = d->next()) {
|
||||
d->get().last_frame_call_count = d->get().frame_call_count;
|
||||
|
@ -1867,9 +1831,7 @@ void NativeReloadNode::_notification(int p_what) {
|
|||
|
||||
if (unloaded)
|
||||
break;
|
||||
#ifndef NO_THREADS
|
||||
MutexLock lock(NSL->mutex);
|
||||
#endif
|
||||
NSL->_unload_stuff(true);
|
||||
|
||||
for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
|
||||
|
@ -1904,9 +1866,8 @@ void NativeReloadNode::_notification(int p_what) {
|
|||
|
||||
if (!unloaded)
|
||||
break;
|
||||
#ifndef NO_THREADS
|
||||
MutexLock lock(NSL->mutex);
|
||||
#endif
|
||||
|
||||
Set<StringName> libs_to_remove;
|
||||
for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue