1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-19 14:31:59 +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:
Pedro J. Estébanez
2020-02-26 11:28:13 +01:00
parent 1e57b558f2
commit 18fbdbb456
98 changed files with 739 additions and 1754 deletions

View File

@@ -601,7 +601,8 @@ void ScriptDebuggerRemote::debug(ScriptLanguage *p_script, bool p_can_continue,
void ScriptDebuggerRemote::_get_output() {
mutex->lock();
MutexLock lock(mutex);
if (output_strings.size()) {
locking = true;
@@ -666,7 +667,6 @@ void ScriptDebuggerRemote::_get_output() {
errors.pop_front();
locking = false;
}
mutex->unlock();
}
void ScriptDebuggerRemote::line_poll() {
@@ -914,7 +914,8 @@ void ScriptDebuggerRemote::_send_network_bandwidth_usage() {
void ScriptDebuggerRemote::send_message(const String &p_message, const Array &p_args) {
mutex->lock();
MutexLock lock(mutex);
if (!locking && is_peer_connected()) {
if (messages.size() >= max_messages_per_frame) {
@@ -926,7 +927,6 @@ void ScriptDebuggerRemote::send_message(const String &p_message, const Array &p_
messages.push_back(msg);
}
}
mutex->unlock();
}
void ScriptDebuggerRemote::send_error(const String &p_func, const String &p_file, int p_line, const String &p_err, const String &p_descr, ErrorHandlerType p_type, const Vector<ScriptLanguage::StackInfo> &p_stack_info) {
@@ -972,7 +972,7 @@ void ScriptDebuggerRemote::send_error(const String &p_func, const String &p_file
err_count++;
}
mutex->lock();
MutexLock lock(mutex);
if (!locking && is_peer_connected()) {
@@ -990,8 +990,6 @@ void ScriptDebuggerRemote::send_error(const String &p_func, const String &p_file
}
}
}
mutex->unlock();
}
void ScriptDebuggerRemote::_print_handler(void *p_this, const String &p_string, bool p_error) {
@@ -1020,19 +1018,21 @@ void ScriptDebuggerRemote::_print_handler(void *p_this, const String &p_string,
sdr->char_count += allowed_chars;
bool overflowed = sdr->char_count >= sdr->max_cps;
sdr->mutex->lock();
if (!sdr->locking && sdr->is_peer_connected()) {
{
MutexLock lock(sdr->mutex);
if (overflowed)
s += "[...]";
if (!sdr->locking && sdr->is_peer_connected()) {
sdr->output_strings.push_back(s);
if (overflowed)
s += "[...]";
if (overflowed) {
sdr->output_strings.push_back("[output overflow, print less text!]");
sdr->output_strings.push_back(s);
if (overflowed) {
sdr->output_strings.push_back("[output overflow, print less text!]");
}
}
}
sdr->mutex->unlock();
}
void ScriptDebuggerRemote::request_quit() {
@@ -1106,7 +1106,6 @@ ScriptDebuggerRemote::ScriptDebuggerRemote() :
last_net_bandwidth_time(0),
performance(Engine::get_singleton()->get_singleton_object("Performance")),
requested_quit(false),
mutex(Mutex::create()),
max_messages_per_frame(GLOBAL_GET("network/limits/debugger_stdout/max_messages_per_frame")),
n_messages_dropped(0),
max_errors_per_second(GLOBAL_GET("network/limits/debugger_stdout/max_errors_per_second")),
@@ -1141,5 +1140,4 @@ ScriptDebuggerRemote::~ScriptDebuggerRemote() {
remove_print_handler(&phl);
remove_error_handler(&eh);
memdelete(mutex);
}