2007-12-01 04:08:34 +00:00
|
|
|
#include "MailBox.h"
|
2017-02-21 15:47:18 +00:00
|
|
|
#if defined(_WIN32)
|
2013-09-07 04:30:52 +00:00
|
|
|
#include "win32/Win32Defs.h"
|
2007-12-09 02:28:28 +00:00
|
|
|
#endif
|
2007-12-01 04:08:34 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2013-07-07 00:31:53 +00:00
|
|
|
std::unique_lock<std::mutex> 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)
|
|
|
|
{
|
2013-07-07 00:31:53 +00:00
|
|
|
std::unique_lock<std::mutex> 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 20:01:23 +00: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
|
|
|
{
|
2013-07-07 00:31:53 +00:00
|
|
|
std::unique_lock<std::mutex> callLock(m_callMutex);
|
|
|
|
|
2017-03-25 22:10:49 +00:00
|
|
|
{
|
|
|
|
MESSAGE message;
|
|
|
|
message.function = function;
|
2018-04-30 20:01:23 +00:00
|
|
|
message.sync = waitForCompletion;
|
2017-03-25 22:10:49 +00:00
|
|
|
m_calls.push_back(std::move(message));
|
|
|
|
}
|
2018-04-30 20:01:23 +00:00
|
|
|
|
2013-07-07 00:31:53 +00:00
|
|
|
m_waitCondition.notify_all();
|
|
|
|
|
2012-08-05 19:13:23 +00:00
|
|
|
if(waitForCompletion)
|
|
|
|
{
|
|
|
|
m_callDone = false;
|
2008-12-15 02:57:21 +00:00
|
|
|
while(!m_callDone)
|
2012-08-05 19:13:23 +00:00
|
|
|
{
|
2013-09-07 04:30:52 +00:00
|
|
|
#ifdef FRAMEWORK_WIN_DESKTOP
|
2012-08-05 19:13:23 +00:00
|
|
|
MSG wmmsg;
|
|
|
|
while(PeekMessage(&wmmsg, NULL, 0, 0, PM_REMOVE))
|
|
|
|
{
|
|
|
|
TranslateMessage(&wmmsg);
|
|
|
|
DispatchMessage(&wmmsg);
|
|
|
|
}
|
2013-07-07 00:31:53 +00:00
|
|
|
m_callFinished.wait_for(callLock, std::chrono::milliseconds(100));
|
2007-12-09 02:28:28 +00:00
|
|
|
#else
|
2013-07-07 00:31:53 +00:00
|
|
|
m_callFinished.wait(callLock);
|
2007-12-09 02:28:28 +00:00
|
|
|
#endif
|
2012-08-05 19:13:23 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-01 04:08:34 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 22:10:49 +00:00
|
|
|
void CMailBox::SendCall(FunctionType&& function)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> callLock(m_callMutex);
|
2018-04-30 20:01:23 +00:00
|
|
|
|
2017-03-25 22:10:49 +00:00
|
|
|
{
|
|
|
|
MESSAGE message;
|
|
|
|
message.function = std::move(function);
|
2018-04-30 20:01:23 +00:00
|
|
|
message.sync = false;
|
2017-03-25 22:10:49 +00:00
|
|
|
m_calls.push_back(std::move(message));
|
|
|
|
}
|
2018-04-30 20:01:23 +00:00
|
|
|
|
2017-03-25 22:10:49 +00: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;
|
|
|
|
{
|
2017-03-25 22:10:49 +00:00
|
|
|
std::lock_guard<std::mutex> waitLock(m_callMutex);
|
2012-08-05 19:13:23 +00:00
|
|
|
if(!IsPending()) return;
|
2017-03-25 22:10:49 +00:00
|
|
|
message = std::move(m_calls.front());
|
2012-08-05 19:13:23 +00:00
|
|
|
m_calls.pop_front();
|
|
|
|
}
|
|
|
|
message.function();
|
2008-12-13 00:30:38 +00:00
|
|
|
if(message.sync)
|
2012-08-05 19:13:23 +00:00
|
|
|
{
|
2017-03-25 22:10:49 +00:00
|
|
|
std::lock_guard<std::mutex> waitLock(m_callMutex);
|
2012-08-05 19:13:23 +00:00
|
|
|
m_callDone = true;
|
|
|
|
m_callFinished.notify_all();
|
|
|
|
}
|
2007-12-01 04:08:34 +00:00
|
|
|
}
|