2007-12-01 04:08:34 +00:00
|
|
|
#include "MailBox.h"
|
2007-12-09 02:28:28 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include "win32/Window.h"
|
|
|
|
#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
|
|
|
|
{
|
|
|
|
return m_calls.size() != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMailBox::WaitForCall()
|
|
|
|
{
|
2008-12-13 00:30:38 +00:00
|
|
|
boost::mutex::scoped_lock waitLock(m_waitMutex);
|
2008-12-15 02:57:21 +00:00
|
|
|
while(!IsPending())
|
|
|
|
{
|
|
|
|
m_waitCondition.wait(waitLock);
|
|
|
|
}
|
2007-12-01 04:08:34 +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
|
|
|
{
|
2007-12-09 02:28:28 +00:00
|
|
|
{
|
2008-12-13 00:30:38 +00:00
|
|
|
boost::mutex::scoped_lock waitLock(m_waitMutex);
|
|
|
|
MESSAGE message;
|
|
|
|
message.function = function;
|
|
|
|
message.sync = waitForCompletion;
|
|
|
|
m_calls.push_back(message);
|
2007-12-09 02:28:28 +00:00
|
|
|
m_waitCondition.notify_all();
|
|
|
|
}
|
|
|
|
if(waitForCompletion)
|
|
|
|
{
|
2008-12-13 00:30:38 +00:00
|
|
|
boost::mutex::scoped_lock doneLock(m_doneNotifyMutex);
|
2008-12-15 02:57:21 +00:00
|
|
|
m_callDone = false;
|
|
|
|
while(!m_callDone)
|
2007-12-09 02:28:28 +00:00
|
|
|
{
|
2008-12-15 02:57:21 +00:00
|
|
|
#ifdef WIN32
|
2007-12-09 02:28:28 +00:00
|
|
|
MSG wmmsg;
|
|
|
|
while(PeekMessage(&wmmsg, NULL, 0, 0, PM_REMOVE))
|
|
|
|
{
|
|
|
|
TranslateMessage(&wmmsg);
|
|
|
|
DispatchMessage(&wmmsg);
|
|
|
|
}
|
2008-12-15 02:57:21 +00:00
|
|
|
boost::xtime xt;
|
|
|
|
boost::xtime_get(&xt, boost::TIME_UTC);
|
|
|
|
xt.nsec += 100 * 1000000;
|
|
|
|
m_callFinished.timed_wait(doneLock, xt);
|
2007-12-09 02:28:28 +00:00
|
|
|
#else
|
2008-12-15 02:57:21 +00:00
|
|
|
m_callFinished.wait(doneLock);
|
2007-12-09 02:28:28 +00:00
|
|
|
#endif
|
2008-12-15 02:57:21 +00:00
|
|
|
}
|
2007-12-09 02:28:28 +00:00
|
|
|
}
|
2007-12-01 04:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMailBox::ReceiveCall()
|
|
|
|
{
|
2008-12-13 00:30:38 +00:00
|
|
|
MESSAGE message;
|
2007-12-09 02:28:28 +00:00
|
|
|
{
|
2008-12-13 00:30:38 +00:00
|
|
|
boost::mutex::scoped_lock waitLock(m_waitMutex);
|
2007-12-09 02:28:28 +00:00
|
|
|
if(!IsPending()) return;
|
2008-12-13 00:30:38 +00:00
|
|
|
message = *m_calls.begin();
|
2007-12-09 02:28:28 +00:00
|
|
|
m_calls.pop_front();
|
|
|
|
}
|
2008-12-13 00:30:38 +00:00
|
|
|
message.function();
|
|
|
|
if(message.sync)
|
2007-12-09 02:28:28 +00:00
|
|
|
{
|
2008-12-13 00:30:38 +00:00
|
|
|
boost::mutex::scoped_lock doneLock(m_doneNotifyMutex);
|
2008-12-15 02:57:21 +00:00
|
|
|
m_callDone = true;
|
2007-12-09 02:28:28 +00:00
|
|
|
m_callFinished.notify_all();
|
|
|
|
}
|
2007-12-01 04:08:34 +00:00
|
|
|
}
|