Replace the mutex in the web server to an rwlock.

This commit is contained in:
Relintai 2022-07-24 14:27:58 +02:00
parent 275c11cc6b
commit 731dbb427c
2 changed files with 10 additions and 4 deletions

View File

@ -147,10 +147,12 @@ void WebServerSimple::_start() {
// Restart server. // Restart server.
{ {
MutexLock lock(server_lock); server_lock.write_lock();
server->stop(); server->stop();
err = server->listen(bind_port, bind_ip, use_ssl, ssl_key, ssl_cert); 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)); ERR_FAIL_COND_MSG(err != OK, "Error starting HTTP server:\n" + itos(err));
@ -167,7 +169,8 @@ void WebServerSimple::_stop() {
WebServer::_stop(); WebServer::_stop();
MutexLock lock(server_lock); server_lock.write_lock();
server->stop(); server->stop();
if (server_thread) { if (server_thread) {
@ -177,6 +180,7 @@ void WebServerSimple::_stop() {
} }
_running = false; _running = false;
server_lock.write_unlock();
} }
WebServerSimple::WebServerSimple() { WebServerSimple::WebServerSimple() {
@ -254,8 +258,9 @@ void WebServerSimple::_server_thread_poll(void *data) {
while (!ej->server_quit) { while (!ej->server_quit) {
OS::get_singleton()->delay_usec(1000); OS::get_singleton()->delay_usec(1000);
{ {
MutexLock lock(ej->server_lock); ej->server_lock.read_lock();
ej->server->poll(); ej->server->poll();
ej->server_lock.read_unlock();
} }
} }
} }

View File

@ -35,6 +35,7 @@
#include "core/io/stream_peer_ssl.h" #include "core/io/stream_peer_ssl.h"
#include "core/io/tcp_server.h" #include "core/io/tcp_server.h"
#include "core/io/zip_io.h" #include "core/io/zip_io.h"
#include "core/os/rw_lock.h"
#include "../http/web_server.h" #include "../http/web_server.h"
@ -99,7 +100,7 @@ protected:
Ref<HTTPServerSimple> server; Ref<HTTPServerSimple> server;
bool server_quit; bool server_quit;
Mutex server_lock; RWLock server_lock;
Thread *server_thread; Thread *server_thread;
bool _running; bool _running;