Use std::mutex instead of ArchMutex in IpcServer

This commit is contained in:
Povilas Kanapickas 2019-08-17 16:40:22 +03:00
parent d9d39040ae
commit 36f3235f51
2 changed files with 19 additions and 17 deletions

View File

@ -56,7 +56,6 @@ IpcServer::init()
{ {
m_socket = new TCPListenSocket(m_events, m_socketMultiplexer, IArchNetwork::kINET); m_socket = new TCPListenSocket(m_events, m_socketMultiplexer, IArchNetwork::kINET);
m_clientsMutex = ARCH->newMutex();
m_address.resolve(); m_address.resolve();
m_events->adoptHandler( m_events->adoptHandler(
@ -75,14 +74,14 @@ IpcServer::~IpcServer()
delete m_socket; delete m_socket;
} }
ARCH->lockMutex(m_clientsMutex); {
std::lock_guard<std::mutex> lock(m_clientsMutex);
ClientList::iterator it; ClientList::iterator it;
for (it = m_clients.begin(); it != m_clients.end(); it++) { for (it = m_clients.begin(); it != m_clients.end(); it++) {
deleteClient(*it); deleteClient(*it);
} }
m_clients.clear(); m_clients.clear();
ARCH->unlockMutex(m_clientsMutex); }
ARCH->closeMutex(m_clientsMutex);
m_events->removeHandler(m_events->forIListenSocket().connecting(), m_socket); m_events->removeHandler(m_events->forIListenSocket().connecting(), m_socket);
} }
@ -103,10 +102,12 @@ IpcServer::handleClientConnecting(const Event&, void*)
LOG((CLOG_DEBUG "accepted ipc client connection")); LOG((CLOG_DEBUG "accepted ipc client connection"));
ARCH->lockMutex(m_clientsMutex); IpcClientProxy* proxy = nullptr;
IpcClientProxy* proxy = new IpcClientProxy(*stream, m_events); {
std::lock_guard<std::mutex> lock(m_clientsMutex);
proxy = new IpcClientProxy(*stream, m_events);
m_clients.push_back(proxy); m_clients.push_back(proxy);
ARCH->unlockMutex(m_clientsMutex); }
m_events->adoptHandler( m_events->adoptHandler(
m_events->forIpcClientProxy().disconnected(), proxy, m_events->forIpcClientProxy().disconnected(), proxy,
@ -127,7 +128,7 @@ IpcServer::handleClientDisconnected(const Event& e, void*)
{ {
IpcClientProxy* proxy = static_cast<IpcClientProxy*>(e.getTarget()); IpcClientProxy* proxy = static_cast<IpcClientProxy*>(e.getTarget());
ArchMutexLock lock(m_clientsMutex); std::lock_guard<std::mutex> lock(m_clientsMutex);
m_clients.remove(proxy); m_clients.remove(proxy);
deleteClient(proxy); deleteClient(proxy);
@ -153,7 +154,7 @@ IpcServer::deleteClient(IpcClientProxy* proxy)
bool bool
IpcServer::hasClients(EIpcClientType clientType) const IpcServer::hasClients(EIpcClientType clientType) const
{ {
ArchMutexLock lock(m_clientsMutex); std::lock_guard<std::mutex> lock(m_clientsMutex);
if (m_clients.empty()) { if (m_clients.empty()) {
return false; return false;
@ -175,7 +176,7 @@ IpcServer::hasClients(EIpcClientType clientType) const
void void
IpcServer::send(const IpcMessage& message, EIpcClientType filterType) IpcServer::send(const IpcMessage& message, EIpcClientType filterType)
{ {
ArchMutexLock lock(m_clientsMutex); std::lock_guard<std::mutex> lock(m_clientsMutex);
ClientList::iterator it; ClientList::iterator it;
for (it = m_clients.begin(); it != m_clients.end(); it++) { for (it = m_clients.begin(); it != m_clients.end(); it++) {

View File

@ -25,6 +25,7 @@
#include "base/EventTypes.h" #include "base/EventTypes.h"
#include <list> #include <list>
#include <mutex>
class Event; class Event;
class IpcClientProxy; class IpcClientProxy;
@ -79,7 +80,7 @@ private:
TCPListenSocket* m_socket; TCPListenSocket* m_socket;
NetworkAddress m_address; NetworkAddress m_address;
ClientList m_clients; ClientList m_clients;
ArchMutex m_clientsMutex; mutable std::mutex m_clientsMutex;
#ifdef TEST_ENV #ifdef TEST_ENV
public: public: