From: Gunnar Beutner Date: Wed, 10 Jan 2018 15:44:48 +0000 (+0100) Subject: Update the RingBuffer class to use a regular mutex instead of ObjectLock X-Git-Tag: v2.9.0~230^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=refs%2Fpull%2F5968%2Fhead;p=icinga2 Update the RingBuffer class to use a regular mutex instead of ObjectLock --- diff --git a/lib/base/ringbuffer.cpp b/lib/base/ringbuffer.cpp index f7fd4d882..9e3b1c840 100644 --- a/lib/base/ringbuffer.cpp +++ b/lib/base/ringbuffer.cpp @@ -25,20 +25,24 @@ 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) @@ -68,9 +72,14 @@ void RingBuffer::InsertValue(RingBuffer::SizeType tv, int num) 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(); @@ -92,7 +101,8 @@ int RingBuffer::UpdateAndGetValues(RingBuffer::SizeType tv, RingBuffer::SizeType 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(std::min(span, m_InsertedValues)); } diff --git a/lib/base/ringbuffer.hpp b/lib/base/ringbuffer.hpp index 94917668a..44a96f868 100644 --- a/lib/base/ringbuffer.hpp +++ b/lib/base/ringbuffer.hpp @@ -22,6 +22,7 @@ #include "base/i2-base.hpp" #include "base/object.hpp" +#include #include namespace icinga @@ -32,7 +33,7 @@ namespace icinga * * @ingroup base */ -class RingBuffer final : public Object +class RingBuffer final { public: DECLARE_PTR_TYPEDEFS(RingBuffer); @@ -47,9 +48,13 @@ public: double CalculateRate(SizeType tv, SizeType span); private: + mutable boost::mutex m_Mutex; std::vector m_Slots; SizeType m_TimeValue; SizeType m_InsertedValues; + + void InsertValueUnlocked(SizeType tv, int num); + int UpdateAndGetValuesUnlocked(SizeType tv, SizeType span); }; }