Skip to content

Commit

Permalink
Clean locks.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpd002 committed May 27, 2024
1 parent 58a9c2f commit 5b80de9
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions Source/MailBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ bool CMailBox::IsPending() const

void CMailBox::WaitForCall()
{
std::unique_lock<std::mutex> callLock(m_callMutex);
std::unique_lock callLock(m_callMutex);
while(!IsPending())
{
m_waitCondition.wait(callLock);
Expand All @@ -16,7 +16,7 @@ void CMailBox::WaitForCall()

void CMailBox::WaitForCall(unsigned int timeOut)
{
std::unique_lock<std::mutex> callLock(m_callMutex);
std::unique_lock callLock(m_callMutex);
if(IsPending()) return;
m_waitCondition.wait_for(callLock, std::chrono::milliseconds(timeOut));
}
Expand All @@ -29,8 +29,8 @@ void CMailBox::FlushCalls()
void CMailBox::SendCall(const FunctionType& function, bool waitForCompletion)
{
std::future<void> future;

{
std::unique_lock<std::mutex> callLock(m_callMutex);
MESSAGE message;
message.function = function;

Expand All @@ -40,6 +40,7 @@ void CMailBox::SendCall(const FunctionType& function, bool waitForCompletion)
future = message.promise->get_future();
}

std::lock_guard callLock(m_callMutex);
m_calls.push_back(std::move(message));
}

Expand All @@ -53,11 +54,11 @@ void CMailBox::SendCall(const FunctionType& function, bool waitForCompletion)

void CMailBox::SendCall(FunctionType&& function)
{
std::lock_guard<std::mutex> callLock(m_callMutex);

{
MESSAGE message;
message.function = std::move(function);

std::lock_guard callLock(m_callMutex);
m_calls.push_back(std::move(message));
}

Expand All @@ -68,7 +69,7 @@ void CMailBox::ReceiveCall()
{
MESSAGE message;
{
std::lock_guard<std::mutex> waitLock(m_callMutex);
std::lock_guard callLock(m_callMutex);
if(!IsPending()) return;
message = std::move(m_calls.front());
m_calls.pop_front();
Expand Down

0 comments on commit 5b80de9

Please sign in to comment.