/*
* This file is part of the libCEC(R) library.
*
- * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
+ * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
* libCEC(R) is an original work, containing original code.
*
* libCEC(R) is a trademark of Pulse-Eight Limited.
struct SyncedBuffer
{
public:
- SyncedBuffer(size_t iMaxSize = 100)
- {
- m_maxSize = iMaxSize;
- }
+ SyncedBuffer(size_t iMaxSize = 100) :
+ m_maxSize(iMaxSize),
+ m_bHasMessages(false) {}
virtual ~SyncedBuffer(void)
{
- CLockObject lock(m_mutex, true);
Clear();
}
void Clear(void)
{
+ CLockObject lock(m_mutex);
while (!m_buffer.empty())
m_buffer.pop();
+ m_condition.Broadcast();
}
- size_t Size(void) const { return m_buffer.size(); }
+ size_t Size(void)
+ {
+ CLockObject lock(m_mutex);
+ return m_buffer.size();
+ }
+
+ bool IsEmpty(void)
+ {
+ CLockObject lock(m_mutex);
+ return m_buffer.empty();
+ }
bool Push(_BType entry)
{
return false;
m_buffer.push(entry);
+ m_bHasMessages = true;
+ m_condition.Signal();
return true;
}
- bool Pop(_BType &entry)
+ bool Pop(_BType &entry, uint32_t iTimeoutMs = 0)
{
bool bReturn(false);
CLockObject lock(m_mutex);
+
+ // wait for a signal if the buffer is empty
+ if (m_buffer.empty() && iTimeoutMs > 0)
+ {
+ if (!m_condition.Wait(m_mutex, m_bHasMessages, iTimeoutMs))
+ return bReturn;
+ }
+
+ // pop the first item
if (!m_buffer.empty())
{
entry = m_buffer.front();
m_buffer.pop();
+ m_bHasMessages = !m_buffer.empty();
+ bReturn = true;
+ }
+ return bReturn;
+ }
+
+ bool Peek(_BType &entry)
+ {
+ bool bReturn(false);
+ CLockObject lock(m_mutex);
+ if (!m_buffer.empty())
+ {
+ entry = m_buffer.front();
bReturn = true;
}
return bReturn;
size_t m_maxSize;
std::queue<_BType> m_buffer;
CMutex m_mutex;
+ CCondition<bool> m_condition;
+ bool m_bHasMessages;
};
};