using namespace icinga;
RingBuffer::RingBuffer(RingBuffer::SizeType slots)
- : Object(), m_Slots(slots, 0), m_TimeValue(0), m_InsertedValues(0)
+ : m_Slots(slots, 0), m_TimeValue(0), m_InsertedValues(0)
{ }
RingBuffer::SizeType RingBuffer::GetLength() const
{
- ObjectLock olock(this);
-
+ boost::mutex::scoped_lock lock(m_Mutex);
return m_Slots.size();
}
void RingBuffer::InsertValue(RingBuffer::SizeType tv, int num)
{
- ObjectLock olock(this);
+ boost::mutex::scoped_lock lock(m_Mutex);
+
+ InsertValueUnlocked(tv, num);
+}
+void RingBuffer::InsertValueUnlocked(RingBuffer::SizeType tv, int num)
+{
RingBuffer::SizeType offsetTarget = tv % m_Slots.size();
if (m_TimeValue == 0)
int RingBuffer::UpdateAndGetValues(RingBuffer::SizeType tv, RingBuffer::SizeType span)
{
- ObjectLock olock(this);
+ boost::mutex::scoped_lock lock(m_Mutex);
- InsertValue(tv, 0);
+ return UpdateAndGetValuesUnlocked(tv, span);
+}
+
+int RingBuffer::UpdateAndGetValuesUnlocked(RingBuffer::SizeType tv, RingBuffer::SizeType span)
+{
+ InsertValueUnlocked(tv, 0);
if (span > m_Slots.size())
span = m_Slots.size();
double RingBuffer::CalculateRate(RingBuffer::SizeType tv, RingBuffer::SizeType span)
{
- ObjectLock olock(this);
- int sum = UpdateAndGetValues(tv, span);
+ boost::mutex::scoped_lock lock(m_Mutex);
+
+ int sum = UpdateAndGetValuesUnlocked(tv, span);
return sum / static_cast<double>(std::min(span, m_InsertedValues));
}
#include "base/i2-base.hpp"
#include "base/object.hpp"
+#include <boost/thread/mutex.hpp>
#include <vector>
namespace icinga
*
* @ingroup base
*/
-class RingBuffer final : public Object
+class RingBuffer final
{
public:
DECLARE_PTR_TYPEDEFS(RingBuffer);
double CalculateRate(SizeType tv, SizeType span);
private:
+ mutable boost::mutex m_Mutex;
std::vector<int> m_Slots;
SizeType m_TimeValue;
SizeType m_InsertedValues;
+
+ void InsertValueUnlocked(SizeType tv, int num);
+ int UpdateAndGetValuesUnlocked(SizeType tv, SizeType span);
};
}