refactor: use std::mutex in net/TCPListenSocket

This commit is contained in:
sithlord48 2025-06-10 21:28:43 -04:00 committed by Nick Bolton
parent 818d588b65
commit 5686d24626
2 changed files with 5 additions and 8 deletions

View file

@ -12,8 +12,6 @@
#include "base/IEventQueue.h" #include "base/IEventQueue.h"
#include "base/Log.h" #include "base/Log.h"
#include "io/XIO.h" #include "io/XIO.h"
#include "mt/Lock.h"
#include "mt/Mutex.h"
#include "net/NetworkAddress.h" #include "net/NetworkAddress.h"
#include "net/SocketMultiplexer.h" #include "net/SocketMultiplexer.h"
#include "net/TCPSocket.h" #include "net/TCPSocket.h"
@ -30,7 +28,6 @@ TCPListenSocket::TCPListenSocket(
: m_events(events), : m_events(events),
m_socketMultiplexer(socketMultiplexer) m_socketMultiplexer(socketMultiplexer)
{ {
m_mutex = new Mutex;
try { try {
m_socket = ARCH->newSocket(family, IArchNetwork::kSTREAM); m_socket = ARCH->newSocket(family, IArchNetwork::kSTREAM);
} catch (XArchNetwork &e) { } catch (XArchNetwork &e) {
@ -49,13 +46,12 @@ TCPListenSocket::~TCPListenSocket()
// ignore // ignore
LOG((CLOG_WARN "error while closing TCP socket")); LOG((CLOG_WARN "error while closing TCP socket"));
} }
delete m_mutex;
} }
void TCPListenSocket::bind(const NetworkAddress &addr) void TCPListenSocket::bind(const NetworkAddress &addr)
{ {
try { try {
Lock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
ARCH->setReuseAddrOnSocket(m_socket, true); ARCH->setReuseAddrOnSocket(m_socket, true);
ARCH->bindSocket(m_socket, addr.getAddress()); ARCH->bindSocket(m_socket, addr.getAddress());
ARCH->listenOnSocket(m_socket); ARCH->listenOnSocket(m_socket);
@ -73,7 +69,7 @@ void TCPListenSocket::bind(const NetworkAddress &addr)
void TCPListenSocket::close() void TCPListenSocket::close()
{ {
Lock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
if (m_socket == nullptr) { if (m_socket == nullptr) {
throw XIOClosed(); throw XIOClosed();
} }

View file

@ -10,7 +10,8 @@
#include "arch/IArchNetwork.h" #include "arch/IArchNetwork.h"
#include "net/IListenSocket.h" #include "net/IListenSocket.h"
class Mutex; #include <mutex>
class ISocketMultiplexerJob; class ISocketMultiplexerJob;
class IEventQueue; class IEventQueue;
class SocketMultiplexer; class SocketMultiplexer;
@ -46,7 +47,7 @@ public:
protected: protected:
ArchSocket m_socket; ArchSocket m_socket;
Mutex *m_mutex = nullptr; std::mutex m_mutex;
IEventQueue *m_events; IEventQueue *m_events;
SocketMultiplexer *m_socketMultiplexer; SocketMultiplexer *m_socketMultiplexer;
}; };