Merge pull request #5968 from Icinga/feature/ringbuffer-mutex

Update the RingBuffer class to use a regular mutex instead of ObjectLock
This commit is contained in:
Michael Friedrich 2018-01-11 13:37:35 +01:00 committed by GitHub
commit de5f2b01c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 9 deletions

View File

@ -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<double>(std::min(span, m_InsertedValues));
}

View File

@ -22,6 +22,7 @@
#include "base/i2-base.hpp"
#include "base/object.hpp"
#include <boost/thread/mutex.hpp>
#include <vector>
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<int> m_Slots;
SizeType m_TimeValue;
SizeType m_InsertedValues;
void InsertValueUnlocked(SizeType tv, int num);
int UpdateAndGetValuesUnlocked(SizeType tv, SizeType span);
};
}