mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-23 05:34:48 +02:00
Update the RingBuffer class to use a regular mutex instead of ObjectLock
This commit is contained in:
parent
e26494bf08
commit
ddf09263af
@ -25,20 +25,24 @@
|
|||||||
using namespace icinga;
|
using namespace icinga;
|
||||||
|
|
||||||
RingBuffer::RingBuffer(RingBuffer::SizeType slots)
|
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
|
RingBuffer::SizeType RingBuffer::GetLength() const
|
||||||
{
|
{
|
||||||
ObjectLock olock(this);
|
boost::mutex::scoped_lock lock(m_Mutex);
|
||||||
|
|
||||||
return m_Slots.size();
|
return m_Slots.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RingBuffer::InsertValue(RingBuffer::SizeType tv, int num)
|
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();
|
RingBuffer::SizeType offsetTarget = tv % m_Slots.size();
|
||||||
|
|
||||||
if (m_TimeValue == 0)
|
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)
|
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())
|
if (span > m_Slots.size())
|
||||||
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)
|
double RingBuffer::CalculateRate(RingBuffer::SizeType tv, RingBuffer::SizeType span)
|
||||||
{
|
{
|
||||||
ObjectLock olock(this);
|
boost::mutex::scoped_lock lock(m_Mutex);
|
||||||
int sum = UpdateAndGetValues(tv, span);
|
|
||||||
|
int sum = UpdateAndGetValuesUnlocked(tv, span);
|
||||||
return sum / static_cast<double>(std::min(span, m_InsertedValues));
|
return sum / static_cast<double>(std::min(span, m_InsertedValues));
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include "base/i2-base.hpp"
|
#include "base/i2-base.hpp"
|
||||||
#include "base/object.hpp"
|
#include "base/object.hpp"
|
||||||
|
#include <boost/thread/mutex.hpp>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace icinga
|
namespace icinga
|
||||||
@ -32,7 +33,7 @@ namespace icinga
|
|||||||
*
|
*
|
||||||
* @ingroup base
|
* @ingroup base
|
||||||
*/
|
*/
|
||||||
class RingBuffer final : public Object
|
class RingBuffer final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
DECLARE_PTR_TYPEDEFS(RingBuffer);
|
DECLARE_PTR_TYPEDEFS(RingBuffer);
|
||||||
@ -47,9 +48,13 @@ public:
|
|||||||
double CalculateRate(SizeType tv, SizeType span);
|
double CalculateRate(SizeType tv, SizeType span);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
mutable boost::mutex m_Mutex;
|
||||||
std::vector<int> m_Slots;
|
std::vector<int> m_Slots;
|
||||||
SizeType m_TimeValue;
|
SizeType m_TimeValue;
|
||||||
SizeType m_InsertedValues;
|
SizeType m_InsertedValues;
|
||||||
|
|
||||||
|
void InsertValueUnlocked(SizeType tv, int num);
|
||||||
|
int UpdateAndGetValuesUnlocked(SizeType tv, SizeType span);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user