2007-12-01 04:08:34 +00:00
|
|
|
#include "MailBox.h"
|
2013-04-28 22:23:25 +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
|
|
|
|
|
|
|
CMailBox::CMailBox()
|
|
|
|
{
|
2008-12-13 00:30:38 +00:00
|
|
|
|
2007-12-01 04:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CMailBox::~CMailBox()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
SendCall([] () { }, true);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
MESSAGE message;
|
|
|
|
message.function = function;
|
|
|
|
message.sync = waitForCompletion;
|
|
|
|
m_calls.push_back(message);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void CMailBox::ReceiveCall()
|
|
|
|
{
|
2012-08-05 19:13:23 +00:00
|
|
|
MESSAGE message;
|
|
|
|
{
|
2013-07-07 00:31:53 +00:00
|
|
|
std::unique_lock<std::mutex> waitLock(m_callMutex);
|
2012-08-05 19:13:23 +00:00
|
|
|
if(!IsPending()) return;
|
|
|
|
message = *m_calls.begin();
|
|
|
|
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
|
|
|
{
|
2013-07-07 00:31:53 +00:00
|
|
|
std::unique_lock<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
|
|
|
}
|