From 6d5a606d7f9e2bd66b2ee75cc126c453f1bbdea6 Mon Sep 17 00:00:00 2001 From: G S Senthil Kumar Date: Tue, 29 Dec 2015 19:20:11 +0530 Subject: [PATCH] [IoTivity Simulator]Resolved JIRA issues IOT-893 and IOT-870. IOT-893 is about the time delay after stopping the automation. IOT-870 is about the server crash while notifying the observers. Updated the code to allow errors when notifying the observers to be propagated till application. Managing thread objects using shared pointers and detaching them for de-allocation of thread resources. Change-Id: I06e33926a79fd241bf4089b8c4760742edab8290 Signed-off-by: G S Senthil Kumar Reviewed-on: https://gerrit.iotivity.org/gerrit/4721 Reviewed-by: Radha Bhavani Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- .../src/server/resource_update_automation.cpp | 31 +++++++++++++--------- .../src/server/resource_update_automation.h | 4 +-- .../src/server/resource_update_automation_mngr.cpp | 2 -- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/service/simulator/src/server/resource_update_automation.cpp b/service/simulator/src/server/resource_update_automation.cpp index 6c6f863..07ea8de 100644 --- a/service/simulator/src/server/resource_update_automation.cpp +++ b/service/simulator/src/server/resource_update_automation.cpp @@ -51,14 +51,13 @@ AttributeUpdateAutomation::AttributeUpdateAutomation(int id, SimulatorSingleReso void AttributeUpdateAutomation::start() { - m_thread = new std::thread(&AttributeUpdateAutomation::updateAttribute, this); + m_thread = std::make_shared(&AttributeUpdateAutomation::updateAttribute, this); + m_thread->detach(); } void AttributeUpdateAutomation::stop() { m_stopRequested = true; - if (m_thread) - m_thread->join(); } void AttributeUpdateAutomation::updateAttribute() @@ -76,11 +75,16 @@ void AttributeUpdateAutomation::updateAttribute() SimulatorResourceModel::Attribute attribute; while (!m_stopRequested && true == m_attributeGen.next(attribute)) { - if (false == m_resource->updateAttributeValue(attribute)) + try { - OC_LOG_V(ERROR, ATAG, "Failed to update the attribute![%s]", attribute.getName().c_str()); - continue; + if (false == m_resource->updateAttributeValue(attribute)) + { + OC_LOG_V(ERROR, ATAG, "Failed to update the attribute![%s]", attribute.getName().c_str()); + continue; + } } + catch(SimulatorException &e) {} + resourceImpl->notifyApp(); SLEEP_FOR(m_updateInterval); @@ -105,7 +109,7 @@ void AttributeUpdateAutomation::updateAttribute() if (m_callback) m_callback(m_resource->getURI(), m_id); - if (m_finishedCallback && !m_stopRequested) + if (m_finishedCallback) m_finishedCallback(m_id); } @@ -135,14 +139,13 @@ void ResourceUpdateAutomation::start() throw SimulatorException(SIMULATOR_ERROR, "Resource has zero attributes!"); } - m_thread = new std::thread(&ResourceUpdateAutomation::updateAttributes, this, attributes); + m_thread = std::make_shared(&ResourceUpdateAutomation::updateAttributes, this, attributes); + m_thread->detach(); } void ResourceUpdateAutomation::stop() { m_stopRequested = true; - if (m_thread) - m_thread->join(); } void ResourceUpdateAutomation::updateAttributes( @@ -162,7 +165,11 @@ void ResourceUpdateAutomation::updateAttributes( { for (auto &attributeEntry : resModel.getAttributes()) { - resourceImpl->updateAttributeValue(attributeEntry.second); + try + { + resourceImpl->updateAttributeValue(attributeEntry.second); + } + catch(SimulatorException &e) {} } resourceImpl->notifyApp(); @@ -180,7 +187,7 @@ void ResourceUpdateAutomation::updateAttributes( if (m_callback) m_callback(m_resource->getURI(), m_id); - if (m_finishedCallback && !m_stopRequested) + if (m_finishedCallback) m_finishedCallback(m_id); } diff --git a/service/simulator/src/server/resource_update_automation.h b/service/simulator/src/server/resource_update_automation.h index a3dc6d1..54263bf 100644 --- a/service/simulator/src/server/resource_update_automation.h +++ b/service/simulator/src/server/resource_update_automation.h @@ -50,7 +50,7 @@ class AttributeUpdateAutomation AttributeGenerator m_attributeGen; updateCompleteCallback m_callback; std::function m_finishedCallback; - std::thread *m_thread; + std::shared_ptr m_thread; }; typedef std::shared_ptr AttributeUpdateAutomationSP; @@ -77,7 +77,7 @@ class ResourceUpdateAutomation int m_updateInterval; updateCompleteCallback m_callback; std::function m_finishedCallback; - std::thread *m_thread; + std::shared_ptr m_thread; }; typedef std::shared_ptr ResourceUpdateAutomationSP; diff --git a/service/simulator/src/server/resource_update_automation_mngr.cpp b/service/simulator/src/server/resource_update_automation_mngr.cpp index 1b52c70..972b176 100644 --- a/service/simulator/src/server/resource_update_automation_mngr.cpp +++ b/service/simulator/src/server/resource_update_automation_mngr.cpp @@ -112,13 +112,11 @@ void UpdateAutomationMngr::stop(int id) if (m_resourceUpdationList.end() != m_resourceUpdationList.find(id)) { m_resourceUpdationList[id]->stop(); - m_resourceUpdationList.erase(m_resourceUpdationList.find(id)); return; } else if (m_attrUpdationList.end() != m_attrUpdationList.find(id)) { m_attrUpdationList[id]->stop(); - m_attrUpdationList.erase(m_attrUpdationList.find(id)); return; } } -- 2.7.4