diff --git a/src/lib/arch/IArchString.cpp b/src/lib/arch/IArchString.cpp index f618c125..0f7f3006 100644 --- a/src/lib/arch/IArchString.cpp +++ b/src/lib/arch/IArchString.cpp @@ -24,7 +24,9 @@ #include #include -static ArchMutex s_mutex = NULL; +#include + +std::mutex s_mutex; // // use C library non-reentrant multibyte conversion with mutex @@ -32,16 +34,14 @@ static ArchMutex s_mutex = NULL; IArchString::~IArchString() { - if (s_mutex != NULL) { - ARCH->closeMutex(s_mutex); - s_mutex = NULL; - } } int IArchString::convStringWCToMB(char* dst, const wchar_t* src, UInt32 n, bool* errors) { + std::lock_guard lock(s_mutex); + ptrdiff_t len = 0; bool dummyErrors; @@ -49,12 +49,6 @@ IArchString::convStringWCToMB(char* dst, errors = &dummyErrors; } - if (s_mutex == NULL) { - s_mutex = ARCH->newMutex(); - } - - ARCH->lockMutex(s_mutex); - if (dst == NULL) { char dummy[MB_LEN_MAX]; for (const wchar_t* scan = src; n > 0; ++scan, --n) { @@ -89,7 +83,6 @@ IArchString::convStringWCToMB(char* dst, } len = dst - dst0; } - ARCH->unlockMutex(s_mutex); return (int)len; } @@ -98,6 +91,8 @@ int IArchString::convStringMBToWC(wchar_t* dst, const char* src, UInt32 n_param, bool* errors) { + std::lock_guard lock(s_mutex); + ptrdiff_t n = (ptrdiff_t)n_param; // fix compiler warning ptrdiff_t len = 0; wchar_t dummy; @@ -107,12 +102,6 @@ IArchString::convStringMBToWC(wchar_t* dst, errors = &dummyErrors; } - if (s_mutex == NULL) { - s_mutex = ARCH->newMutex(); - } - - ARCH->lockMutex(s_mutex); - if (dst == NULL) { for (const char* scan = src; n > 0; ) { ptrdiff_t mblen = mbtowc(&dummy, scan, n); @@ -184,7 +173,6 @@ IArchString::convStringMBToWC(wchar_t* dst, } len = dst - dst0; } - ARCH->unlockMutex(s_mutex); return (int)len; } diff --git a/src/lib/arch/unix/ArchMultithreadPosix.cpp b/src/lib/arch/unix/ArchMultithreadPosix.cpp index ade6c513..c9ddc6ce 100644 --- a/src/lib/arch/unix/ArchMultithreadPosix.cpp +++ b/src/lib/arch/unix/ArchMultithreadPosix.cpp @@ -114,9 +114,6 @@ ArchMultithreadPosix::ArchMultithreadPosix() : m_signalUserData[i] = NULL; } - // create mutex for thread list - m_threadMutex = newMutex(); - // create thread for calling (main) thread and add it to our // list. no need to lock the mutex since we're the only thread. m_mainThread = new ArchThreadImpl; @@ -153,26 +150,22 @@ ArchMultithreadPosix::~ArchMultithreadPosix() { assert(s_instance != NULL); - closeMutex(m_threadMutex); s_instance = NULL; } void ArchMultithreadPosix::setNetworkDataForCurrentThread(void* data) { - lockMutex(m_threadMutex); + std::lock_guard lock(m_threadMutex); ArchThreadImpl* thread = find(pthread_self()); thread->m_networkData = data; - unlockMutex(m_threadMutex); } void* ArchMultithreadPosix::getNetworkDataForThread(ArchThread thread) { - lockMutex(m_threadMutex); - void* data = thread->m_networkData; - unlockMutex(m_threadMutex); - return data; + std::lock_guard lock(m_threadMutex); + return thread->m_networkData; } ArchMultithreadPosix* @@ -356,7 +349,8 @@ ArchMultithreadPosix::newThread(ThreadFunc func, void* data) #endif } - lockMutex(m_threadMutex); + // note that the child thread will wait until we release this mutex + std::lock_guard lock(m_threadMutex); // create thread impl for new thread ArchThreadImpl* thread = new ArchThreadImpl; @@ -387,18 +381,15 @@ ArchMultithreadPosix::newThread(ThreadFunc func, void* data) refThread(thread); } - // note that the child thread will wait until we release this mutex - unlockMutex(m_threadMutex); - return thread; } ArchThread ArchMultithreadPosix::newCurrentThread() { - lockMutex(m_threadMutex); + std::lock_guard lock(m_threadMutex); + ArchThreadImpl* thread = find(pthread_self()); - unlockMutex(m_threadMutex); assert(thread != NULL); return thread; } @@ -416,10 +407,11 @@ ArchMultithreadPosix::closeThread(ArchThread thread) } // remove thread from list - lockMutex(m_threadMutex); - assert(findNoRef(thread->m_thread) == thread); - erase(thread); - unlockMutex(m_threadMutex); + { + std::lock_guard lock(m_threadMutex); + assert(findNoRef(thread->m_thread) == thread); + erase(thread); + } // done with thread delete thread; @@ -440,12 +432,14 @@ ArchMultithreadPosix::cancelThread(ArchThread thread) // set cancel and wakeup flags if thread can be cancelled bool wakeup = false; - lockMutex(m_threadMutex); - if (!thread->m_exited && !thread->m_cancelling) { - thread->m_cancel = true; - wakeup = true; + + { + std::lock_guard lock(m_threadMutex); + if (!thread->m_exited && !thread->m_cancelling) { + thread->m_cancel = true; + wakeup = true; + } } - unlockMutex(m_threadMutex); // force thread to exit system calls if wakeup is true if (wakeup) { @@ -465,9 +459,11 @@ void ArchMultithreadPosix::testCancelThread() { // find current thread - lockMutex(m_threadMutex); - ArchThreadImpl* thread = findNoRef(pthread_self()); - unlockMutex(m_threadMutex); + ArchThreadImpl* thread = nullptr; + { + std::lock_guard lock(m_threadMutex); + thread = findNoRef(pthread_self()); + } // test cancel on thread testCancelThreadImpl(thread); @@ -478,22 +474,23 @@ ArchMultithreadPosix::wait(ArchThread target, double timeout) { assert(target != NULL); - lockMutex(m_threadMutex); + ArchThreadImpl* self = nullptr; - // find current thread - ArchThreadImpl* self = findNoRef(pthread_self()); + { + std::lock_guard lock(m_threadMutex); - // ignore wait if trying to wait on ourself - if (target == self) { - unlockMutex(m_threadMutex); - return false; + // find current thread + self = findNoRef(pthread_self()); + + // ignore wait if trying to wait on ourself + if (target == self) { + return false; + } + + // ref the target so it can't go away while we're watching it + refThread(target); } - // ref the target so it can't go away while we're watching it - refThread(target); - - unlockMutex(m_threadMutex); - try { // do first test regardless of timeout testCancelThreadImpl(self); @@ -538,19 +535,15 @@ ArchMultithreadPosix::isSameThread(ArchThread thread1, ArchThread thread2) bool ArchMultithreadPosix::isExitedThread(ArchThread thread) { - lockMutex(m_threadMutex); - bool exited = thread->m_exited; - unlockMutex(m_threadMutex); - return exited; + std::lock_guard lock(m_threadMutex); + return thread->m_exited; } void* ArchMultithreadPosix::getResultOfThread(ArchThread thread) { - lockMutex(m_threadMutex); - void* result = thread->m_result; - unlockMutex(m_threadMutex); - return result; + std::lock_guard lock(m_threadMutex); + return thread->m_result; } IArchMultithread::ThreadID @@ -563,16 +556,15 @@ void ArchMultithreadPosix::setSignalHandler( ESignal signal, SignalFunc func, void* userData) { - lockMutex(m_threadMutex); + std::lock_guard lock(m_threadMutex); m_signalFunc[signal] = func; m_signalUserData[signal] = userData; - unlockMutex(m_threadMutex); } void ArchMultithreadPosix::raiseSignal(ESignal signal) { - lockMutex(m_threadMutex); + std::lock_guard lock(m_threadMutex); if (m_signalFunc[signal] != NULL) { m_signalFunc[signal](signal, m_signalUserData[signal]); pthread_kill(m_mainThread->m_thread, SIGWAKEUP); @@ -580,7 +572,6 @@ ArchMultithreadPosix::raiseSignal(ESignal signal) else if (signal == kINTERRUPT || signal == kTERMINATE) { ARCH->cancelThread(m_mainThread); } - unlockMutex(m_threadMutex); } void @@ -677,15 +668,15 @@ ArchMultithreadPosix::testCancelThreadImpl(ArchThreadImpl* thread) { assert(thread != NULL); + std::lock_guard lock(m_threadMutex); + // update cancel state - lockMutex(m_threadMutex); bool cancel = false; if (thread->m_cancel && !thread->m_cancelling) { thread->m_cancelling = true; thread->m_cancel = false; cancel = true; } - unlockMutex(m_threadMutex); // unwind thread's stack if cancelling if (cancel) { @@ -717,8 +708,9 @@ ArchMultithreadPosix::doThreadFunc(ArchThread thread) setPriorityOfThread(thread, 1); // wait for parent to initialize this object - lockMutex(m_threadMutex); - unlockMutex(m_threadMutex); + { + std::lock_guard lock(m_threadMutex); + } void* result = NULL; try { @@ -731,18 +723,20 @@ ArchMultithreadPosix::doThreadFunc(ArchThread thread) } catch (...) { // note -- don't catch (...) to avoid masking bugs - lockMutex(m_threadMutex); - thread->m_exited = true; - unlockMutex(m_threadMutex); + { + std::lock_guard lock(m_threadMutex); + thread->m_exited = true; + } closeThread(thread); throw; } // thread has exited - lockMutex(m_threadMutex); - thread->m_result = result; - thread->m_exited = true; - unlockMutex(m_threadMutex); + { + std::lock_guard lock(m_threadMutex); + thread->m_result = result; + thread->m_exited = true; + } // done with thread closeThread(thread); diff --git a/src/lib/arch/unix/ArchMultithreadPosix.h b/src/lib/arch/unix/ArchMultithreadPosix.h index 98b5eda3..4bd879f5 100644 --- a/src/lib/arch/unix/ArchMultithreadPosix.h +++ b/src/lib/arch/unix/ArchMultithreadPosix.h @@ -22,6 +22,7 @@ #include "common/stdlist.h" #include +#include #define ARCH_MULTITHREAD ArchMultithreadPosix @@ -104,7 +105,7 @@ private: bool m_newThreadCalled; - ArchMutex m_threadMutex; + std::mutex m_threadMutex; ArchThread m_mainThread; ThreadList m_threadList; ThreadID m_nextID; diff --git a/src/lib/base/EventQueue.cpp b/src/lib/base/EventQueue.cpp index b17e35bf..fe8cff5d 100644 --- a/src/lib/base/EventQueue.cpp +++ b/src/lib/base/EventQueue.cpp @@ -90,7 +90,6 @@ EventQueue::EventQueue() : m_readyMutex(new Mutex), m_readyCondVar(new CondVar(m_readyMutex, false)) { - m_mutex = ARCH->newMutex(); ARCH->setSignalHandler(Arch::kINTERRUPT, &interrupt, this); ARCH->setSignalHandler(Arch::kTERMINATE, &interrupt, this); m_buffer = new SimpleEventQueueBuffer; @@ -104,7 +103,6 @@ EventQueue::~EventQueue() ARCH->setSignalHandler(Arch::kINTERRUPT, NULL, NULL); ARCH->setSignalHandler(Arch::kTERMINATE, NULL, NULL); - ARCH->closeMutex(m_mutex); } void @@ -136,7 +134,7 @@ EventQueue::loop() Event::Type EventQueue::registerTypeOnce(Event::Type& type, const char* name) { - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); if (type == Event::kUnknown) { m_typeMap.insert(std::make_pair(m_nextType, name)); m_nameMap.insert(std::make_pair(name, m_nextType)); @@ -176,7 +174,7 @@ EventQueue::getTypeName(Event::Type type) void EventQueue::adoptBuffer(IEventQueueBuffer* buffer) { - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); LOG((CLOG_DEBUG "adopting new buffer")); @@ -261,7 +259,7 @@ retry: case IEventQueueBuffer::kUser: { - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); event = removeEvent(dataID); return true; } @@ -316,7 +314,7 @@ EventQueue::addEvent(const Event& event) void EventQueue::addEventToBuffer(const Event& event) { - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); // store the event's data locally UInt32 eventID = saveEvent(event); @@ -338,7 +336,7 @@ EventQueue::newTimer(double duration, void* target) if (target == NULL) { target = timer; } - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); m_timers.insert(timer); // initial duration is requested duration plus whatever's on // the clock currently because the latter will be subtracted @@ -357,7 +355,7 @@ EventQueue::newOneShotTimer(double duration, void* target) if (target == NULL) { target = timer; } - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); m_timers.insert(timer); // initial duration is requested duration plus whatever's on // the clock currently because the latter will be subtracted @@ -370,7 +368,7 @@ EventQueue::newOneShotTimer(double duration, void* target) void EventQueue::deleteTimer(EventQueueTimer* timer) { - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); for (TimerQueue::iterator index = m_timerQueue.begin(); index != m_timerQueue.end(); ++index) { if (index->getTimer() == timer) { @@ -388,7 +386,7 @@ EventQueue::deleteTimer(EventQueueTimer* timer) void EventQueue::adoptHandler(Event::Type type, void* target, IEventJob* handler) { - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); IEventJob*& job = m_handlers[target][type]; delete job; job = handler; @@ -399,7 +397,7 @@ EventQueue::removeHandler(Event::Type type, void* target) { IEventJob* handler = NULL; { - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); HandlerTable::iterator index = m_handlers.find(target); if (index != m_handlers.end()) { TypeHandlerTable& typeHandlers = index->second; @@ -418,7 +416,7 @@ EventQueue::removeHandlers(void* target) { std::vector handlers; { - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); HandlerTable::iterator index = m_handlers.find(target); if (index != m_handlers.end()) { // copy to handlers array and clear table for target @@ -447,7 +445,7 @@ EventQueue::isEmpty() const IEventJob* EventQueue::getHandler(Event::Type type, void* target) const { - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); HandlerTable::const_iterator index = m_handlers.find(target); if (index != m_handlers.end()) { const TypeHandlerTable& typeHandlers = index->second; diff --git a/src/lib/base/EventQueue.h b/src/lib/base/EventQueue.h index 97e7fba7..0a2179be 100644 --- a/src/lib/base/EventQueue.h +++ b/src/lib/base/EventQueue.h @@ -28,10 +28,9 @@ #include "common/stdset.h" #include "base/NonBlockingStream.h" +#include #include -class Mutex; - //! Event queue /*! An event queue that implements the platform independent parts and @@ -114,7 +113,7 @@ private: typedef std::map HandlerTable; int m_systemTarget; - ArchMutex m_mutex; + mutable std::mutex m_mutex; // registered events Event::Type m_nextType; diff --git a/src/lib/base/Log.cpp b/src/lib/base/Log.cpp index 823bf6d1..1252ed9f 100644 --- a/src/lib/base/Log.cpp +++ b/src/lib/base/Log.cpp @@ -63,9 +63,6 @@ Log::Log() { assert(s_log == NULL); - // create mutex for multithread safe operation - m_mutex = ARCH->newMutex(); - // other initalization m_maxPriority = g_defaultMaxPriority; m_maxNewlineLength = 0; @@ -90,7 +87,6 @@ Log::~Log() index != m_alwaysOutputters.end(); ++index) { delete *index; } - ARCH->closeMutex(m_mutex); } Log* @@ -214,7 +210,7 @@ Log::insert(ILogOutputter* outputter, bool alwaysAtHead) { assert(outputter != NULL); - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); if (alwaysAtHead) { m_alwaysOutputters.push_front(outputter); } @@ -237,7 +233,7 @@ Log::insert(ILogOutputter* outputter, bool alwaysAtHead) void Log::remove(ILogOutputter* outputter) { - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); m_outputters.remove(outputter); m_alwaysOutputters.remove(outputter); } @@ -245,7 +241,7 @@ Log::remove(ILogOutputter* outputter) void Log::pop_front(bool alwaysAtHead) { - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); OutputterList* list = alwaysAtHead ? &m_alwaysOutputters : &m_outputters; if (!list->empty()) { delete list->front(); @@ -271,14 +267,14 @@ Log::setFilter(const char* maxPriority) void Log::setFilter(int maxPriority) { - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); m_maxPriority = maxPriority; } int Log::getFilter() const { - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); return m_maxPriority; } @@ -289,7 +285,7 @@ Log::output(ELevel priority, char* msg) assert(msg != NULL); if (!msg) return; - ArchMutexLock lock(m_mutex); + std::lock_guard lock(m_mutex); OutputterList::const_iterator i; diff --git a/src/lib/base/Log.h b/src/lib/base/Log.h index 1d09be22..0ed458f8 100644 --- a/src/lib/base/Log.h +++ b/src/lib/base/Log.h @@ -24,6 +24,7 @@ #include "common/stdlist.h" #include +#include #define CLOG (Log::getInstance()) #define BYE "\nTry `%s --help' for more information." @@ -132,7 +133,7 @@ private: static Log* s_log; - ArchMutex m_mutex; + mutable std::mutex m_mutex; OutputterList m_outputters; OutputterList m_alwaysOutputters; int m_maxNewlineLength; diff --git a/src/lib/ipc/IpcClientProxy.cpp b/src/lib/ipc/IpcClientProxy.cpp index af85ecab..51042774 100644 --- a/src/lib/ipc/IpcClientProxy.cpp +++ b/src/lib/ipc/IpcClientProxy.cpp @@ -34,8 +34,6 @@ IpcClientProxy::IpcClientProxy(barrier::IStream& stream, IEventQueue* events) : m_stream(stream), m_clientType(kIpcClientUnknown), m_disconnecting(false), - m_readMutex(ARCH->newMutex()), - m_writeMutex(ARCH->newMutex()), m_events(events) { m_events->adoptHandler( @@ -71,14 +69,11 @@ IpcClientProxy::~IpcClientProxy() m_events->forIStream().outputShutdown(), m_stream.getEventTarget()); // don't delete the stream while it's being used. - ARCH->lockMutex(m_readMutex); - ARCH->lockMutex(m_writeMutex); - delete &m_stream; - ARCH->unlockMutex(m_readMutex); - ARCH->unlockMutex(m_writeMutex); - - ARCH->closeMutex(m_readMutex); - ARCH->closeMutex(m_writeMutex); + { + std::lock_guard lock_read(m_readMutex); + std::lock_guard lock_write(m_writeMutex); + delete &m_stream; + } } void @@ -99,7 +94,7 @@ void IpcClientProxy::handleData(const Event&, void*) { // don't allow the dtor to destroy the stream while we're using it. - ArchMutexLock lock(m_readMutex); + std::lock_guard lock(m_readMutex); LOG((CLOG_DEBUG "start ipc handle data")); @@ -139,7 +134,7 @@ IpcClientProxy::send(const IpcMessage& message) // don't allow other threads to write until we've finished the entire // message. stream write is locked, but only for that single write. // also, don't allow the dtor to destroy the stream while we're using it. - ArchMutexLock lock(m_writeMutex); + std::lock_guard lock(m_writeMutex); LOG((CLOG_DEBUG4 "ipc write: %d", message.type())); diff --git a/src/lib/ipc/IpcClientProxy.h b/src/lib/ipc/IpcClientProxy.h index eaa12c78..eb9f1e9b 100644 --- a/src/lib/ipc/IpcClientProxy.h +++ b/src/lib/ipc/IpcClientProxy.h @@ -23,6 +23,8 @@ #include "base/EventTypes.h" #include "base/Event.h" +#include + namespace barrier { class IStream; } class IpcMessage; class IpcCommandMessage; @@ -49,7 +51,7 @@ private: barrier::IStream& m_stream; EIpcClientType m_clientType; bool m_disconnecting; - ArchMutex m_readMutex; - ArchMutex m_writeMutex; + std::mutex m_readMutex; + std::mutex m_writeMutex; IEventQueue* m_events; }; diff --git a/src/lib/ipc/IpcLogOutputter.cpp b/src/lib/ipc/IpcLogOutputter.cpp index 984793e4..b62c76a0 100644 --- a/src/lib/ipc/IpcLogOutputter.cpp +++ b/src/lib/ipc/IpcLogOutputter.cpp @@ -39,7 +39,6 @@ enum EIpcLogOutputter { IpcLogOutputter::IpcLogOutputter(IpcServer& ipcServer, EIpcClientType clientType, bool useThread) : m_ipcServer(ipcServer), - m_bufferMutex(ARCH->newMutex()), m_sending(false), m_bufferThread(nullptr), m_running(false), @@ -52,8 +51,7 @@ IpcLogOutputter::IpcLogOutputter(IpcServer& ipcServer, EIpcClientType clientType m_bufferRateTimeLimit(kBufferRateTimeLimit), m_bufferWriteCount(0), m_bufferRateStart(ARCH->time()), - m_clientType(clientType), - m_runningMutex(ARCH->newMutex()) + m_clientType(clientType) { if (useThread) { m_bufferThread = new Thread(new TMethodJob( @@ -65,8 +63,6 @@ IpcLogOutputter::~IpcLogOutputter() { close(); - ARCH->closeMutex(m_bufferMutex); - if (m_bufferThread != nullptr) { m_bufferThread->cancel(); m_bufferThread->wait(); @@ -86,7 +82,7 @@ void IpcLogOutputter::close() { if (m_bufferThread != nullptr) { - ArchMutexLock lock(m_runningMutex); + std::lock_guard lock(m_runningMutex); m_running = false; notifyBuffer(); m_bufferThread->wait(5); @@ -116,7 +112,7 @@ IpcLogOutputter::write(ELevel, const char* text) void IpcLogOutputter::appendBuffer(const String& text) { - ArchMutexLock lock(m_bufferMutex); + std::lock_guard lock(m_bufferMutex); double elapsed = ARCH->time() - m_bufferRateStart; if (elapsed < m_bufferRateTimeLimit) { @@ -143,7 +139,7 @@ IpcLogOutputter::appendBuffer(const String& text) bool IpcLogOutputter::isRunning() { - ArchMutexLock lock(m_runningMutex); + std::lock_guard lock(m_runningMutex); return m_running; } @@ -180,7 +176,7 @@ IpcLogOutputter::notifyBuffer() String IpcLogOutputter::getChunk(size_t count) { - ArchMutexLock lock(m_bufferMutex); + std::lock_guard lock(m_bufferMutex); if (m_buffer.size() < count) { count = m_buffer.size(); diff --git a/src/lib/ipc/IpcLogOutputter.h b/src/lib/ipc/IpcLogOutputter.h index 461f022d..2f1b98c6 100644 --- a/src/lib/ipc/IpcLogOutputter.h +++ b/src/lib/ipc/IpcLogOutputter.h @@ -24,6 +24,7 @@ #include "ipc/Ipc.h" #include +#include class IpcServer; class Event; @@ -100,7 +101,7 @@ private: IpcServer& m_ipcServer; Buffer m_buffer; - ArchMutex m_bufferMutex; + std::mutex m_bufferMutex; bool m_sending; Thread* m_bufferThread; bool m_running; @@ -115,5 +116,5 @@ private: UInt16 m_bufferWriteCount; double m_bufferRateStart; EIpcClientType m_clientType; - ArchMutex m_runningMutex; + std::mutex m_runningMutex; }; diff --git a/src/lib/ipc/IpcServer.cpp b/src/lib/ipc/IpcServer.cpp index e05a913f..8df98d1e 100644 --- a/src/lib/ipc/IpcServer.cpp +++ b/src/lib/ipc/IpcServer.cpp @@ -56,7 +56,6 @@ IpcServer::init() { m_socket = new TCPListenSocket(m_events, m_socketMultiplexer, IArchNetwork::kINET); - m_clientsMutex = ARCH->newMutex(); m_address.resolve(); m_events->adoptHandler( @@ -75,15 +74,15 @@ IpcServer::~IpcServer() delete m_socket; } - ARCH->lockMutex(m_clientsMutex); - ClientList::iterator it; - for (it = m_clients.begin(); it != m_clients.end(); it++) { - deleteClient(*it); + { + std::lock_guard lock(m_clientsMutex); + ClientList::iterator it; + for (it = m_clients.begin(); it != m_clients.end(); 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); } @@ -103,10 +102,12 @@ IpcServer::handleClientConnecting(const Event&, void*) LOG((CLOG_DEBUG "accepted ipc client connection")); - ARCH->lockMutex(m_clientsMutex); - IpcClientProxy* proxy = new IpcClientProxy(*stream, m_events); - m_clients.push_back(proxy); - ARCH->unlockMutex(m_clientsMutex); + IpcClientProxy* proxy = nullptr; + { + std::lock_guard lock(m_clientsMutex); + proxy = new IpcClientProxy(*stream, m_events); + m_clients.push_back(proxy); + } m_events->adoptHandler( m_events->forIpcClientProxy().disconnected(), proxy, @@ -127,7 +128,7 @@ IpcServer::handleClientDisconnected(const Event& e, void*) { IpcClientProxy* proxy = static_cast(e.getTarget()); - ArchMutexLock lock(m_clientsMutex); + std::lock_guard lock(m_clientsMutex); m_clients.remove(proxy); deleteClient(proxy); @@ -153,7 +154,7 @@ IpcServer::deleteClient(IpcClientProxy* proxy) bool IpcServer::hasClients(EIpcClientType clientType) const { - ArchMutexLock lock(m_clientsMutex); + std::lock_guard lock(m_clientsMutex); if (m_clients.empty()) { return false; @@ -175,7 +176,7 @@ IpcServer::hasClients(EIpcClientType clientType) const void IpcServer::send(const IpcMessage& message, EIpcClientType filterType) { - ArchMutexLock lock(m_clientsMutex); + std::lock_guard lock(m_clientsMutex); ClientList::iterator it; for (it = m_clients.begin(); it != m_clients.end(); it++) { diff --git a/src/lib/ipc/IpcServer.h b/src/lib/ipc/IpcServer.h index d9bbe3e8..179bad8c 100644 --- a/src/lib/ipc/IpcServer.h +++ b/src/lib/ipc/IpcServer.h @@ -25,6 +25,7 @@ #include "base/EventTypes.h" #include +#include class Event; class IpcClientProxy; @@ -79,7 +80,7 @@ private: TCPListenSocket* m_socket; NetworkAddress m_address; ClientList m_clients; - ArchMutex m_clientsMutex; + mutable std::mutex m_clientsMutex; #ifdef TEST_ENV public: