refactor: use std::scoped_lock inplace of std::lock_guard

This commit is contained in:
sithlord48
2025-06-15 20:15:07 -04:00
committed by Nick Bolton
parent 5fd4d93f7f
commit c15214aee7
16 changed files with 92 additions and 92 deletions

View File

@ -20,7 +20,7 @@ std::mutex s_mutex;
int ArchString::convStringWCToMB(char *dst, const wchar_t *src, uint32_t n, bool *errors) const int ArchString::convStringWCToMB(char *dst, const wchar_t *src, uint32_t n, bool *errors) const
{ {
std::lock_guard<std::mutex> lock(s_mutex); std::scoped_lock lock{s_mutex};
ptrdiff_t len = 0; ptrdiff_t len = 0;
bool dummyErrors; bool dummyErrors;
@ -77,7 +77,7 @@ ArchString::EWideCharEncoding ArchString::getWideCharEncoding() const
int ArchString::convStringMBToWC(wchar_t *dst, const char *src, uint32_t n, bool *errors) const int ArchString::convStringMBToWC(wchar_t *dst, const char *src, uint32_t n, bool *errors) const
{ {
std::lock_guard<std::mutex> lock(s_mutex); std::scoped_lock lock{s_mutex};
ptrdiff_t len = 0; ptrdiff_t len = 0;
wchar_t dummy; wchar_t dummy;

View File

@ -106,14 +106,14 @@ ArchMultithreadPosix::~ArchMultithreadPosix()
void ArchMultithreadPosix::setNetworkDataForCurrentThread(void *data) void ArchMultithreadPosix::setNetworkDataForCurrentThread(void *data)
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
ArchThreadImpl *thread = find(pthread_self()); ArchThreadImpl *thread = find(pthread_self());
thread->m_networkData = data; thread->m_networkData = data;
} }
void *ArchMultithreadPosix::getNetworkDataForThread(ArchThread thread) void *ArchMultithreadPosix::getNetworkDataForThread(ArchThread thread)
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
return thread->m_networkData; return thread->m_networkData;
} }
@ -285,7 +285,7 @@ ArchThread ArchMultithreadPosix::newThread(ThreadFunc func, void *data)
} }
// note that the child thread will wait until we release this mutex // note that the child thread will wait until we release this mutex
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
// create thread impl for new thread // create thread impl for new thread
auto *thread = new ArchThreadImpl; auto *thread = new ArchThreadImpl;
@ -319,7 +319,7 @@ ArchThread ArchMultithreadPosix::newThread(ThreadFunc func, void *data)
ArchThread ArchMultithreadPosix::newCurrentThread() ArchThread ArchMultithreadPosix::newCurrentThread()
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
ArchThreadImpl *thread = find(pthread_self()); ArchThreadImpl *thread = find(pthread_self());
assert(thread != nullptr); assert(thread != nullptr);
return thread; return thread;
@ -338,7 +338,7 @@ void ArchMultithreadPosix::closeThread(ArchThread thread)
// remove thread from list // remove thread from list
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
assert(findNoRef(thread->m_thread) == thread); assert(findNoRef(thread->m_thread) == thread);
erase(thread); erase(thread);
} }
@ -362,7 +362,7 @@ void ArchMultithreadPosix::cancelThread(ArchThread thread)
bool wakeup = false; bool wakeup = false;
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock 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;
@ -387,7 +387,7 @@ void ArchMultithreadPosix::testCancelThread()
// find current thread // find current thread
ArchThreadImpl *thread = nullptr; ArchThreadImpl *thread = nullptr;
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
thread = findNoRef(pthread_self()); thread = findNoRef(pthread_self());
} }
// test cancel on thread // test cancel on thread
@ -400,7 +400,7 @@ bool ArchMultithreadPosix::wait(ArchThread target, double timeout)
ArchThreadImpl *self = nullptr; ArchThreadImpl *self = nullptr;
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
// find current thread // find current thread
self = findNoRef(pthread_self()); self = findNoRef(pthread_self());
// ignore wait if trying to wait on ourself // ignore wait if trying to wait on ourself
@ -452,13 +452,13 @@ bool ArchMultithreadPosix::isSameThread(ArchThread thread1, ArchThread thread2)
bool ArchMultithreadPosix::isExitedThread(ArchThread thread) bool ArchMultithreadPosix::isExitedThread(ArchThread thread)
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
return thread->m_exited; return thread->m_exited;
} }
void *ArchMultithreadPosix::getResultOfThread(ArchThread thread) void *ArchMultithreadPosix::getResultOfThread(ArchThread thread)
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
return thread->m_result; return thread->m_result;
} }
@ -469,14 +469,14 @@ IArchMultithread::ThreadID ArchMultithreadPosix::getIDOfThread(ArchThread thread
void ArchMultithreadPosix::setSignalHandler(ESignal signal, SignalFunc func, void *userData) void ArchMultithreadPosix::setSignalHandler(ESignal signal, SignalFunc func, void *userData)
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
m_signalFunc[signal] = func; m_signalFunc[signal] = func;
m_signalUserData[signal] = userData; m_signalUserData[signal] = userData;
} }
void ArchMultithreadPosix::raiseSignal(ESignal signal) void ArchMultithreadPosix::raiseSignal(ESignal signal)
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
if (m_signalFunc[signal] != nullptr) { if (m_signalFunc[signal] != nullptr) {
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);
@ -570,7 +570,7 @@ void ArchMultithreadPosix::testCancelThreadImpl(ArchThreadImpl *thread)
assert(thread != nullptr); assert(thread != nullptr);
// update cancel state // update cancel state
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{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;
@ -607,7 +607,7 @@ void ArchMultithreadPosix::doThreadFunc(ArchThread thread)
// wait for parent to initialize this object // wait for parent to initialize this object
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
} }
void *result = nullptr; void *result = nullptr;
@ -623,7 +623,7 @@ void ArchMultithreadPosix::doThreadFunc(ArchThread thread)
} catch (...) { } catch (...) {
// note -- don't catch (...) to avoid masking bugs // note -- don't catch (...) to avoid masking bugs
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
thread->m_exited = true; thread->m_exited = true;
} }
closeThread(thread); closeThread(thread);
@ -632,7 +632,7 @@ void ArchMultithreadPosix::doThreadFunc(ArchThread thread)
// thread has exited // thread has exited
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
thread->m_result = result; thread->m_result = result;
thread->m_exited = true; thread->m_exited = true;
} }

