2005-06-25 04:21:26 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2005-06-28 01:04:34 +02:00
|
|
|
#include "MessagePasserImpl.h"
|
2005-06-25 04:21:26 +02:00
|
|
|
|
2005-08-20 17:44:50 +02:00
|
|
|
#define MESSAGE_TRACE 0
|
|
|
|
|
|
|
|
#if MESSAGE_TRACE
|
|
|
|
#include "Messages.h" // for mCommand implementation
|
|
|
|
#endif
|
|
|
|
|
2005-06-25 04:21:26 +02:00
|
|
|
using namespace AtlasMessage;
|
|
|
|
|
2005-08-20 17:44:50 +02:00
|
|
|
|
2005-07-03 18:25:48 +02:00
|
|
|
template<typename T> void MessagePasserImpl<T>::Add(T* msg)
|
2005-06-25 04:21:26 +02:00
|
|
|
{
|
2005-08-20 17:44:50 +02:00
|
|
|
debug_assert(msg);
|
|
|
|
|
|
|
|
#if MESSAGE_TRACE
|
|
|
|
debug_printf("Add %s\n", msg->GetType());
|
|
|
|
#endif
|
2005-06-25 04:21:26 +02:00
|
|
|
m_Mutex.Lock();
|
|
|
|
|
|
|
|
m_Queue.push(msg);
|
|
|
|
|
|
|
|
m_Mutex.Unlock();
|
|
|
|
}
|
|
|
|
|
2005-07-03 18:25:48 +02:00
|
|
|
template <typename T> T* MessagePasserImpl<T>::Retrieve()
|
2005-06-25 04:21:26 +02:00
|
|
|
{
|
2005-07-03 18:25:48 +02:00
|
|
|
// (It should be fairly easy to use a more efficient thread-safe queue,
|
|
|
|
// since there's only one thread adding items and one thread consuming;
|
|
|
|
// but it's not worthwhile yet.)
|
|
|
|
|
2005-06-25 04:21:26 +02:00
|
|
|
m_Mutex.Lock();
|
|
|
|
|
2005-07-03 18:25:48 +02:00
|
|
|
T* msg = NULL;
|
2005-06-25 04:21:26 +02:00
|
|
|
if (! m_Queue.empty())
|
|
|
|
{
|
|
|
|
msg = m_Queue.front();
|
|
|
|
m_Queue.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Mutex.Unlock();
|
|
|
|
|
2005-08-20 17:44:50 +02:00
|
|
|
#if MESSAGE_TRACE
|
|
|
|
if (msg) debug_printf("Retrieved %s\n", msg->GetType());
|
|
|
|
#endif
|
|
|
|
|
2005-06-25 04:21:26 +02:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2005-09-13 05:57:34 +02:00
|
|
|
template <typename T> bool MessagePasserImpl<T>::IsEmpty()
|
2005-06-25 04:21:26 +02:00
|
|
|
{
|
2005-09-13 05:57:34 +02:00
|
|
|
m_Mutex.Lock();
|
|
|
|
bool empty = m_Queue.empty();
|
|
|
|
m_Mutex.Unlock();
|
|
|
|
return empty;
|
2005-06-25 04:21:26 +02:00
|
|
|
}
|
|
|
|
|
2005-07-03 18:25:48 +02:00
|
|
|
MessagePasser<mCommand>* g_MessagePasser_Command = NULL;
|
|
|
|
MessagePasser<mInput>* g_MessagePasser_Input = NULL;
|
|
|
|
|
|
|
|
// Explicit instantiation:
|
|
|
|
template MessagePasserImpl<mCommand>;
|
2005-08-20 17:44:50 +02:00
|
|
|
template MessagePasserImpl<mInput>;
|