Skip to content

Commit

Permalink
fix(Datapoints): merge fix for potential deadlock from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
obiltschnig committed Feb 5, 2024
1 parent 3c66d49 commit de708ec
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions devices/Devices/include/IoT/Devices/DeviceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class DeviceImpl: public Super

void setProperty(const std::string& name, const Poco::Any& value)
{
Poco::Mutex::ScopedLock lock(_mutex);
ScopedLock lock(*this);

typename PropertyMap::const_iterator it = _properties.find(name);
if (it != _properties.end())
Expand All @@ -133,7 +133,7 @@ class DeviceImpl: public Super

Poco::Any getProperty(const std::string& name) const
{
Poco::Mutex::ScopedLock lock(_mutex);
ScopedLock lock(*this);

typename PropertyMap::const_iterator it = _properties.find(name);
if (it != _properties.end())
Expand All @@ -148,15 +148,15 @@ class DeviceImpl: public Super

bool hasProperty(const std::string& name) const
{
Poco::Mutex::ScopedLock lock(_mutex);
ScopedLock lock(*this);

typename PropertyMap::const_iterator it = _properties.find(name);
return it != _properties.end();
}

void setFeature(const std::string& name, bool enable)
{
Poco::Mutex::ScopedLock lock(_mutex);
ScopedLock lock(*this);

typename FeatureMap::const_iterator it = _features.find(name);
if (it != _features.end())
Expand All @@ -171,7 +171,7 @@ class DeviceImpl: public Super

bool getFeature(const std::string& name) const
{
Poco::Mutex::ScopedLock lock(_mutex);
ScopedLock lock(*this);

typename FeatureMap::const_iterator it = _features.find(name);
if (it != _features.end())
Expand All @@ -186,13 +186,50 @@ class DeviceImpl: public Super

bool hasFeature(const std::string& name) const
{
Poco::Mutex::ScopedLock lock(_mutex);
ScopedLock lock(*this);

typename FeatureMap::const_iterator it = _features.find(name);
return it != _features.end();
}

protected:
class ScopedLock
{
public:
explicit ScopedLock(const DeviceImpl& device): _device(device)
{
_device.lock();
}

~ScopedLock()
{
_device.unlock();
}

private:
const DeviceImpl& _device;

ScopedLock();
ScopedLock(const ScopedLock&);
ScopedLock& operator = (const ScopedLock&);
};

void lock() const
/// Locks the internal Mutex.
///
/// Should not be called directly, only via ScopedLock.
{
_mutex.lock();
}

void unlock() const
/// Unlocks the internal Mutex.
//
/// Should not be called directly, only via ScopedLock.
{
_mutex.unlock();
}

void addFeature(const std::string& name, FeatureGetter getter, FeatureSetter setter = 0)
/// Adds a feature to the map of supported features.
///
Expand Down Expand Up @@ -229,12 +266,14 @@ class DeviceImpl: public Super
PropertyGetter getter;
};

typedef std::map<std::string, Feature> FeatureMap;
typedef std::map<std::string, Property> PropertyMap;
using FeatureMap = std::map<std::string, Feature>;
using PropertyMap = std::map<std::string, Property>;

FeatureMap _features;
PropertyMap _properties;
mutable Poco::Mutex _mutex;

friend class ScopedLock;
};


Expand Down

0 comments on commit de708ec

Please sign in to comment.