#define ATAG "ATTRIBUTE_AUTOMATION"
#define RTAG "RESOURCE_AUTOMATION"
-#define SLEEP_FOR(X) if (X > 0) std::this_thread::sleep_for(std::chrono::milliseconds(X));
-
AttributeUpdateAutomation::AttributeUpdateAutomation(int id, SimulatorSingleResource *resource,
const SimulatorResourceModel::Attribute &attribute, AutomationType type, int interval,
updateCompleteCallback callback, std::function<void (const int)> finishedCallback)
}
}
+AttributeUpdateAutomation::~AttributeUpdateAutomation()
+{
+ if(!m_stopRequested)
+ m_thread->detach();
+}
+
void AttributeUpdateAutomation::start()
{
- m_thread = std::make_shared<std::thread>(&AttributeUpdateAutomation::updateAttribute, this);
- m_thread->detach();
+ m_thread.reset(new std::thread(&AttributeUpdateAutomation::updateAttribute, this));
}
void AttributeUpdateAutomation::stop()
{
- m_stopRequested = true;
+ {
+ std::lock_guard<std::mutex> lock(m_lock);
+ m_stopRequested = true;
+ }
+ m_condVariable.notify_one();
+ if(m_thread)
+ m_thread->join();
}
void AttributeUpdateAutomation::updateAttribute()
if (!resourceImpl)
return;
+ std::unique_lock<std::mutex> lock(m_lock);
+ std::chrono::system_clock::time_point now;
do
{
try
resourceImpl->notifyApp();
- SLEEP_FOR(m_updateInterval);
+ now = std::chrono::system_clock::now();
+ m_condVariable.wait_until(lock, now + std::chrono::milliseconds(m_updateInterval),
+ [this]{ return m_stopRequested; });
}
m_attributeGen.reset();
if (m_callback)
m_callback(m_resource->getURI(), m_id);
- if (m_finishedCallback)
- m_finishedCallback(m_id);
+ if (m_finishedCallback && !m_stopRequested)
+ {
+ std::thread notifyManager(m_finishedCallback, m_id);
+ notifyManager.detach();
+ }
}
ResourceUpdateAutomation::ResourceUpdateAutomation(int id, SimulatorSingleResource *resource,
m_finishedCallback(finishedCallback),
m_thread(nullptr) {}
+ResourceUpdateAutomation::~ResourceUpdateAutomation()
+{
+ if(!m_stopRequested)
+ m_thread->detach();
+}
+
void ResourceUpdateAutomation::start()
{
std::vector<SimulatorResourceModel::Attribute> attributes;
throw SimulatorException(SIMULATOR_ERROR, "Resource has zero attributes!");
}
- m_thread = std::make_shared<std::thread>(&ResourceUpdateAutomation::updateAttributes, this, attributes);
- m_thread->detach();
+ m_thread.reset(new std::thread(&ResourceUpdateAutomation::updateAttributes, this, attributes));
}
void ResourceUpdateAutomation::stop()
{
- m_stopRequested = true;
+ {
+ std::lock_guard<std::mutex> lock(m_lock);
+ m_stopRequested = true;
+ }
+ m_condVariable.notify_one();
+ if(m_thread)
+ m_thread->join();
}
void ResourceUpdateAutomation::updateAttributes(
if (!resourceImpl)
return;
+ std::unique_lock<std::mutex> lock(m_lock);
+ std::chrono::system_clock::time_point now;
do
{
AttributeCombinationGen attrCombGen(attributes);
}
resourceImpl->notifyApp();
- SLEEP_FOR(m_updateInterval);
+
+ now = std::chrono::system_clock::now();
+ m_condVariable.wait_until(lock, now + std::chrono::milliseconds(m_updateInterval),
+ [this]{ return m_stopRequested; });
+
}
}
while (!m_stopRequested && AutomationType::RECURRENT == m_type);
if (m_callback)
m_callback(m_resource->getURI(), m_id);
- if (m_finishedCallback)
- m_finishedCallback(m_id);
+ if (m_finishedCallback && !m_stopRequested)
+ {
+ std::thread notifyManager(m_finishedCallback, m_id);
+ notifyManager.detach();
+ }
}
#include "simulator_single_resource.h"
#include "attribute_generator.h"
#include <thread>
+#include <condition_variable>
+#include <atomic>
class AttributeUpdateAutomation
{
updateCompleteCallback callback,
std::function<void (const int)> finishedCallback);
+ ~AttributeUpdateAutomation();
+
void start();
void stop();
AttributeGenerator m_attributeGen;
updateCompleteCallback m_callback;
std::function<void (const int)> m_finishedCallback;
- std::shared_ptr<std::thread> m_thread;
+ std::unique_ptr<std::thread> m_thread;
+
+ std::mutex m_lock;
+ std::condition_variable m_condVariable;
};
typedef std::shared_ptr<AttributeUpdateAutomation> AttributeUpdateAutomationSP;
updateCompleteCallback callback,
std::function<void (const int)> finishedCallback);
+ ~ResourceUpdateAutomation();
+
void start();
void stop();
int m_updateInterval;
updateCompleteCallback m_callback;
std::function<void (const int)> m_finishedCallback;
- std::shared_ptr<std::thread> m_thread;
+ std::unique_ptr<std::thread> m_thread;
+
+ std::mutex m_lock;
+ std::condition_variable m_condVariable;
};
typedef std::shared_ptr<ResourceUpdateAutomation> ResourceUpdateAutomationSP;