Commit 183655ee authored by Micha Mueller's avatar Micha Mueller
Browse files

Make _pendingTasks thread-safe

parent 0562b9b0
......@@ -23,6 +23,42 @@ Sensor::Sensor(const std::string name) :
_readingQueue = NULL;
}
Sensor::Sensor(const Sensor& other) :
_name(other._name),
_mqtt(other._mqtt),
_keepRunning(other._keepRunning),
_minValues(other._minValues),
_interval(other._interval),
_cacheInterval(other._cacheInterval),
_cacheSize(other._cacheSize),
_cacheIndex(other._cacheIndex),
_cache(other._cache),
_latestValue(other._latestValue),
_timer(other._timer),
_readingQueue(other._readingQueue) {
_pendingTasks.store(other._pendingTasks.load());
}
Sensor& Sensor::operator=(const Sensor& other) {
_name = other._name;
_mqtt = other._mqtt;
_keepRunning = other._keepRunning;
_minValues = other._minValues;
_interval = other._interval;
_cacheInterval = other._cacheInterval;
_cacheSize = other._cacheSize;
_cacheIndex = other._cacheIndex;
_pendingTasks.store(other._pendingTasks.load());
_cache = other._cache;
_latestValue.timestamp = other._latestValue.timestamp;
_latestValue.value = other._latestValue.value;
_timer = other._timer;
_readingQueue = other._readingQueue;
return *this;
}
Sensor::~Sensor() {
if(_cache) {
delete[] _cache;
......
......@@ -9,6 +9,7 @@
#define SENSOR_H_
#include <string>
#include <atomic>
#include <boost/asio.hpp>
#include <boost/lockfree/spsc_queue.hpp>
......@@ -28,6 +29,10 @@ class Sensor {
public:
Sensor(const std::string name);
Sensor(const Sensor& other);
Sensor& operator=(const Sensor& other);
virtual ~Sensor();
//non-overwriteable methods
......@@ -80,7 +85,7 @@ protected:
unsigned int _cacheInterval;
unsigned int _cacheSize;
unsigned int _cacheIndex;
unsigned int _pendingTasks;
std::atomic_uint _pendingTasks;
reading_t * _cache;
reading_t _latestValue;
boost::asio::deadline_timer* _timer;
......
......@@ -14,6 +14,35 @@ SensorGroup::SensorGroup(const std::string name) :
_timer = 0;
}
SensorGroup::SensorGroup(const SensorGroup& other) :
_name(other._name),
_keepRunning(other._keepRunning),
_minValues(other._minValues),
_interval(other._interval),
_cacheInterval(other._cacheInterval),
_cacheSize(other._cacheSize),
_cacheIndex(other._cacheIndex),
_sensors(other._sensors),
_timer(other._timer) {
_pendingTasks.store(other._pendingTasks.load());
}
SensorGroup& SensorGroup::operator=(const SensorGroup& other) {
_name = other._name;
_keepRunning = other._keepRunning;
_minValues = other._minValues;
_interval = other._interval;
_cacheInterval = other._cacheInterval;
_cacheSize = other._cacheSize;
_cacheIndex = other._cacheIndex;
_pendingTasks.store(other._pendingTasks.load());
_sensors = other._sensors;
_timer = other._timer;
return *this;
}
SensorGroup::~SensorGroup() {
if(_timer) {
delete _timer;
......
......@@ -10,6 +10,7 @@
#include <string>
#include <vector>
#include <atomic>
#include <boost/asio.hpp>
#include "Logging.h"
......@@ -18,6 +19,8 @@
class SensorGroup {
public:
SensorGroup(const std::string name);
SensorGroup(const SensorGroup& other);
SensorGroup& operator=(const SensorGroup& other);
virtual ~SensorGroup();
//non-overwriteable methods
......@@ -54,7 +57,7 @@ protected:
unsigned int _cacheInterval;
unsigned int _cacheSize;
unsigned int _cacheIndex;
unsigned int _pendingTasks;
std::atomic_uint _pendingTasks;
std::vector<Sensor*> _sensors;
boost::asio::deadline_timer* _timer;
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
......
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