From 731dbb427c5cb1638677ab61472599fe161293f9 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 24 Jul 2022 14:27:58 +0200 Subject: [PATCH] Replace the mutex in the web server to an rwlock. --- modules/web/http_server_simple/web_server_simple.cpp | 11 ++++++++--- modules/web/http_server_simple/web_server_simple.h | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/web/http_server_simple/web_server_simple.cpp b/modules/web/http_server_simple/web_server_simple.cpp index 1e401b7bb..da6b84d6c 100644 --- a/modules/web/http_server_simple/web_server_simple.cpp +++ b/modules/web/http_server_simple/web_server_simple.cpp @@ -147,10 +147,12 @@ void WebServerSimple::_start() { // Restart server. { - MutexLock lock(server_lock); + server_lock.write_lock(); server->stop(); err = server->listen(bind_port, bind_ip, use_ssl, ssl_key, ssl_cert); + + server_lock.write_unlock(); } ERR_FAIL_COND_MSG(err != OK, "Error starting HTTP server:\n" + itos(err)); @@ -167,7 +169,8 @@ void WebServerSimple::_stop() { WebServer::_stop(); - MutexLock lock(server_lock); + server_lock.write_lock(); + server->stop(); if (server_thread) { @@ -177,6 +180,7 @@ void WebServerSimple::_stop() { } _running = false; + server_lock.write_unlock(); } WebServerSimple::WebServerSimple() { @@ -254,8 +258,9 @@ void WebServerSimple::_server_thread_poll(void *data) { while (!ej->server_quit) { OS::get_singleton()->delay_usec(1000); { - MutexLock lock(ej->server_lock); + ej->server_lock.read_lock(); ej->server->poll(); + ej->server_lock.read_unlock(); } } } diff --git a/modules/web/http_server_simple/web_server_simple.h b/modules/web/http_server_simple/web_server_simple.h index b6ee0bf3f..e2d88f414 100644 --- a/modules/web/http_server_simple/web_server_simple.h +++ b/modules/web/http_server_simple/web_server_simple.h @@ -35,6 +35,7 @@ #include "core/io/stream_peer_ssl.h" #include "core/io/tcp_server.h" #include "core/io/zip_io.h" +#include "core/os/rw_lock.h" #include "../http/web_server.h" @@ -99,7 +100,7 @@ protected: Ref server; bool server_quit; - Mutex server_lock; + RWLock server_lock; Thread *server_thread; bool _running;