View File

@ -124,7 +124,7 @@ ArchSocket ArchNetworkBSD::copySocket(ArchSocket s)
assert(s != nullptr); assert(s != nullptr);
// ref the socket and return it // ref the socket and return it
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
++s->m_refCount; ++s->m_refCount;
return s; return s;
} }
@ -136,7 +136,7 @@ void ArchNetworkBSD::closeSocket(ArchSocket s)
bool doClose = false; bool doClose = false;
// unref the socket and note if it should be released // unref the socket and note if it should be released
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
doClose = (--s->m_refCount == 0); doClose = (--s->m_refCount == 0);
} }
@ -146,7 +146,7 @@ void ArchNetworkBSD::closeSocket(ArchSocket s)
// close failed. restore the last ref and throw. // close failed. restore the last ref and throw.
int err = errno; int err = errno;
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
++s->m_refCount; ++s->m_refCount;
} }
throwError(err); throwError(err);
@ -531,7 +531,7 @@ std::vector<ArchNetAddress> ArchNetworkBSD::nameToAddr(const std::string &name)
} }
// done with static buffer // done with static buffer
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
struct addrinfo *pResult = nullptr; struct addrinfo *pResult = nullptr;
if (int ret = getaddrinfo(name.c_str(), nullptr, &hints, &pResult); ret != 0) { if (int ret = getaddrinfo(name.c_str(), nullptr, &hints, &pResult); ret != 0) {
@ -568,7 +568,7 @@ std::string ArchNetworkBSD::addrToName(ArchNetAddress addr)
assert(addr != nullptr); assert(addr != nullptr);
// mutexed name lookup (ugh) // mutexed name lookup (ugh)
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
char host[1024]; char host[1024];
char service[20]; char service[20];
@ -591,7 +591,7 @@ std::string ArchNetworkBSD::addrToString(ArchNetAddress addr)
switch (getAddrFamily(addr)) { switch (getAddrFamily(addr)) {
case kINET: { case kINET: {
const auto *ipAddr = TYPED_ADDR(struct sockaddr_in, addr); const auto *ipAddr = TYPED_ADDR(struct sockaddr_in, addr);
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
std::string s = inet_ntoa(ipAddr->sin_addr); std::string s = inet_ntoa(ipAddr->sin_addr);
return s; return s;
} }
@ -600,7 +600,7 @@ std::string ArchNetworkBSD::addrToString(ArchNetAddress addr)
char strAddr[INET6_ADDRSTRLEN]; char strAddr[INET6_ADDRSTRLEN];
const auto *ipAddr = TYPED_ADDR(struct sockaddr_in6, addr); const auto *ipAddr = TYPED_ADDR(struct sockaddr_in6, addr);
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
inet_ntop(AF_INET6, &ipAddr->sin6_addr, strAddr, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &ipAddr->sin6_addr, strAddr, INET6_ADDRSTRLEN);
} }
return strAddr; return strAddr;

View File

@ -85,7 +85,7 @@ ArchMultithreadWindows::ArchMultithreadWindows()
m_signalUserData[i] = nullptr; m_signalUserData[i] = nullptr;
} }
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
// 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.
@ -107,20 +107,20 @@ ArchMultithreadWindows::~ArchMultithreadWindows()
void ArchMultithreadWindows::setNetworkDataForCurrentThread(void *data) void ArchMultithreadWindows::setNetworkDataForCurrentThread(void *data)
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
ArchThreadImpl *thread = findNoRef(GetCurrentThreadId()); ArchThreadImpl *thread = findNoRef(GetCurrentThreadId());
thread->m_networkData = data; thread->m_networkData = data;
} }
void *ArchMultithreadWindows::getNetworkDataForThread(ArchThread thread) void *ArchMultithreadWindows::getNetworkDataForThread(ArchThread thread)
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
return thread->m_networkData; return thread->m_networkData;
} }
HANDLE ArchMultithreadWindows::getCancelEventForCurrentThread() HANDLE ArchMultithreadWindows::getCancelEventForCurrentThread()
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
ArchThreadImpl *thread = findNoRef(GetCurrentThreadId()); ArchThreadImpl *thread = findNoRef(GetCurrentThreadId());
return thread->m_cancel; return thread->m_cancel;
} }
@ -255,7 +255,7 @@ void ArchMultithreadWindows::unlockMutex(ArchMutex mutex)
ArchThread ArchMultithreadWindows::newThread(ThreadFunc func, void *data) ArchThread ArchMultithreadWindows::newThread(ThreadFunc func, void *data)
{ {
// note that the child thread will wait until we release this mutex // note that the child thread will wait until we release this mutex
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
// create thread impl for new thread // create thread impl for new thread
ArchThreadImpl *thread = new ArchThreadImpl; ArchThreadImpl *thread = new ArchThreadImpl;
@ -285,7 +285,7 @@ ArchThread ArchMultithreadWindows::newThread(ThreadFunc func, void *data)
ArchThread ArchMultithreadWindows::newCurrentThread() ArchThread ArchMultithreadWindows::newCurrentThread()
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
ArchThreadImpl *thread = find(GetCurrentThreadId()); ArchThreadImpl *thread = find(GetCurrentThreadId());
assert(thread != nullptr); assert(thread != nullptr);
return thread; return thread;
@ -304,7 +304,7 @@ void ArchMultithreadWindows::closeThread(ArchThread thread)
// remove thread from list // remove thread from list
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
assert(findNoRefOrCreate(thread->m_id) == thread); assert(findNoRefOrCreate(thread->m_id) == thread);
erase(thread); erase(thread);
} }
@ -389,7 +389,7 @@ void ArchMultithreadWindows::setPriorityOfThread(ArchThread thread, int n)
void ArchMultithreadWindows::testCancelThread() void ArchMultithreadWindows::testCancelThread()
{ {
// find current thread // find current thread
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
ArchThreadImpl *thread = findNoRef(GetCurrentThreadId()); ArchThreadImpl *thread = findNoRef(GetCurrentThreadId());
// test cancel on thread // test cancel on thread
@ -402,7 +402,7 @@ bool ArchMultithreadWindows::wait(ArchThread target, double timeout)
ArchThreadImpl *self = nullptr; ArchThreadImpl *self = nullptr;
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
// find current thread // find current thread
self = findNoRef(GetCurrentThreadId()); self = findNoRef(GetCurrentThreadId());
// ignore wait if trying to wait on ourself // ignore wait if trying to wait on ourself
@ -465,7 +465,7 @@ bool ArchMultithreadWindows::isExitedThread(ArchThread thread)
void *ArchMultithreadWindows::getResultOfThread(ArchThread thread) void *ArchMultithreadWindows::getResultOfThread(ArchThread thread)
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
return thread->m_result; return thread->m_result;
} }
@ -476,14 +476,14 @@ IArchMultithread::ThreadID ArchMultithreadWindows::getIDOfThread(ArchThread thre
void ArchMultithreadWindows::setSignalHandler(ESignal signal, SignalFunc func, void *userData) void ArchMultithreadWindows::setSignalHandler(ESignal signal, SignalFunc func, void *userData)
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
m_signalFunc[signal] = func; m_signalFunc[signal] = func;
m_signalUserData[signal] = userData; m_signalUserData[signal] = userData;
} }
void ArchMultithreadWindows::raiseSignal(ESignal signal) void ArchMultithreadWindows::raiseSignal(ESignal signal)
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
if (m_signalFunc[signal] != nullptr) { if (m_signalFunc[signal] != nullptr) {
m_signalFunc[signal](signal, m_signalUserData[signal]); m_signalFunc[signal](signal, m_signalUserData[signal]);
ARCH->unblockPollSocket(m_mainThread); ARCH->unblockPollSocket(m_mainThread);
@ -567,7 +567,7 @@ void ArchMultithreadWindows::testCancelThreadImpl(ArchThreadImpl *thread)
} }
// update cancel state // update cancel state
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
bool cancel = !thread->m_cancelling; bool cancel = !thread->m_cancelling;
thread->m_cancelling = true; thread->m_cancelling = true;
ResetEvent(thread->m_cancel); ResetEvent(thread->m_cancel);
@ -594,7 +594,7 @@ void ArchMultithreadWindows::doThreadFunc(ArchThread thread)
{ {
// wait for parent to initialize this object // wait for parent to initialize this object
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
} }
void *result = nullptr; void *result = nullptr;
@ -608,7 +608,7 @@ void ArchMultithreadWindows::doThreadFunc(ArchThread thread)
} catch (...) { } catch (...) {
// note -- don't catch (...) to avoid masking bugs // note -- don't catch (...) to avoid masking bugs
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
SetEvent(thread->m_exit); SetEvent(thread->m_exit);
} }
closeThread(thread); closeThread(thread);
@ -617,7 +617,7 @@ void ArchMultithreadWindows::doThreadFunc(ArchThread thread)
// thread has exited // thread has exited
{ {
std::lock_guard<std::mutex> lock(m_threadMutex); std::scoped_lock lock{m_threadMutex};
thread->m_result = result; thread->m_result = result;
SetEvent(thread->m_exit); SetEvent(thread->m_exit);
} }

