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

@ -200,7 +200,7 @@ class EditorExportPlatformJavaScript : public EditorExportPlatform {
private:
Ref<EditorHTTPServer> server;
bool server_quit;
Mutex *server_lock;
Mutex server_lock;
Thread *server_thread;
static void _server_thread_poll(void *data);
@ -531,9 +531,8 @@ bool EditorExportPlatformJavaScript::poll_export() {
menu_options = preset.is_valid();
if (server->is_listening()) {
if (menu_options == 0) {
server_lock->lock();
MutexLock lock(server_lock);
server->stop();
server_lock->unlock();
} else {
menu_options += 1;
}
@ -553,9 +552,8 @@ int EditorExportPlatformJavaScript::get_options_count() const {
Error EditorExportPlatformJavaScript::run(const Ref<EditorExportPreset> &p_preset, int p_option, int p_debug_flags) {
if (p_option == 1) {
server_lock->lock();
MutexLock lock(server_lock);
server->stop();
server_lock->unlock();
return OK;
}
@ -584,10 +582,12 @@ Error EditorExportPlatformJavaScript::run(const Ref<EditorExportPreset> &p_prese
ERR_FAIL_COND_V_MSG(!bind_ip.is_valid(), ERR_INVALID_PARAMETER, "Invalid editor setting 'export/web/http_host': '" + bind_host + "'. Try using '127.0.0.1'.");
// Restart server.
server_lock->lock();
server->stop();
err = server->listen(bind_port, bind_ip);
server_lock->unlock();
{
MutexLock lock(server_lock);
server->stop();
err = server->listen(bind_port, bind_ip);
}
ERR_FAIL_COND_V_MSG(err != OK, err, "Unable to start HTTP server.");
OS::get_singleton()->shell_open(String("http://" + bind_host + ":" + itos(bind_port) + "/tmp_js_export.html"));
@ -605,9 +605,10 @@ void EditorExportPlatformJavaScript::_server_thread_poll(void *data) {
EditorExportPlatformJavaScript *ej = (EditorExportPlatformJavaScript *)data;
while (!ej->server_quit) {
OS::get_singleton()->delay_usec(1000);
ej->server_lock->lock();
ej->server->poll();
ej->server_lock->unlock();
{
MutexLock lock(ej->server_lock);
ej->server->poll();
}
}
}
@ -615,7 +616,6 @@ EditorExportPlatformJavaScript::EditorExportPlatformJavaScript() {
server.instance();
server_quit = false;
server_lock = Mutex::create();
server_thread = Thread::create(_server_thread_poll, this);
Ref<Image> img = memnew(Image(_javascript_logo));
@ -639,7 +639,6 @@ EditorExportPlatformJavaScript::~EditorExportPlatformJavaScript() {
server->stop();
server_quit = true;
Thread::wait_to_finish(server_thread);
memdelete(server_lock);
memdelete(server_thread);
}