2007-12-01 04:08:34 +00:00
|
|
|
#include "MailBox.h"
|
|
|
|
|
|
|
|
bool CMailBox::IsPending() const
|
|
|
|
{
|
2012-08-05 19:13:23 +00:00
|
|
|
return m_calls.size() != 0;
|
2007-12-01 04:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMailBox::WaitForCall()
|
|
|
|
{
|
2024-05-18 11:41:27 -04:00
|
|
|
std::unique_lock callLock(m_callMutex);
|
2012-08-05 19:13:23 +00:00
|
|
|
while(!IsPending())
|
|
|
|
{
|
2013-07-07 00:31:53 +00:00
|
|
|
m_waitCondition.wait(callLock);
|
2012-08-05 19:13:23 +00:00
|
|
|
}
|
2007-12-01 04:08:34 +00:00
|
|
|
}
|
|
|
|
|
2009-02-10 05:06:57 +00:00
|
|
|
void CMailBox::WaitForCall(unsigned int timeOut)
|
|
|
|
{
|
2024-05-18 11:41:27 -04:00
|
|
|
std::unique_lock callLock(m_callMutex);
|
2012-08-05 19:13:23 +00:00
|
|
|
if(IsPending()) return;
|
2013-07-07 00:31:53 +00:00
|
|
|
m_waitCondition.wait_for(callLock, std::chrono::milliseconds(timeOut));
|
2009-02-10 05:06:57 +00:00
|
|
|
}
|
|
|
|
|
2013-05-19 07:18:00 +00:00
|
|
|
void CMailBox::FlushCalls()
|
|
|
|
{
|
2018-04-30 21:01:23 +01:00
|
|
|
SendCall([]() {}, true);
|
2013-05-19 07:18:00 +00:00
|
|
|
}
|
|
|
|
|
2007-12-09 02:28:28 +00:00
|
|
|
void CMailBox::SendCall(const FunctionType& function, bool waitForCompletion)
|
2007-12-01 04:08:34 +00:00
|
|
|
{
|
2023-10-25 13:20:43 +01:00
|
|
|
std::future<void> future;
|
2024-05-18 11:41:27 -04:00
|
|
|
|
2017-03-25 18:10:49 -04:00
|
|
|
{
|
|
|
|
MESSAGE message;
|
|
|
|
message.function = function;
|
2023-10-25 13:20:43 +01:00
|
|
|
|
|
|
|
if(waitForCompletion)
|
|
|
|
{
|
2024-05-18 11:28:47 -04:00
|
|
|
message.promise = std::make_unique<std::promise<void>>();
|
|
|
|
future = message.promise->get_future();
|
2023-10-25 13:20:43 +01:00
|
|
|
}
|
|
|
|
|
2024-05-18 11:41:27 -04:00
|
|
|
std::lock_guard callLock(m_callMutex);
|
2017-03-25 18:10:49 -04:00
|
|
|
m_calls.push_back(std::move(message));
|
|
|
|
}
|
2018-04-30 21:01:23 +01:00
|
|
|
|
2013-07-07 00:31:53 +00:00
|
|
|
m_waitCondition.notify_all();
|
|
|
|
|
2012-08-05 19:13:23 +00:00
|
|
|
if(waitForCompletion)
|
|
|
|
{
|
2023-10-25 13:20:43 +01:00
|
|
|
future.wait();
|
2012-08-05 19:13:23 +00:00
|
|
|
}
|
2007-12-01 04:08:34 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 18:10:49 -04:00
|
|
|
void CMailBox::SendCall(FunctionType&& function)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
MESSAGE message;
|
|
|
|
message.function = std::move(function);
|
2024-05-18 11:41:27 -04:00
|
|
|
|
|
|
|
std::lock_guard callLock(m_callMutex);
|
2017-03-25 18:10:49 -04:00
|
|
|
m_calls.push_back(std::move(message));
|
|
|
|
}
|
2018-04-30 21:01:23 +01:00
|
|
|
|
2017-03-25 18:10:49 -04:00
|
|
|
m_waitCondition.notify_all();
|
|
|
|
}
|
|
|
|
|
2007-12-01 04:08:34 +00:00
|
|
|
void CMailBox::ReceiveCall()
|
|
|
|
{
|
2012-08-05 19:13:23 +00:00
|
|
|
MESSAGE message;
|
|
|
|
{
|
2024-05-18 11:41:27 -04:00
|
|
|
std::lock_guard callLock(m_callMutex);
|
2012-08-05 19:13:23 +00:00
|
|
|
if(!IsPending()) return;
|
2017-03-25 18:10:49 -04:00
|
|
|
message = std::move(m_calls.front());
|
2012-08-05 19:13:23 +00:00
|
|
|
m_calls.pop_front();
|
|
|
|
}
|
|
|
|
message.function();
|
2024-05-18 11:28:47 -04:00
|
|
|
if(message.promise)
|
|
|
|
{
|
|
|
|
message.promise->set_value();
|
|
|
|
}
|
2007-12-01 04:08:34 +00:00
|
|
|
}
|