X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fplatform%2Futil%2Fbuffer.h;h=aa658ee61f231b4d111ba9d1a19a82bcfd3c17a8;hb=200322e51c698c791ede1fad098e11defc5f81e8;hp=1c12e86dd05b6ec961f5521f1252ddec0712be58;hpb=ba65909d0a9c43a1bac71c6182c53f202285cec5;p=deb_libcec.git diff --git a/src/lib/platform/util/buffer.h b/src/lib/platform/util/buffer.h index 1c12e86..aa658ee 100644 --- a/src/lib/platform/util/buffer.h +++ b/src/lib/platform/util/buffer.h @@ -2,7 +2,7 @@ /* * 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. @@ -40,24 +40,34 @@ namespace PLATFORM 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) { @@ -66,17 +76,41 @@ namespace PLATFORM 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; @@ -86,5 +120,7 @@ namespace PLATFORM size_t m_maxSize; std::queue<_BType> m_buffer; CMutex m_mutex; + CCondition m_condition; + bool m_bHasMessages; }; };