From 8a222d5e750d70b039727c52c06bc0b30378a8e3 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Mon, 1 May 2023 20:13:57 +0200 Subject: [PATCH] TCPServer: check if loop thread is running before waiting for it --- src/tcp_server.cpp | 8 +++++++- src/tcp_server.h | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/tcp_server.cpp b/src/tcp_server.cpp index 477d981..4c09ca2 100644 --- a/src/tcp_server.cpp +++ b/src/tcp_server.cpp @@ -27,6 +27,7 @@ TCPServer::TCPServer(int default_backlog, allocate_client_callback allocate_new_ : m_allocateNewClient(allocate_new_client) , m_defaultBacklog(default_backlog) , m_loopThread{} + , m_loopThreadRunning(false) #ifdef WITH_UPNP , m_portMapping(0) #endif @@ -473,7 +474,9 @@ void TCPServer::shutdown_tcp() } #endif - uv_thread_join(&m_loopThread); + if (m_loopThreadRunning.load()) { + uv_thread_join(&m_loopThread); + } uv_mutex_destroy(&m_bansLock); @@ -573,6 +576,9 @@ bool TCPServer::send_internal(Client* client, Callback void TCPServer::loop(void* data) { TCPServer* server = static_cast(data); + server->m_loopThreadRunning.exchange(true); + ON_SCOPE_LEAVE([server]() { server->m_loopThreadRunning.exchange(false); }); + log_category_prefix = server->get_category(); LOGINFO(1, "event loop started"); diff --git a/src/tcp_server.h b/src/tcp_server.h index fe43b12..5a734e8 100644 --- a/src/tcp_server.h +++ b/src/tcp_server.h @@ -149,6 +149,7 @@ protected: int m_defaultBacklog; uv_thread_t m_loopThread; + std::atomic m_loopThreadRunning; static void loop(void* data);