Use standard mutex APIs instead of the home-grown wrapper (#410)

Use standard mutex APIs instead of the home-grown wrapper
This commit is contained in:
Adrian Lucrèce Céleste 2019-08-22 15:01:35 -04:00 committed by GitHub
commit fca05b9163
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 125 additions and 152 deletions

View File

@ -24,7 +24,9 @@
#include <cstring> #include <cstring>
#include <cstdlib> #include <cstdlib>
static ArchMutex s_mutex = NULL; #include <mutex>
std::mutex s_mutex;
// //
// use C library non-reentrant multibyte conversion with mutex // use C library non-reentrant multibyte conversion with mutex
@ -32,16 +34,14 @@ static ArchMutex s_mutex = NULL;
IArchString::~IArchString() IArchString::~IArchString()
{ {
if (s_mutex != NULL) {
ARCH->closeMutex(s_mutex);
s_mutex = NULL;
}
} }
int int
IArchString::convStringWCToMB(char* dst, IArchString::convStringWCToMB(char* dst,
const wchar_t* src, UInt32 n, bool* errors) const wchar_t* src, UInt32 n, bool* errors)
{ {
std::lock_guard<std::mutex> lock(s_mutex);
ptrdiff_t len = 0; ptrdiff_t len = 0;
bool dummyErrors; bool dummyErrors;
@ -49,12 +49,6 @@ IArchString::convStringWCToMB(char* dst,
errors = &dummyErrors; errors = &dummyErrors;
} }
if (s_mutex == NULL) {
s_mutex = ARCH->newMutex();
}
ARCH->lockMutex(s_mutex);
if (dst == NULL) { if (dst == NULL) {
char dummy[MB_LEN_MAX]; char dummy[MB_LEN_MAX];
for (const wchar_t* scan = src; n > 0; ++scan, --n) { for (const wchar_t* scan = src; n > 0; ++scan, --n) {
@ -89,7 +83,6 @@ IArchString::convStringWCToMB(char* dst,
} }
len = dst - dst0; len = dst - dst0;
} }
ARCH->unlockMutex(s_mutex);
return (int)len; return (int)len;
} }
@ -98,6 +91,8 @@ int
IArchString::convStringMBToWC(wchar_t* dst, IArchString::convStringMBToWC(wchar_t* dst,
const char* src, UInt32 n_param, bool* errors) const char* src, UInt32 n_param, bool* errors)
{ {
std::lock_guard<std::mutex> lock(s_mutex);
ptrdiff_t n = (ptrdiff_t)n_param; // fix compiler warning ptrdiff_t n = (ptrdiff_t)n_param; // fix compiler warning
ptrdiff_t len = 0; ptrdiff_t len = 0;
wchar_t dummy; wchar_t dummy;
@ -107,12 +102,6 @@ IArchString::convStringMBToWC(wchar_t* dst,
errors = &dummyErrors; errors = &dummyErrors;
} }
if (s_mutex == NULL) {
s_mutex = ARCH->newMutex();
}
ARCH->lockMutex(s_mutex);
if (dst == NULL) { if (dst == NULL) {
for (const char* scan = src; n > 0; ) { for (const char* scan = src; n > 0; ) {
ptrdiff_t mblen = mbtowc(&dummy, scan, n); ptrdiff_t mblen = mbtowc(&dummy, scan, n);
@ -184,7 +173,6 @@ IArchString::convStringMBToWC(wchar_t* dst,
} }
len = dst - dst0; len = dst - dst0;
} }
ARCH->unlockMutex(s_mutex);
return (int)len; return (int)len;
} }

View File

@ -114,9 +114,6 @@ ArchMultithreadPosix::ArchMultithreadPosix() :
m_signalUserData[i] = NULL; m_signalUserData[i] = NULL;
} }
// create mutex for thread list
m_threadMutex = newMutex();
// create thread for calling (main) thread and add it to our // create thread for calling (main) thread and add it to our
// list. no need to lock the mutex since we're the only thread. // list. no need to lock the mutex since we're the only thread.
m_mainThread = new ArchThreadImpl; m_mainThread = new ArchThreadImpl;
@ -153,26 +150,22 @@ ArchMultithreadPosix::~ArchMultithreadPosix()
{ {
assert(s_instance != NULL); assert(s_instance != NULL);
closeMutex(m_threadMutex);
s_instance = NULL; s_instance = NULL;
} }
void void
ArchMultithreadPosix::setNetworkDataForCurrentThread(void* data) ArchMultithreadPosix::setNetworkDataForCurrentThread(void* data)
{ {
lockMutex(m_threadMutex); std::lock_guard<std::mutex> lock(m_threadMutex);
ArchThreadImpl* thread = find(pthread_self()); ArchThreadImpl* thread = find(pthread_self());
thread->m_networkData = data; thread->m_networkData = data;
unlockMutex(m_threadMutex);
} }
void* void*
ArchMultithreadPosix::getNetworkDataForThread(ArchThread thread) ArchMultithreadPosix::getNetworkDataForThread(ArchThread thread)
{ {
lockMutex(m_threadMutex); std::lock_guard<std::mutex> lock(m_threadMutex);
void* data = thread->m_networkData; return thread->m_networkData;
unlockMutex(m_threadMutex);
return data;
} }
ArchMultithreadPosix* ArchMultithreadPosix*
@ -356,7 +349,8 @@ ArchMultithreadPosix::newThread(ThreadFunc func, void* data)
#endif #endif
} }
lockMutex(m_threadMutex); // note that the child thread will wait until we release this mutex
std::lock_guard<std::mutex> lock(m_threadMutex);
// create thread impl for new thread // create thread impl for new thread
ArchThreadImpl* thread = new ArchThreadImpl; ArchThreadImpl* thread = new ArchThreadImpl;
@ -387,18 +381,15 @@ ArchMultithreadPosix::newThread(ThreadFunc func, void* data)
refThread(thread); refThread(thread);
} }
// note that the child thread will wait until we release this mutex
unlockMutex(m_threadMutex);
return thread; return thread;
} }
ArchThread ArchThread
ArchMultithreadPosix::newCurrentThread() ArchMultithreadPosix::newCurrentThread()
{ {
lockMutex(m_threadMutex); std::lock_guard<std::mutex> lock(m_threadMutex);
ArchThreadImpl* thread = find(pthread_self()); ArchThreadImpl* thread = find(pthread_self());
unlockMutex(m_threadMutex);
assert(thread != NULL); assert(thread != NULL);
return thread; return thread;
} }
@ -416,10 +407,11 @@ ArchMultithreadPosix::closeThread(ArchThread thread)
} }
// remove thread from list // remove thread from list
lockMutex(m_threadMutex); {
std::lock_guard<std::mutex> lock(m_threadMutex);
assert(findNoRef(thread->m_thread) == thread); assert(findNoRef(thread->m_thread) == thread);
erase(thread); erase(thread);
unlockMutex(m_threadMutex); }
// done with thread // done with thread
delete thread; delete thread;
@ -440,12 +432,14 @@ ArchMultithreadPosix::cancelThread(ArchThread thread)
// set cancel and wakeup flags if thread can be cancelled // set cancel and wakeup flags if thread can be cancelled
bool wakeup = false; bool wakeup = false;
lockMutex(m_threadMutex);
{
std::lock_guard<std::mutex> lock(m_threadMutex);
if (!thread->m_exited && !thread->m_cancelling) { if (!thread->m_exited && !thread->m_cancelling) {
thread->m_cancel = true; thread->m_cancel = true;
wakeup = true; wakeup = true;
} }
unlockMutex(m_threadMutex); }
// force thread to exit system calls if wakeup is true // force thread to exit system calls if wakeup is true
if (wakeup) { if (wakeup) {
@ -465,9 +459,11 @@ void
ArchMultithreadPosix::testCancelThread() ArchMultithreadPosix::testCancelThread()
{ {
// find current thread // find current thread
lockMutex(m_threadMutex); ArchThreadImpl* thread = nullptr;
ArchThreadImpl* thread = findNoRef(pthread_self()); {
unlockMutex(m_threadMutex); std::lock_guard<std::mutex> lock(m_threadMutex);
thread = findNoRef(pthread_self());
}
// test cancel on thread // test cancel on thread
testCancelThreadImpl(thread); testCancelThreadImpl(thread);
@ -478,21 +474,22 @@ ArchMultithreadPosix::wait(ArchThread target, double timeout)
{ {
assert(target != NULL); assert(target != NULL);
lockMutex(m_threadMutex); ArchThreadImpl* self = nullptr;
{
std::lock_guard<std::mutex> lock(m_threadMutex);
// find current thread // find current thread
ArchThreadImpl* self = findNoRef(pthread_self()); self = findNoRef(pthread_self());
// ignore wait if trying to wait on ourself // ignore wait if trying to wait on ourself
if (target == self) { if (target == self) {
unlockMutex(m_threadMutex);
return false; return false;
} }
// ref the target so it can't go away while we're watching it // ref the target so it can't go away while we're watching it
refThread(target); refThread(target);
}
unlockMutex(m_threadMutex);
try { try {
// do first test regardless of timeout // do first test regardless of timeout
@ -538,19 +535,15 @@ ArchMultithreadPosix::isSameThread(ArchThread thread1, ArchThread thread2)
bool bool
ArchMultithreadPosix::isExitedThread(ArchThread thread) ArchMultithreadPosix::isExitedThread(ArchThread thread)
{ {
lockMutex(m_threadMutex); std::lock_guard<std::mutex> lock(m_threadMutex);
bool exited = thread->m_exited; return thread->m_exited;
unlockMutex(m_threadMutex);
return exited;
} }
void* void*
ArchMultithreadPosix::getResultOfThread(ArchThread thread) ArchMultithreadPosix::getResultOfThread(ArchThread thread)
{ {
lockMutex(m_threadMutex); std::lock_guard<std::mutex> lock(m_threadMutex);
void* result = thread->m_result; return thread->m_result;
unlockMutex(m_threadMutex);
return result;
} }
IArchMultithread::ThreadID IArchMultithread::ThreadID
@ -563,16 +556,15 @@ void
ArchMultithreadPosix::setSignalHandler( ArchMultithreadPosix::setSignalHandler(
ESignal signal, SignalFunc func, void* userData) ESignal signal, SignalFunc func, void* userData)
{ {
lockMutex(m_threadMutex); std::lock_guard<std::mutex> lock(m_threadMutex);
m_signalFunc[signal] = func; m_signalFunc[signal] = func;
m_signalUserData[signal] = userData; m_signalUserData[signal] = userData;
unlockMutex(m_threadMutex);
} }
void void
ArchMultithreadPosix::raiseSignal(ESignal signal) ArchMultithreadPosix::raiseSignal(ESignal signal)
{ {
lockMutex(m_threadMutex); std::lock_guard<std::mutex> lock(m_threadMutex);
if (m_signalFunc[signal] != NULL) { if (m_signalFunc[signal] != NULL) {
m_signalFunc[signal](signal, m_signalUserData[signal]); m_signalFunc[signal](signal, m_signalUserData[signal]);
pthread_kill(m_mainThread->m_thread, SIGWAKEUP); pthread_kill(m_mainThread->m_thread, SIGWAKEUP);
@ -580,7 +572,6 @@ ArchMultithreadPosix::raiseSignal(ESignal signal)
else if (signal == kINTERRUPT || signal == kTERMINATE) { else if (signal == kINTERRUPT || signal == kTERMINATE) {
ARCH->cancelThread(m_mainThread); ARCH->cancelThread(m_mainThread);
} }
unlockMutex(m_threadMutex);
} }
void void
@ -677,15 +668,15 @@ ArchMultithreadPosix::testCancelThreadImpl(ArchThreadImpl* thread)
{ {
assert(thread != NULL); assert(thread != NULL);
std::lock_guard<std::mutex> lock(m_threadMutex);
// update cancel state // update cancel state
lockMutex(m_threadMutex);
bool cancel = false; bool cancel = false;
if (thread->m_cancel && !thread->m_cancelling) { if (thread->m_cancel && !thread->m_cancelling) {
thread->m_cancelling = true; thread->m_cancelling = true;
thread->m_cancel = false; thread->m_cancel = false;
cancel = true; cancel = true;
} }
unlockMutex(m_threadMutex);
// unwind thread's stack if cancelling // unwind thread's stack if cancelling
if (cancel) { if (cancel) {
@ -717,8 +708,9 @@ ArchMultithreadPosix::doThreadFunc(ArchThread thread)
setPriorityOfThread(thread, 1); setPriorityOfThread(thread, 1);
// wait for parent to initialize this object // wait for parent to initialize this object
lockMutex(m_threadMutex); {
unlockMutex(m_threadMutex); std::lock_guard<std::mutex> lock(m_threadMutex);
}
void* result = NULL; void* result = NULL;
try { try {
@ -731,18 +723,20 @@ ArchMultithreadPosix::doThreadFunc(ArchThread thread)
} }
catch (...) { catch (...) {
// note -- don't catch (...) to avoid masking bugs // note -- don't catch (...) to avoid masking bugs
lockMutex(m_threadMutex); {
std::lock_guard<std::mutex> lock(m_threadMutex);
thread->m_exited = true; thread->m_exited = true;
unlockMutex(m_threadMutex); }
closeThread(thread); closeThread(thread);
throw; throw;
} }
// thread has exited // thread has exited
lockMutex(m_threadMutex); {
std::lock_guard<std::mutex> lock(m_threadMutex);
thread->m_result = result; thread->m_result = result;
thread->m_exited = true; thread->m_exited = true;
unlockMutex(m_threadMutex); }
// done with thread // done with thread
closeThread(thread); closeThread(thread);

View File

@ -22,6 +22,7 @@
#include "common/stdlist.h" #include "common/stdlist.h"
#include <pthread.h> #include <pthread.h>
#include <mutex>
#define ARCH_MULTITHREAD ArchMultithreadPosix #define ARCH_MULTITHREAD ArchMultithreadPosix
@ -104,7 +105,7 @@ private:
bool m_newThreadCalled; bool m_newThreadCalled;
ArchMutex m_threadMutex; std::mutex m_threadMutex;
ArchThread m_mainThread; ArchThread m_mainThread;
ThreadList m_threadList; ThreadList m_threadList;
ThreadID m_nextID; ThreadID m_nextID;

View File

@ -90,7 +90,6 @@ EventQueue::EventQueue() :
m_readyMutex(new Mutex), m_readyMutex(new Mutex),
m_readyCondVar(new CondVar<bool>(m_readyMutex, false)) m_readyCondVar(new CondVar<bool>(m_readyMutex, false))
{ {
m_mutex = ARCH->newMutex();
ARCH->setSignalHandler(Arch::kINTERRUPT, &interrupt, this); ARCH->setSignalHandler(Arch::kINTERRUPT, &interrupt, this);
ARCH->setSignalHandler(Arch::kTERMINATE, &interrupt, this); ARCH->setSignalHandler(Arch::kTERMINATE, &interrupt, this);
m_buffer = new SimpleEventQueueBuffer; m_buffer = new SimpleEventQueueBuffer;
@ -104,7 +103,6 @@ EventQueue::~EventQueue()
ARCH->setSignalHandler(Arch::kINTERRUPT, NULL, NULL); ARCH->setSignalHandler(Arch::kINTERRUPT, NULL, NULL);
ARCH->setSignalHandler(Arch::kTERMINATE, NULL, NULL); ARCH->setSignalHandler(Arch::kTERMINATE, NULL, NULL);
ARCH->closeMutex(m_mutex);
} }
void void
@ -136,7 +134,7 @@ EventQueue::loop()
Event::Type Event::Type
EventQueue::registerTypeOnce(Event::Type& type, const char* name) EventQueue::registerTypeOnce(Event::Type& type, const char* name)
{ {
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
if (type == Event::kUnknown) { if (type == Event::kUnknown) {
m_typeMap.insert(std::make_pair(m_nextType, name)); m_typeMap.insert(std::make_pair(m_nextType, name));
m_nameMap.insert(std::make_pair(name, m_nextType)); m_nameMap.insert(std::make_pair(name, m_nextType));
@ -176,7 +174,7 @@ EventQueue::getTypeName(Event::Type type)
void void
EventQueue::adoptBuffer(IEventQueueBuffer* buffer) EventQueue::adoptBuffer(IEventQueueBuffer* buffer)
{ {
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
LOG((CLOG_DEBUG "adopting new buffer")); LOG((CLOG_DEBUG "adopting new buffer"));
@ -261,7 +259,7 @@ retry:
case IEventQueueBuffer::kUser: case IEventQueueBuffer::kUser:
{ {
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
event = removeEvent(dataID); event = removeEvent(dataID);
return true; return true;
} }
@ -316,7 +314,7 @@ EventQueue::addEvent(const Event& event)
void void
EventQueue::addEventToBuffer(const Event& event) EventQueue::addEventToBuffer(const Event& event)
{ {
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
// store the event's data locally // store the event's data locally
UInt32 eventID = saveEvent(event); UInt32 eventID = saveEvent(event);
@ -338,7 +336,7 @@ EventQueue::newTimer(double duration, void* target)
if (target == NULL) { if (target == NULL) {
target = timer; target = timer;
} }
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
m_timers.insert(timer); m_timers.insert(timer);
// initial duration is requested duration plus whatever's on // initial duration is requested duration plus whatever's on
// the clock currently because the latter will be subtracted // the clock currently because the latter will be subtracted
@ -357,7 +355,7 @@ EventQueue::newOneShotTimer(double duration, void* target)
if (target == NULL) { if (target == NULL) {
target = timer; target = timer;
} }
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
m_timers.insert(timer); m_timers.insert(timer);
// initial duration is requested duration plus whatever's on // initial duration is requested duration plus whatever's on
// the clock currently because the latter will be subtracted // the clock currently because the latter will be subtracted
@ -370,7 +368,7 @@ EventQueue::newOneShotTimer(double duration, void* target)
void void
EventQueue::deleteTimer(EventQueueTimer* timer) EventQueue::deleteTimer(EventQueueTimer* timer)
{ {
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
for (TimerQueue::iterator index = m_timerQueue.begin(); for (TimerQueue::iterator index = m_timerQueue.begin();
index != m_timerQueue.end(); ++index) { index != m_timerQueue.end(); ++index) {
if (index->getTimer() == timer) { if (index->getTimer() == timer) {
@ -388,7 +386,7 @@ EventQueue::deleteTimer(EventQueueTimer* timer)
void void
EventQueue::adoptHandler(Event::Type type, void* target, IEventJob* handler) EventQueue::adoptHandler(Event::Type type, void* target, IEventJob* handler)
{ {
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
IEventJob*& job = m_handlers[target][type]; IEventJob*& job = m_handlers[target][type];
delete job; delete job;
job = handler; job = handler;
@ -399,7 +397,7 @@ EventQueue::removeHandler(Event::Type type, void* target)
{ {
IEventJob* handler = NULL; IEventJob* handler = NULL;
{ {
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
HandlerTable::iterator index = m_handlers.find(target); HandlerTable::iterator index = m_handlers.find(target);
if (index != m_handlers.end()) { if (index != m_handlers.end()) {
TypeHandlerTable& typeHandlers = index->second; TypeHandlerTable& typeHandlers = index->second;
@ -418,7 +416,7 @@ EventQueue::removeHandlers(void* target)
{ {
std::vector<IEventJob*> handlers; std::vector<IEventJob*> handlers;
{ {
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
HandlerTable::iterator index = m_handlers.find(target); HandlerTable::iterator index = m_handlers.find(target);
if (index != m_handlers.end()) { if (index != m_handlers.end()) {
// copy to handlers array and clear table for target // copy to handlers array and clear table for target
@ -447,7 +445,7 @@ EventQueue::isEmpty() const
IEventJob* IEventJob*
EventQueue::getHandler(Event::Type type, void* target) const EventQueue::getHandler(Event::Type type, void* target) const
{ {
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
HandlerTable::const_iterator index = m_handlers.find(target); HandlerTable::const_iterator index = m_handlers.find(target);
if (index != m_handlers.end()) { if (index != m_handlers.end()) {
const TypeHandlerTable& typeHandlers = index->second; const TypeHandlerTable& typeHandlers = index->second;

View File

@ -28,10 +28,9 @@
#include "common/stdset.h" #include "common/stdset.h"
#include "base/NonBlockingStream.h" #include "base/NonBlockingStream.h"
#include <mutex>
#include <queue> #include <queue>
class Mutex;
//! Event queue //! Event queue
/*! /*!
An event queue that implements the platform independent parts and An event queue that implements the platform independent parts and
@ -114,7 +113,7 @@ private:
typedef std::map<void*, TypeHandlerTable> HandlerTable; typedef std::map<void*, TypeHandlerTable> HandlerTable;
int m_systemTarget; int m_systemTarget;
ArchMutex m_mutex; mutable std::mutex m_mutex;
// registered events // registered events
Event::Type m_nextType; Event::Type m_nextType;

View File

@ -63,9 +63,6 @@ Log::Log()
{ {
assert(s_log == NULL); assert(s_log == NULL);
// create mutex for multithread safe operation
m_mutex = ARCH->newMutex();
// other initalization // other initalization
m_maxPriority = g_defaultMaxPriority; m_maxPriority = g_defaultMaxPriority;
m_maxNewlineLength = 0; m_maxNewlineLength = 0;
@ -90,7 +87,6 @@ Log::~Log()
index != m_alwaysOutputters.end(); ++index) { index != m_alwaysOutputters.end(); ++index) {
delete *index; delete *index;
} }
ARCH->closeMutex(m_mutex);
} }
Log* Log*
@ -214,7 +210,7 @@ Log::insert(ILogOutputter* outputter, bool alwaysAtHead)
{ {
assert(outputter != NULL); assert(outputter != NULL);
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
if (alwaysAtHead) { if (alwaysAtHead) {
m_alwaysOutputters.push_front(outputter); m_alwaysOutputters.push_front(outputter);
} }
@ -237,7 +233,7 @@ Log::insert(ILogOutputter* outputter, bool alwaysAtHead)
void void
Log::remove(ILogOutputter* outputter) Log::remove(ILogOutputter* outputter)
{ {
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
m_outputters.remove(outputter); m_outputters.remove(outputter);
m_alwaysOutputters.remove(outputter); m_alwaysOutputters.remove(outputter);
} }
@ -245,7 +241,7 @@ Log::remove(ILogOutputter* outputter)
void void
Log::pop_front(bool alwaysAtHead) Log::pop_front(bool alwaysAtHead)
{ {
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
OutputterList* list = alwaysAtHead ? &m_alwaysOutputters : &m_outputters; OutputterList* list = alwaysAtHead ? &m_alwaysOutputters : &m_outputters;
if (!list->empty()) { if (!list->empty()) {
delete list->front(); delete list->front();
@ -271,14 +267,14 @@ Log::setFilter(const char* maxPriority)
void void
Log::setFilter(int maxPriority) Log::setFilter(int maxPriority)
{ {
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
m_maxPriority = maxPriority; m_maxPriority = maxPriority;
} }
int int
Log::getFilter() const Log::getFilter() const
{ {
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
return m_maxPriority; return m_maxPriority;
} }
@ -289,7 +285,7 @@ Log::output(ELevel priority, char* msg)
assert(msg != NULL); assert(msg != NULL);
if (!msg) return; if (!msg) return;
ArchMutexLock lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
OutputterList::const_iterator i; OutputterList::const_iterator i;

View File

@ -24,6 +24,7 @@
#include "common/stdlist.h" #include "common/stdlist.h"
#include <stdarg.h> #include <stdarg.h>
#include <mutex>
#define CLOG (Log::getInstance()) #define CLOG (Log::getInstance())
#define BYE "\nTry `%s --help' for more information." #define BYE "\nTry `%s --help' for more information."
@ -132,7 +133,7 @@ private:
static Log* s_log; static Log* s_log;
ArchMutex m_mutex; mutable std::mutex m_mutex;
OutputterList m_outputters; OutputterList m_outputters;
OutputterList m_alwaysOutputters; OutputterList m_alwaysOutputters;
int m_maxNewlineLength; int m_maxNewlineLength;

View File

@ -34,8 +34,6 @@ IpcClientProxy::IpcClientProxy(barrier::IStream& stream, IEventQueue* events) :
m_stream(stream), m_stream(stream),
m_clientType(kIpcClientUnknown), m_clientType(kIpcClientUnknown),
m_disconnecting(false), m_disconnecting(false),
m_readMutex(ARCH->newMutex()),
m_writeMutex(ARCH->newMutex()),
m_events(events) m_events(events)
{ {
m_events->adoptHandler( m_events->adoptHandler(
@ -71,14 +69,11 @@ IpcClientProxy::~IpcClientProxy()
m_events->forIStream().outputShutdown(), m_stream.getEventTarget()); m_events->forIStream().outputShutdown(), m_stream.getEventTarget());
// don't delete the stream while it's being used. // don't delete the stream while it's being used.
ARCH->lockMutex(m_readMutex); {
ARCH->lockMutex(m_writeMutex); std::lock_guard<std::mutex> lock_read(m_readMutex);
std::lock_guard<std::mutex> lock_write(m_writeMutex);
delete &m_stream; delete &m_stream;
ARCH->unlockMutex(m_readMutex); }
ARCH->unlockMutex(m_writeMutex);
ARCH->closeMutex(m_readMutex);
ARCH->closeMutex(m_writeMutex);
} }
void void
@ -99,7 +94,7 @@ void
IpcClientProxy::handleData(const Event&, void*) IpcClientProxy::handleData(const Event&, void*)
{ {
// don't allow the dtor to destroy the stream while we're using it. // don't allow the dtor to destroy the stream while we're using it.
ArchMutexLock lock(m_readMutex); std::lock_guard<std::mutex> lock(m_readMutex);
LOG((CLOG_DEBUG "start ipc handle data")); 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 // don't allow other threads to write until we've finished the entire
// message. stream write is locked, but only for that single write. // 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. // also, don't allow the dtor to destroy the stream while we're using it.
ArchMutexLock lock(m_writeMutex); std::lock_guard<std::mutex> lock(m_writeMutex);
LOG((CLOG_DEBUG4 "ipc write: %d", message.type())); LOG((CLOG_DEBUG4 "ipc write: %d", message.type()));

View File

@ -23,6 +23,8 @@
#include "base/EventTypes.h" #include "base/EventTypes.h"
#include "base/Event.h" #include "base/Event.h"
#include <mutex>
namespace barrier { class IStream; } namespace barrier { class IStream; }
class IpcMessage; class IpcMessage;
class IpcCommandMessage; class IpcCommandMessage;
@ -49,7 +51,7 @@ private:
barrier::IStream& m_stream; barrier::IStream& m_stream;
EIpcClientType m_clientType; EIpcClientType m_clientType;
bool m_disconnecting; bool m_disconnecting;
ArchMutex m_readMutex; std::mutex m_readMutex;
ArchMutex m_writeMutex; std::mutex m_writeMutex;
IEventQueue* m_events; IEventQueue* m_events;
}; };

View File

@ -39,7 +39,6 @@ enum EIpcLogOutputter {
IpcLogOutputter::IpcLogOutputter(IpcServer& ipcServer, EIpcClientType clientType, bool useThread) : IpcLogOutputter::IpcLogOutputter(IpcServer& ipcServer, EIpcClientType clientType, bool useThread) :
m_ipcServer(ipcServer), m_ipcServer(ipcServer),
m_bufferMutex(ARCH->newMutex()),
m_sending(false), m_sending(false),
m_bufferThread(nullptr), m_bufferThread(nullptr),
m_running(false), m_running(false),
@ -52,8 +51,7 @@ IpcLogOutputter::IpcLogOutputter(IpcServer& ipcServer, EIpcClientType clientType
m_bufferRateTimeLimit(kBufferRateTimeLimit), m_bufferRateTimeLimit(kBufferRateTimeLimit),
m_bufferWriteCount(0), m_bufferWriteCount(0),
m_bufferRateStart(ARCH->time()), m_bufferRateStart(ARCH->time()),
m_clientType(clientType), m_clientType(clientType)
m_runningMutex(ARCH->newMutex())
{ {
if (useThread) { if (useThread) {
m_bufferThread = new Thread(new TMethodJob<IpcLogOutputter>( m_bufferThread = new Thread(new TMethodJob<IpcLogOutputter>(
@ -65,8 +63,6 @@ IpcLogOutputter::~IpcLogOutputter()
{ {
close(); close();
ARCH->closeMutex(m_bufferMutex);
if (m_bufferThread != nullptr) { if (m_bufferThread != nullptr) {
m_bufferThread->cancel(); m_bufferThread->cancel();
m_bufferThread->wait(); m_bufferThread->wait();
@ -86,7 +82,7 @@ void
IpcLogOutputter::close() IpcLogOutputter::close()
{ {
if (m_bufferThread != nullptr) { if (m_bufferThread != nullptr) {
ArchMutexLock lock(m_runningMutex); std::lock_guard<std::mutex> lock(m_runningMutex);
m_running = false; m_running = false;
notifyBuffer(); notifyBuffer();
m_bufferThread->wait(5); m_bufferThread->wait(5);
@ -116,7 +112,7 @@ IpcLogOutputter::write(ELevel, const char* text)
void void
IpcLogOutputter::appendBuffer(const String& text) IpcLogOutputter::appendBuffer(const String& text)
{ {
ArchMutexLock lock(m_bufferMutex); std::lock_guard<std::mutex> lock(m_bufferMutex);
double elapsed = ARCH->time() - m_bufferRateStart; double elapsed = ARCH->time() - m_bufferRateStart;
if (elapsed < m_bufferRateTimeLimit) { if (elapsed < m_bufferRateTimeLimit) {
@ -143,7 +139,7 @@ IpcLogOutputter::appendBuffer(const String& text)
bool bool
IpcLogOutputter::isRunning() IpcLogOutputter::isRunning()
{ {
ArchMutexLock lock(m_runningMutex); std::lock_guard<std::mutex> lock(m_runningMutex);
return m_running; return m_running;
} }
@ -180,7 +176,7 @@ IpcLogOutputter::notifyBuffer()
String String
IpcLogOutputter::getChunk(size_t count) IpcLogOutputter::getChunk(size_t count)
{ {
ArchMutexLock lock(m_bufferMutex); std::lock_guard<std::mutex> lock(m_bufferMutex);
if (m_buffer.size() < count) { if (m_buffer.size() < count) {
count = m_buffer.size(); count = m_buffer.size();

View File

@ -24,6 +24,7 @@
#include "ipc/Ipc.h" #include "ipc/Ipc.h"
#include <deque> #include <deque>
#include <mutex>
class IpcServer; class IpcServer;
class Event; class Event;
@ -100,7 +101,7 @@ private:
IpcServer& m_ipcServer; IpcServer& m_ipcServer;
Buffer m_buffer; Buffer m_buffer;
ArchMutex m_bufferMutex; std::mutex m_bufferMutex;
bool m_sending; bool m_sending;
Thread* m_bufferThread; Thread* m_bufferThread;
bool m_running; bool m_running;
@ -115,5 +116,5 @@ private:
UInt16 m_bufferWriteCount; UInt16 m_bufferWriteCount;
double m_bufferRateStart; double m_bufferRateStart;
EIpcClientType m_clientType; EIpcClientType m_clientType;
ArchMutex m_runningMutex; std::mutex m_runningMutex;
}; };

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: