X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fplatform%2Futil%2Fbuffer.h;h=28ba30cf3468bd61d7dac496d16b04f3ef23497b;hb=2ee922ad2f1812c74f2aa6e61305fdcc5c27de53;hp=ce6029c622cda793261f8de667e1cdc2af5c2ebc;hpb=d749efb2d01817c0a8ad8b32a1b9861c5a78c83d;p=deb_libcec.git diff --git a/src/lib/platform/util/buffer.h b/src/lib/platform/util/buffer.h index ce6029c..28ba30c 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-2012 Pulse-Eight Limited. All rights reserved. + * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved. * libCEC(R) is an original work, containing original code. * * libCEC(R) is a trademark of Pulse-Eight Limited. @@ -31,7 +31,7 @@ * http://www.pulse-eight.net/ */ -#include "../threads/mutex.h" +#include "lib/platform/threads/mutex.h" #include namespace PLATFORM @@ -40,24 +40,35 @@ 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_bHasMessages = true; + 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,13 +77,25 @@ 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 + m_bHasMessages = !m_buffer.empty(); if (!m_buffer.empty()) { entry = m_buffer.front(); @@ -82,9 +105,23 @@ namespace PLATFORM 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; + } + private: size_t m_maxSize; std::queue<_BType> m_buffer; CMutex m_mutex; + CCondition m_condition; + bool m_bHasMessages; }; };