View File

@ -225,7 +225,7 @@ ArchSocket ArchNetworkWinsock::copySocket(ArchSocket s)
assert(s != nullptr); assert(s != nullptr);
// ref the socket and return it // ref the socket and return it
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
++s->m_refCount; ++s->m_refCount;
return s; return s;
} }
@ -237,7 +237,7 @@ void ArchNetworkWinsock::closeSocket(ArchSocket s)
// unref the socket and note if it should be released // unref the socket and note if it should be released
bool doClose = false; bool doClose = false;
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
doClose = (--s->m_refCount == 0); doClose = (--s->m_refCount == 0);
} }
@ -247,7 +247,7 @@ void ArchNetworkWinsock::closeSocket(ArchSocket s)
// close failed. restore the last ref and throw. // close failed. restore the last ref and throw.
int err = getsockerror_winsock(); int err = getsockerror_winsock();
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
++s->m_refCount; ++s->m_refCount;
} }
throwError(err); throwError(err);
@ -684,7 +684,7 @@ 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;
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
if ((ret = getaddrinfo(name.c_str(), nullptr, &hints, &pResult)) != 0) { if ((ret = getaddrinfo(name.c_str(), nullptr, &hints, &pResult)) != 0) {
throwNameError(ret); throwNameError(ret);
} }

