lib/arch/win32: Use standard mutex primitives in networking code

This commit is contained in:
Povilas Kanapickas 2021-11-03 02:58:40 +02:00
parent aa1c3ae7a4
commit c59074835f
2 changed files with 13 additions and 19 deletions

View File

@ -97,10 +97,7 @@ ArchNetAddressImpl::alloc(size_t size)
// ArchNetworkWinsock
//
ArchNetworkWinsock::ArchNetworkWinsock() :
m_mutex(NULL)
{
}
ArchNetworkWinsock::ArchNetworkWinsock() = default;
ArchNetworkWinsock::~ArchNetworkWinsock()
{
@ -111,9 +108,6 @@ ArchNetworkWinsock::~ArchNetworkWinsock()
WSACleanup_winsock = NULL;
s_networkModule = NULL;
}
if (m_mutex != NULL) {
ARCH->closeMutex(m_mutex);
}
EventList::iterator it;
for (it = m_unblockEvents.begin(); it != m_unblockEvents.end(); it++) {
@ -133,7 +127,6 @@ ArchNetworkWinsock::init()
for (size_t i = 0; i < sizeof(s_library) / sizeof(s_library[0]); ++i) {
try {
initModule((HMODULE)::LoadLibrary(s_library[i]));
m_mutex = ARCH->newMutex();
return;
}
catch (XArchNetwork&) {
@ -239,9 +232,8 @@ ArchNetworkWinsock::copySocket(ArchSocket s)
assert(s != NULL);
// ref the socket and return it
ARCH->lockMutex(m_mutex);
std::lock_guard<std::mutex> lock(mutex_);
++s->m_refCount;
ARCH->unlockMutex(m_mutex);
return s;
}
@ -251,18 +243,20 @@ ArchNetworkWinsock::closeSocket(ArchSocket s)
assert(s != NULL);
// unref the socket and note if it should be released
ARCH->lockMutex(m_mutex);
const bool doClose = (--s->m_refCount == 0);
ARCH->unlockMutex(m_mutex);
bool doClose = false;
{
std::lock_guard<std::mutex> lock(mutex_);
doClose = (--s->m_refCount == 0);
}
// close the socket if necessary
if (doClose) {
if (close_winsock(s->m_socket) == SOCKET_ERROR) {
// close failed. restore the last ref and throw.
int err = getsockerror_winsock();
ARCH->lockMutex(m_mutex);
std::lock_guard<std::mutex> lock(mutex_);
++s->m_refCount;
ARCH->unlockMutex(m_mutex);
throwError(err);
}
WSACloseEvent_winsock(s->m_event);
@ -729,9 +723,8 @@ ArchNetworkWinsock::nameToAddr(const std::string& name)
hints.ai_family = AF_UNSPEC;
int ret = -1;
ARCH->lockMutex(m_mutex);
std::lock_guard<std::mutex> lock(mutex_);
if ((ret = getaddrinfo(name.c_str(), NULL, &hints, &p)) != 0) {
ARCH->unlockMutex(m_mutex);
delete addr;
throwNameError(ret);
}
@ -744,7 +737,6 @@ ArchNetworkWinsock::nameToAddr(const std::string& name)
memcpy(&addr->m_addr, p->ai_addr, addr->m_len);
freeaddrinfo(p);
ARCH->unlockMutex(m_mutex);
return addr;
}

View File

@ -31,6 +31,8 @@
#include <WinSock2.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <mutex>
#include <list>
#pragma comment(lib, "ws2_32.lib")
@ -106,6 +108,6 @@ private:
private:
typedef std::list<WSAEVENT> EventList;
ArchMutex m_mutex;
std::mutex mutex_;
EventList m_unblockEvents;
};