Clean locks.

This commit is contained in:
Jean-Philip Desjardins 2024-05-18 11:41:27 -04:00
parent 58a9c2f278
commit 5b80de936e

View file

@ -7,7 +7,7 @@ bool CMailBox::IsPending() const
void CMailBox::WaitForCall() void CMailBox::WaitForCall()
{ {
std::unique_lock<std::mutex> callLock(m_callMutex); std::unique_lock callLock(m_callMutex);
while(!IsPending()) while(!IsPending())
{ {
m_waitCondition.wait(callLock); m_waitCondition.wait(callLock);
@ -16,7 +16,7 @@ void CMailBox::WaitForCall()
void CMailBox::WaitForCall(unsigned int timeOut) void CMailBox::WaitForCall(unsigned int timeOut)
{ {
std::unique_lock<std::mutex> callLock(m_callMutex); std::unique_lock callLock(m_callMutex);
if(IsPending()) return; if(IsPending()) return;
m_waitCondition.wait_for(callLock, std::chrono::milliseconds(timeOut)); m_waitCondition.wait_for(callLock, std::chrono::milliseconds(timeOut));
} }
@ -29,8 +29,8 @@ void CMailBox::FlushCalls()
void CMailBox::SendCall(const FunctionType& function, bool waitForCompletion) void CMailBox::SendCall(const FunctionType& function, bool waitForCompletion)
{ {
std::future<void> future; std::future<void> future;
{ {
std::unique_lock<std::mutex> callLock(m_callMutex);
MESSAGE message; MESSAGE message;
message.function = function; message.function = function;
@ -40,6 +40,7 @@ void CMailBox::SendCall(const FunctionType& function, bool waitForCompletion)
future = message.promise->get_future(); future = message.promise->get_future();
} }
std::lock_guard callLock(m_callMutex);
m_calls.push_back(std::move(message)); m_calls.push_back(std::move(message));
} }
@ -53,11 +54,11 @@ void CMailBox::SendCall(const FunctionType& function, bool waitForCompletion)
void CMailBox::SendCall(FunctionType&& function) void CMailBox::SendCall(FunctionType&& function)
{ {
std::lock_guard<std::mutex> callLock(m_callMutex);
{ {
MESSAGE message; MESSAGE message;
message.function = std::move(function); message.function = std::move(function);
std::lock_guard callLock(m_callMutex);
m_calls.push_back(std::move(message)); m_calls.push_back(std::move(message));
} }
@ -68,7 +69,7 @@ void CMailBox::ReceiveCall()
{ {
MESSAGE message; MESSAGE message;
{ {
std::lock_guard<std::mutex> waitLock(m_callMutex); std::lock_guard callLock(m_callMutex);
if(!IsPending()) return; if(!IsPending()) return;
message = std::move(m_calls.front()); message = std::move(m_calls.front());
m_calls.pop_front(); m_calls.pop_front();