View File

@ -70,7 +70,7 @@ void EventQueue::loop()
void EventQueue::adoptBuffer(IEventQueueBuffer *buffer) void EventQueue::adoptBuffer(IEventQueueBuffer *buffer)
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
LOG((CLOG_DEBUG "adopting new buffer")); LOG((CLOG_DEBUG "adopting new buffer"));
@ -139,7 +139,7 @@ bool EventQueue::processEvent(Event &event, double timeout, Stopwatch &timer)
return true; return true;
case IEventQueueBuffer::kUser: { case IEventQueueBuffer::kUser: {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
event = removeEvent(dataID); event = removeEvent(dataID);
return true; return true;
} }
@ -195,7 +195,7 @@ void EventQueue::addEvent(const Event &event)
void EventQueue::addEventToBuffer(const Event &event) void EventQueue::addEventToBuffer(const Event &event)
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
// store the event's data locally // store the event's data locally
auto eventID = saveEvent(event); auto eventID = saveEvent(event);
@ -216,7 +216,7 @@ EventQueueTimer *EventQueue::newTimer(double duration, void *target)
if (target == nullptr) { if (target == nullptr) {
target = timer; target = timer;
} }
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock 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
@ -233,7 +233,7 @@ EventQueueTimer *EventQueue::newOneShotTimer(double duration, void *target)
if (target == nullptr) { if (target == nullptr) {
target = timer; target = timer;
} }
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock 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
@ -244,7 +244,7 @@ EventQueueTimer *EventQueue::newOneShotTimer(double duration, void *target)
void EventQueue::deleteTimer(EventQueueTimer *timer) void EventQueue::deleteTimer(EventQueueTimer *timer)
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
for (auto index = m_timerQueue.begin(); index != m_timerQueue.end(); ++index) { for (auto index = m_timerQueue.begin(); index != m_timerQueue.end(); ++index) {
if (index->getTimer() == timer) { if (index->getTimer() == timer) {
m_timerQueue.erase(index); m_timerQueue.erase(index);
@ -259,7 +259,7 @@ void EventQueue::deleteTimer(EventQueueTimer *timer)
void EventQueue::adoptHandler(EventTypes type, void *target, IEventJob *handler) void EventQueue::adoptHandler(EventTypes type, void *target, IEventJob *handler)
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
m_handlers[target][type].reset(handler); m_handlers[target][type].reset(handler);
} }
@ -267,7 +267,7 @@ void EventQueue::removeHandler(EventTypes type, void *target)
{ {
std::unique_ptr<IEventJob> handler; std::unique_ptr<IEventJob> handler;
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock 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;
@ -285,7 +285,7 @@ void EventQueue::removeHandlers(void *target)
{ {
std::vector<std::unique_ptr<IEventJob>> handlers; std::vector<std::unique_ptr<IEventJob>> handlers;
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock 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
@ -306,7 +306,7 @@ bool EventQueue::isEmpty() const
IEventJob *EventQueue::getHandler(EventTypes type, void *target) const IEventJob *EventQueue::getHandler(EventTypes type, void *target) const
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
if (HandlerTable::const_iterator index = m_handlers.find(target); index != m_handlers.end()) { if (HandlerTable::const_iterator index = m_handlers.find(target); index != m_handlers.end()) {
const TypeHandlerTable &typeHandlers = index->second; const TypeHandlerTable &typeHandlers = index->second;
TypeHandlerTable::const_iterator index2 = typeHandlers.find(type); TypeHandlerTable::const_iterator index2 = typeHandlers.find(type);

View File

@ -208,7 +208,7 @@ void Log::insert(ILogOutputter *adoptedOutputter, bool alwaysAtHead)
{ {
assert(adoptedOutputter != nullptr); assert(adoptedOutputter != nullptr);
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
if (alwaysAtHead) { if (alwaysAtHead) {
m_alwaysOutputters.push_front(adoptedOutputter); m_alwaysOutputters.push_front(adoptedOutputter);
} else { } else {
@ -220,14 +220,14 @@ void Log::insert(ILogOutputter *adoptedOutputter, bool alwaysAtHead)
void Log::remove(ILogOutputter *outputter) void Log::remove(ILogOutputter *outputter)
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
m_outputters.remove(outputter); m_outputters.remove(outputter);
m_alwaysOutputters.remove(outputter); m_alwaysOutputters.remove(outputter);
} }
void Log::pop_front(bool alwaysAtHead) void Log::pop_front(bool alwaysAtHead)
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock 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();
@ -251,13 +251,13 @@ bool Log::setFilter(const char *maxPriority)
void Log::setFilter(int maxPriority) void Log::setFilter(int maxPriority)
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
m_maxPriority = maxPriority; m_maxPriority = maxPriority;
} }
int Log::getFilter() const int Log::getFilter() const
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
return m_maxPriority; return m_maxPriority;
} }
@ -268,7 +268,7 @@ void Log::output(ELevel priority, const char *msg)
if (!msg) if (!msg)
return; return;
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
OutputterList::const_iterator i; OutputterList::const_iterator i;

View File

@ -26,7 +26,7 @@ PacketStreamFilter::PacketStreamFilter(IEventQueue *events, deskflow::IStream *s
void PacketStreamFilter::close() void PacketStreamFilter::close()
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
m_size = 0; m_size = 0;
m_buffer.pop(m_buffer.getSize()); m_buffer.pop(m_buffer.getSize());
StreamFilter::close(); StreamFilter::close();
@ -38,7 +38,7 @@ uint32_t PacketStreamFilter::read(void *buffer, uint32_t n)
return 0; return 0;
} }
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
// if not enough data yet then give up // if not enough data yet then give up
if (!isReadyNoLock()) { if (!isReadyNoLock()) {
@ -84,7 +84,7 @@ void PacketStreamFilter::write(const void *buffer, uint32_t count)
void PacketStreamFilter::shutdownInput() void PacketStreamFilter::shutdownInput()
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
m_size = 0; m_size = 0;
m_buffer.pop(m_buffer.getSize()); m_buffer.pop(m_buffer.getSize());
StreamFilter::shutdownInput(); StreamFilter::shutdownInput();
@ -92,13 +92,13 @@ void PacketStreamFilter::shutdownInput()
bool PacketStreamFilter::isReady() const bool PacketStreamFilter::isReady() const
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
return isReadyNoLock(); return isReadyNoLock();
} }
uint32_t PacketStreamFilter::getSize() const uint32_t PacketStreamFilter::getSize() const
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
return isReadyNoLock() ? m_size : 0; return isReadyNoLock() ? m_size : 0;
} }
@ -157,13 +157,13 @@ bool PacketStreamFilter::readMore()
void PacketStreamFilter::filterEvent(const Event &event) void PacketStreamFilter::filterEvent(const Event &event)
{ {
if (event.getType() == EventTypes::StreamInputReady) { if (event.getType() == EventTypes::StreamInputReady) {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
if (!readMore()) { if (!readMore()) {
return; return;
} }
} else if (event.getType() == EventTypes::StreamInputShutdown) { } else if (event.getType() == EventTypes::StreamInputShutdown) {
// discard this if we have buffered data // discard this if we have buffered data
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
m_inputShutdown = true; m_inputShutdown = true;
if (m_size != 0) { if (m_size != 0) {
return; return;

View File

@ -197,7 +197,7 @@ void AppUtilWindows::eventLoop()
LOG_DEBUG("windows event loop running"); LOG_DEBUG("windows event loop running");
{ {
std::lock_guard lock(m_eventThreadStartedMutex); std::scoped_lock lock{m_eventThreadStartedMutex};
m_eventThreadRunning = true; m_eventThreadRunning = true;
} }
m_eventThreadStartedCond.notify_one(); m_eventThreadStartedCond.notify_one();

View File

@ -230,7 +230,7 @@ TCPSocket::EJobResult SecureSocket::doWrite()
int SecureSocket::secureRead(void *buffer, int size, int &read) int SecureSocket::secureRead(void *buffer, int size, int &read)
{ {
std::lock_guard ssl_lock{ssl_mutex_}; std::scoped_lock ssl_lock{ssl_mutex_};
if (m_ssl->m_ssl != nullptr) { if (m_ssl->m_ssl != nullptr) {
LOG((CLOG_DEBUG2 "reading secure socket")); LOG((CLOG_DEBUG2 "reading secure socket"));
@ -257,7 +257,7 @@ int SecureSocket::secureRead(void *buffer, int size, int &read)
int SecureSocket::secureWrite(const void *buffer, int size, int &wrote) int SecureSocket::secureWrite(const void *buffer, int size, int &wrote)
{ {
std::lock_guard ssl_lock{ssl_mutex_}; std::scoped_lock ssl_lock{ssl_mutex_};
if (m_ssl->m_ssl != nullptr) { if (m_ssl->m_ssl != nullptr) {
LOG((CLOG_DEBUG2 "writing secure socket: %p", this)); LOG((CLOG_DEBUG2 "writing secure socket: %p", this));
@ -290,7 +290,7 @@ bool SecureSocket::isSecureReady() const
void SecureSocket::initSsl(bool server) void SecureSocket::initSsl(bool server)
{ {
std::lock_guard ssl_lock{ssl_mutex_}; std::scoped_lock ssl_lock{ssl_mutex_};
m_ssl = new Ssl(); m_ssl = new Ssl();
m_ssl->m_context = nullptr; m_ssl->m_context = nullptr;
@ -301,7 +301,7 @@ void SecureSocket::initSsl(bool server)
bool SecureSocket::loadCertificates(const std::string &filename) bool SecureSocket::loadCertificates(const std::string &filename)
{ {
std::lock_guard ssl_lock{ssl_mutex_}; std::scoped_lock ssl_lock{ssl_mutex_};
if (filename.empty()) { if (filename.empty()) {
SslLogger::logError("tls certificate is not specified"); SslLogger::logError("tls certificate is not specified");
@ -392,7 +392,7 @@ void SecureSocket::createSSL()
void SecureSocket::freeSSL() void SecureSocket::freeSSL()
{ {
std::lock_guard ssl_lock{ssl_mutex_}; std::scoped_lock ssl_lock{ssl_mutex_};
isFatal(true); isFatal(true);
// take socket from multiplexer ASAP otherwise the race condition // take socket from multiplexer ASAP otherwise the race condition
@ -417,7 +417,7 @@ void SecureSocket::freeSSL()
int SecureSocket::secureAccept(int socket) int SecureSocket::secureAccept(int socket)
{ {
std::lock_guard ssl_lock{ssl_mutex_}; std::scoped_lock ssl_lock{ssl_mutex_};
createSSL(); createSSL();
@ -479,7 +479,7 @@ int SecureSocket::secureConnect(int socket)
return -1; return -1;
} }
std::lock_guard ssl_lock{ssl_mutex_}; std::scoped_lock ssl_lock{ssl_mutex_};
createSSL(); createSSL();

View File

@ -51,7 +51,7 @@ TCPListenSocket::~TCPListenSocket()
void TCPListenSocket::bind(const NetworkAddress &addr) void TCPListenSocket::bind(const NetworkAddress &addr)
{ {
try { try {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
ARCH->setReuseAddrOnSocket(m_socket, true); ARCH->setReuseAddrOnSocket(m_socket, true);
ARCH->bindSocket(m_socket, addr.getAddress()); ARCH->bindSocket(m_socket, addr.getAddress());
ARCH->listenOnSocket(m_socket); ARCH->listenOnSocket(m_socket);
@ -69,7 +69,7 @@ void TCPListenSocket::bind(const NetworkAddress &addr)
void TCPListenSocket::close() void TCPListenSocket::close()
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
if (m_socket == nullptr) { if (m_socket == nullptr) {
throw XIOClosed(); throw XIOClosed();
} }

View File

@ -66,7 +66,7 @@ void EiEventQueueBuffer::waitForEvent(double timeout_in_ms)
if (int retval = poll(pfds, POLLFD_COUNT, timeout); retval > 0) { if (int retval = poll(pfds, POLLFD_COUNT, timeout); retval > 0) {
if (pfds[EIFD].revents & POLLIN) { if (pfds[EIFD].revents & POLLIN) {
std::lock_guard lock(mutex_); std::scoped_lock lock{mutex_};
// libei doesn't allow ei_event_ref() because events are // libei doesn't allow ei_event_ref() because events are
// supposed to be short-lived only. So instead, we create an nullptr-data // supposed to be short-lived only. So instead, we create an nullptr-data
@ -108,7 +108,7 @@ IEventQueueBuffer::Type EiEventQueueBuffer::getEvent(Event &event, uint32_t &dat
// we just have a "something happened" event on the ei fd and the rest is // we just have a "something happened" event on the ei fd and the rest is
// handled by the EiScreen. // handled by the EiScreen.
// //
std::lock_guard lock(mutex_); std::scoped_lock lock{mutex_};
auto pair = queue_.front(); auto pair = queue_.front();
queue_.pop(); queue_.pop();
@ -125,7 +125,7 @@ IEventQueueBuffer::Type EiEventQueueBuffer::getEvent(Event &event, uint32_t &dat
bool EiEventQueueBuffer::addEvent(uint32_t dataID) bool EiEventQueueBuffer::addEvent(uint32_t dataID)
{ {
std::lock_guard lock(mutex_); std::scoped_lock lock{mutex_};
queue_.push({false, dataID}); queue_.push({false, dataID});
// tickle the pipe so our read thread wakes up // tickle the pipe so our read thread wakes up
@ -137,7 +137,7 @@ bool EiEventQueueBuffer::addEvent(uint32_t dataID)
bool EiEventQueueBuffer::isEmpty() const bool EiEventQueueBuffer::isEmpty() const
{ {
std::lock_guard lock(mutex_); std::scoped_lock lock{mutex_};
return queue_.empty(); return queue_.empty();
} }

View File

@ -720,7 +720,7 @@ void EiScreen::handle_portal_session_closed(const Event &event, void *)
void EiScreen::handleSystemEvent(const Event &sysevent, void *) void EiScreen::handleSystemEvent(const Event &sysevent, void *)
{ {
std::lock_guard lock(mutex_); std::scoped_lock lock{mutex_};
// Only one ei_dispatch per system event, see the comment in // Only one ei_dispatch per system event, see the comment in
// EiEventQueueBuffer::addEvent // EiEventQueueBuffer::addEvent

View File

@ -327,7 +327,7 @@ void MSWindowsWatchdog::startProcess()
void MSWindowsWatchdog::setProcessConfig(const std::string_view &command, bool elevate) void MSWindowsWatchdog::setProcessConfig(const std::string_view &command, bool elevate)
{ {
LOG_DEBUG1("locking process state mutex for watchdog config change"); LOG_DEBUG1("locking process state mutex for watchdog config change");
std::lock_guard lock(m_processStateMutex); std::scoped_lock lock{m_processStateMutex};
LOG_DEBUG("setting watchdog process config"); LOG_DEBUG("setting watchdog process config");
m_command = command; m_command = command;

View File

@ -66,7 +66,7 @@ bool OSXEventQueueBuffer::addEvent(uint32_t dataID)
{ {
// Use GCD to dispatch event addition on the main queue // Use GCD to dispatch event addition on the main queue
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
std::lock_guard lock(this->m_mutex); std::scoped_lock lock{this->m_mutex};
LOG_DEBUG2("adding user event with dataID: %u", dataID); LOG_DEBUG2("adding user event with dataID: %u", dataID);
this->m_dataQueue.push(dataID); this->m_dataQueue.push(dataID);
this->m_cond.notify_one(); this->m_cond.notify_one();
@ -79,7 +79,7 @@ bool OSXEventQueueBuffer::addEvent(uint32_t dataID)
bool OSXEventQueueBuffer::isEmpty() const bool OSXEventQueueBuffer::isEmpty() const
{ {
std::lock_guard lock(m_mutex); std::scoped_lock lock{m_mutex};
bool empty = m_dataQueue.empty(); bool empty = m_dataQueue.empty();
LOG_DEBUG2("queue is %s", empty ? "empty" : "not empty"); LOG_DEBUG2("queue is %s", empty ? "empty" : "not empty");
return empty; return empty;

View File

@ -50,7 +50,7 @@ XWindowsEventQueueBuffer::~XWindowsEventQueueBuffer()
int XWindowsEventQueueBuffer::getPendingCountLocked() int XWindowsEventQueueBuffer::getPendingCountLocked()
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
return XPending(m_display); return XPending(m_display);
} }
@ -68,7 +68,7 @@ void XWindowsEventQueueBuffer::waitForEvent(double dtimeout)
} }
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
// we're now waiting for events // we're now waiting for events
m_waiting = true; m_waiting = true;
@ -122,7 +122,7 @@ void XWindowsEventQueueBuffer::waitForEvent(double dtimeout)
{ {
// we're no longer waiting for events // we're no longer waiting for events
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
m_waiting = false; m_waiting = false;
} }
@ -131,7 +131,7 @@ void XWindowsEventQueueBuffer::waitForEvent(double dtimeout)
IEventQueueBuffer::Type XWindowsEventQueueBuffer::getEvent(Event &event, uint32_t &dataID) IEventQueueBuffer::Type XWindowsEventQueueBuffer::getEvent(Event &event, uint32_t &dataID)
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
// push out pending events // push out pending events
flush(); flush();
@ -160,7 +160,7 @@ bool XWindowsEventQueueBuffer::addEvent(uint32_t dataID)
xevent.xclient.data.l[0] = static_cast<long>(dataID); xevent.xclient.data.l[0] = static_cast<long>(dataID);
// save the message // save the message
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
m_postedEvents.push_back(xevent); m_postedEvents.push_back(xevent);
// if we're currently waiting for an event then send saved events to // if we're currently waiting for an event then send saved events to
@ -186,7 +186,7 @@ bool XWindowsEventQueueBuffer::addEvent(uint32_t dataID)
bool XWindowsEventQueueBuffer::isEmpty() const bool XWindowsEventQueueBuffer::isEmpty() const
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::scoped_lock lock{m_mutex};
return (XPending(m_display) == 0); return (XPending(m_display) == 0);
} }