Commit 632f1dc0 authored by Micha Mueller's avatar Micha Mueller
Browse files

Switch _timer to unique_ptr

parent 2ba7e24b
......@@ -9,6 +9,7 @@
#define SRC_SENSORINTERFACE_H_
#include <atomic>
#include <memory>
#include <boost/asio.hpp>
#include "Logging.h"
......@@ -22,9 +23,8 @@ public:
_cacheInterval(900000),
_cacheSize(1),
_cacheIndex(0),
_pendingTasks(0) {
_timer = NULL;
}
_pendingTasks(0),
_timer(nullptr) {}
SensorInterface(const SensorInterface& other) :
_keepRunning(other._keepRunning),
......@@ -33,15 +33,11 @@ public:
_cacheInterval(other._cacheInterval),
_cacheSize(other._cacheSize),
_cacheIndex(other._cacheIndex),
_timer(other._timer) {
_timer(nullptr) {
_pendingTasks.store(other._pendingTasks.load());
}
virtual ~SensorInterface() {
if (_timer) {
delete _timer;
}
}
virtual ~SensorInterface() {}
SensorInterface& operator=(const SensorInterface& other) {
_keepRunning = other._keepRunning;
......@@ -51,7 +47,7 @@ public:
_cacheSize = other._cacheSize;
_cacheIndex = other._cacheIndex;
_pendingTasks.store(other._pendingTasks.load());
_timer = other._timer;
_timer = nullptr;
return *this;
}
......@@ -76,9 +72,7 @@ public:
//can be overwritten
virtual void init(boost::asio::io_service& io) {
_cacheSize = _cacheInterval / _interval + 1;
if(!_timer) {
_timer = new boost::asio::deadline_timer(io, boost::posix_time::seconds(0));
}
_timer.reset(new boost::asio::deadline_timer(io, boost::posix_time::seconds(0)));
}
//have to be overwritten
......@@ -96,7 +90,7 @@ protected:
unsigned int _cacheSize;
unsigned int _cacheIndex;
std::atomic_uint _pendingTasks;
boost::asio::deadline_timer* _timer;
std::unique_ptr<boost::asio::deadline_timer> _timer;
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
};
......
......@@ -67,7 +67,7 @@ void BACnetSingleSensor::read() {
void BACnetSingleSensor::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer != NULL && _keepRunning) {
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_pendingTasks++;
......
......@@ -86,16 +86,14 @@ void IPMISingleSensor::readAsync() {
if (now >= _host->getDelayNextReadUntil()) {
read();
}
if (_timer != NULL && _keepRunning) {
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
while (next < _host->getDelayNextReadUntil()) {
next += MS_TO_NS(_interval);
}
_timer->expires_at(timestamp2ptime(next));
_pendingTasks++;
_timer->async_wait(
_host->getStrand()->wrap(
boost::bind(&IPMISingleSensor::readAsync, this)));
_timer->async_wait(_host->getStrand()->wrap(boost::bind(&IPMISingleSensor::readAsync, this)));
}
_pendingTasks--;
}
......
......@@ -67,7 +67,7 @@ void PDUSingleSensor::read() {
void PDUSingleSensor::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer != NULL && _keepRunning) {
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_pendingTasks++;
......
......@@ -40,9 +40,7 @@ PerfSensorGroup::~PerfSensorGroup() {
void PerfSensorGroup::init(boost::asio::io_service& io) {
_cacheSize = _cacheInterval / _interval + 1;
if (!_timer) {
_timer = new boost::asio::deadline_timer(io, boost::posix_time::seconds(0));
}
_timer.reset(new boost::asio::deadline_timer(io, boost::posix_time::seconds(0)));
std::size_t bufSize = _sensors.size() * 16 + 8;
if (!_buf) {
......@@ -167,7 +165,7 @@ void PerfSensorGroup::read() {
void PerfSensorGroup::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer != NULL && _keepRunning) {
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_pendingTasks++;
......
......@@ -105,7 +105,7 @@ void PerfSingleSensor::read() {
void PerfSingleSensor::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer != NULL && _keepRunning) {
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_pendingTasks++;
......
......@@ -65,7 +65,7 @@ void SNMPSingleSensor::read() {
void SNMPSingleSensor::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer != NULL && _keepRunning) {
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_pendingTasks++;
......
......@@ -88,7 +88,7 @@ void SysfsSingleSensor::read() {
void SysfsSingleSensor::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer != NULL && _keepRunning) {
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_pendingTasks++;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment