refactor: use std::mutex in ArchNetworkWinSock

This commit is contained in:
sithlord48 2025-06-10 21:05:14 -04:00 committed by Nick Bolton
parent fa1ee0690e
commit 86e109baf5
2 changed files with 14 additions and 21 deletions

View file

@ -82,10 +82,6 @@ ArchNetAddressImpl *ArchNetAddressImpl::alloc(size_t size)
// ArchNetworkWinsock // ArchNetworkWinsock
// //
ArchNetworkWinsock::ArchNetworkWinsock() : m_mutex(nullptr)
{
}
ArchNetworkWinsock::~ArchNetworkWinsock() ArchNetworkWinsock::~ArchNetworkWinsock()
{ {
if (s_networkModule != nullptr) { if (s_networkModule != nullptr) {
@ -95,9 +91,6 @@ ArchNetworkWinsock::~ArchNetworkWinsock()
WSACleanup_winsock = nullptr; WSACleanup_winsock = nullptr;
s_networkModule = nullptr; s_networkModule = nullptr;
} }
if (m_mutex != nullptr) {
ARCH->closeMutex(m_mutex);
}
EventList::iterator it; EventList::iterator it;
for (it = m_unblockEvents.begin(); it != m_unblockEvents.end(); it++) { for (it = m_unblockEvents.begin(); it != m_unblockEvents.end(); it++) {
@ -116,7 +109,6 @@ void ArchNetworkWinsock::init()
for (size_t i = 0; i < sizeof(s_library) / sizeof(s_library[0]); ++i) { for (size_t i = 0; i < sizeof(s_library) / sizeof(s_library[0]); ++i) {
try { try {
initModule((HMODULE)::LoadLibrary(s_library[i])); initModule((HMODULE)::LoadLibrary(s_library[i]));
m_mutex = ARCH->newMutex();
return; return;
} catch (XArchNetwork &) { } catch (XArchNetwork &) {
// ignore // ignore
@ -233,9 +225,8 @@ ArchSocket ArchNetworkWinsock::copySocket(ArchSocket s)
assert(s != nullptr); assert(s != nullptr);
// ref the socket and return it // ref the socket and return it
ARCH->lockMutex(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
++s->m_refCount; ++s->m_refCount;
ARCH->unlockMutex(m_mutex);
return s; return s;
} }
@ -244,18 +235,21 @@ void ArchNetworkWinsock::closeSocket(ArchSocket s)
assert(s != nullptr); assert(s != nullptr);
// unref the socket and note if it should be released // unref the socket and note if it should be released
ARCH->lockMutex(m_mutex); bool doClose = false;
const bool doClose = (--s->m_refCount == 0); {
ARCH->unlockMutex(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
doClose = (--s->m_refCount == 0);
}
// close the socket if necessary // close the socket if necessary
if (doClose) { if (doClose) {
if (close_winsock(s->m_socket) == SOCKET_ERROR) { if (close_winsock(s->m_socket) == SOCKET_ERROR) {
// close failed. restore the last ref and throw. // close failed. restore the last ref and throw.
int err = getsockerror_winsock(); int err = getsockerror_winsock();
ARCH->lockMutex(m_mutex); {
++s->m_refCount; std::lock_guard<std::mutex> lock(m_mutex);
ARCH->unlockMutex(m_mutex); ++s->m_refCount;
}
throwError(err); throwError(err);
} }
WSACloseEvent_winsock(s->m_event); WSACloseEvent_winsock(s->m_event);
@ -690,9 +684,8 @@ std::vector<ArchNetAddress> ArchNetworkWinsock::nameToAddr(const std::string &na
hints.ai_family = AF_UNSPEC; hints.ai_family = AF_UNSPEC;
int ret = -1; int ret = -1;
ARCH->lockMutex(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
if ((ret = getaddrinfo(name.c_str(), nullptr, &hints, &pResult)) != 0) { if ((ret = getaddrinfo(name.c_str(), nullptr, &hints, &pResult)) != 0) {
ARCH->unlockMutex(m_mutex);
throwNameError(ret); throwNameError(ret);
} }
@ -708,7 +701,6 @@ std::vector<ArchNetAddress> ArchNetworkWinsock::nameToAddr(const std::string &na
} }
freeaddrinfo(pResult); freeaddrinfo(pResult);
ARCH->unlockMutex(m_mutex);
return addresses; return addresses;
} }

View file

@ -21,6 +21,7 @@
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <Windows.h> #include <Windows.h>
#include <list> #include <list>
#include <mutex>
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
@ -51,7 +52,7 @@ public:
class ArchNetworkWinsock : public IArchNetwork class ArchNetworkWinsock : public IArchNetwork
{ {
public: public:
ArchNetworkWinsock(); ArchNetworkWinsock() = default;
~ArchNetworkWinsock() override; ~ArchNetworkWinsock() override;
void init() override; void init() override;
@ -97,6 +98,6 @@ private:
private: private:
using EventList = std::list<WSAEVENT>; using EventList = std::list<WSAEVENT>;
ArchMutex m_mutex; std::mutex m_mutex;
EventList m_unblockEvents; EventList m_unblockEvents;
}; };