From 7e997c46af6389bbfd78c4599092a0ba186e3713 Mon Sep 17 00:00:00 2001 From: Frank Date: Mon, 18 Dec 2017 11:11:30 -0800 Subject: protect pt_ from concurrent access --- .../policy/policy_external/src/cache_manager.cc | 77 ++++++++++++++++++---- .../policy_regular/include/policy/cache_manager.h | 2 +- .../policy/policy_regular/src/cache_manager.cc | 54 ++++++++++++--- 3 files changed, 111 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/components/policy/policy_external/src/cache_manager.cc b/src/components/policy/policy_external/src/cache_manager.cc index b5e44014dd..59e9996a61 100644 --- a/src/components/policy/policy_external/src/cache_manager.cc +++ b/src/components/policy/policy_external/src/cache_manager.cc @@ -250,6 +250,7 @@ policy_table::MessageString FindLanguage( CacheManager::CacheManager() : CacheManagerInterface() + , cache_lock_(true) , pt_(new policy_table::Table) , backup_(new SQLPTExtRepresentation()) , update_required(false) { @@ -258,6 +259,7 @@ CacheManager::CacheManager() CacheManager::CacheManager(bool in_memory) : CacheManagerInterface() + , cache_lock_(true) , pt_(new policy_table::Table) , backup_(new SQLPTExtRepresentation(in_memory)) , update_required(false) { @@ -277,6 +279,7 @@ ConsentPriorityType CacheManager::GetConsentsPriority( LOG4CXX_AUTO_TRACE(logger_); ConsentPriorityType prio_type = ConsentPriorityType::kExternalConsentPrio; CACHE_MANAGER_CHECK(prio_type); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::DeviceData::const_iterator dev_params_iter = pt_->policy_table.device_data->find(device_id); @@ -304,11 +307,13 @@ ConsentPriorityType CacheManager::GetConsentsPriority( } const policy_table::Strings& CacheManager::GetGroups(const PTString& app_id) { + sync_primitives::AutoLock auto_lock(cache_lock_); return pt_->policy_table.app_policies_section.apps[app_id].groups; } bool CacheManager::CanAppKeepContext(const std::string& app_id) const { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); bool result = false; if (kDeviceId == app_id) { result = pt_->policy_table.app_policies_section.device.keep_context; @@ -320,6 +325,7 @@ bool CacheManager::CanAppKeepContext(const std::string& app_id) const { uint32_t CacheManager::HeartBeatTimeout(const std::string& app_id) const { CACHE_MANAGER_CHECK(0); + sync_primitives::AutoLock auto_lock(cache_lock_); uint32_t result = 0; if (!IsApplicationRepresented(app_id)) { return result; @@ -336,6 +342,7 @@ uint32_t CacheManager::HeartBeatTimeout(const std::string& app_id) const { const policy_table::AppHMITypes* CacheManager::GetHMITypes( const std::string& app_id) { + sync_primitives::AutoLock auto_lock(cache_lock_); const policy_table::ApplicationPolicies& apps = pt_->policy_table.app_policies_section.apps; policy_table::ApplicationPolicies::const_iterator i = apps.find(app_id); @@ -365,6 +372,7 @@ int32_t CacheManager::GenerateHash(const std::string& str_to_hash) { bool CacheManager::CanAppStealFocus(const std::string& app_id) const { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); bool result = false; if (kDeviceId == app_id) { result = pt_->policy_table.app_policies_section.device.steal_focus; @@ -377,6 +385,7 @@ bool CacheManager::CanAppStealFocus(const std::string& app_id) const { bool CacheManager::GetDefaultHMI(const std::string& app_id, std::string& default_hmi) const { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); bool result = false; default_hmi.clear(); if (kDeviceId == app_id) { @@ -412,6 +421,7 @@ bool CacheManager::GetUserPermissionsForDevice( StringArray& disallowed_groups) const { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::DeviceData& device_data = *pt_->policy_table.device_data; if (device_data.end() == device_data.find(device_id)) { LOG4CXX_ERROR(logger_, @@ -444,6 +454,8 @@ void CacheManager::GetAllAppGroups(const std::string& app_id, FunctionalGroupIDs& all_group_ids) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); + if (kDeviceId == app_id) { policy_table::DevicePolicy& device = pt_->policy_table.app_policies_section.device; @@ -481,6 +493,7 @@ void CacheManager::GetPreConsentedGroups( const std::string& app_id, FunctionalGroupIDs& preconsented_groups) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); if (kDeviceId == app_id) { policy_table::DevicePolicy& device = pt_->policy_table.app_policies_section.device; @@ -520,6 +533,7 @@ void CacheManager::GetConsentedGroups(const std::string& device_id, FunctionalGroupIDs& disallowed_groups) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::DeviceData::iterator dev_params_iter = pt_->policy_table.device_data->find(device_id); @@ -559,10 +573,11 @@ void CacheManager::GetUnconsentedGroups( if (!IsApplicationRepresented(policy_app_id)) { LOG4CXX_WARN(logger_, "The application with app_id: " << policy_app_id - << " is not reresented"); + << " is not represented"); return; } + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::Strings::iterator iter_groups; policy_table::Strings::iterator iter_groups_end; if (kDeviceId == policy_app_id) { @@ -608,6 +623,7 @@ void CacheManager::GetUnconsentedGroups( void CacheManager::RemoveAppConsentForGroup(const std::string& app_id, const std::string& group_name) { CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::DeviceData::iterator device_iter = pt_->policy_table.device_data->begin(); policy_table::DeviceData::iterator device_iter_end = @@ -658,6 +674,7 @@ void CacheManager::ProcessUpdate( } } + sync_primitives::AutoLock lock(cache_lock_); const RequestTypes merged_pt_request_types = update_request_types ? new_request_types @@ -716,6 +733,7 @@ void CacheManager::GetHMIAppTypeAfterUpdate( std::map& app_hmi_types) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ApplicationPolicies::const_iterator policy_iter_begin = pt_->policy_table.app_policies_section.apps.begin(); policy_table::ApplicationPolicies::const_iterator policy_iter_end = @@ -739,6 +757,7 @@ void CacheManager::GetHMIAppTypeAfterUpdate( bool CacheManager::AppHasHMIType(const std::string& application_id, policy_table::AppHMIType hmi_type) const { + sync_primitives::AutoLock auto_lock(cache_lock_); const policy_table::ApplicationPolicies& policies = pt_->policy_table.app_policies_section.apps; @@ -798,6 +817,7 @@ bool CacheManager::GetDeviceGroupsFromPolicies( policy_table::Strings& preconsented_groups) const { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); groups = pt_->policy_table.app_policies_section.device.groups; preconsented_groups = *(pt_->policy_table.app_policies_section.device).preconsented_groups; @@ -1057,6 +1077,7 @@ bool CacheManager::ReactOnUserDevConsentForApp(const std::string& app_id, void CacheManager::GetGroupNameByHashID(const int32_t group_id, std::string& group_name) { CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::FunctionalGroupings::const_iterator fg_iter = pt_->policy_table.functional_groupings.begin(); policy_table::FunctionalGroupings::const_iterator fg_iter_end = @@ -1125,6 +1146,7 @@ void CacheManager::SaveUpdateRequired(bool status) { bool CacheManager::IsApplicationRevoked(const std::string& app_id) const { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); if (!IsApplicationRepresented(app_id)) { return false; } @@ -1139,6 +1161,7 @@ void CacheManager::CheckPermissions(const PTString& app_id, CheckPermissionResult& result) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); if (!IsApplicationRepresented(app_id)) { LOG4CXX_ERROR( @@ -1192,11 +1215,13 @@ void CacheManager::CheckPermissions(const PTString& app_id, bool CacheManager::IsPTPreloaded() { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); return *pt_->policy_table.module_config.preloaded_pt; } int CacheManager::IgnitionCyclesBeforeExchange() { CACHE_MANAGER_CHECK(0); + sync_primitives::AutoLock auto_lock(cache_lock_); const uint8_t limit = std::max( static_cast( pt_->policy_table.module_config.exchange_after_x_ignition_cycles), @@ -1218,6 +1243,7 @@ int CacheManager::IgnitionCyclesBeforeExchange() { int CacheManager::KilometersBeforeExchange(int current) { CACHE_MANAGER_CHECK(0); + sync_primitives::AutoLock auto_lock(cache_lock_); const int limit = std::max(static_cast( pt_->policy_table.module_config.exchange_after_x_kilometers), @@ -1238,6 +1264,7 @@ int CacheManager::KilometersBeforeExchange(int current) { bool CacheManager::SetCountersPassedForSuccessfulUpdate( policy::Counters counter, int value) { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); switch (counter) { case KILOMETERS: *pt_->policy_table.module_meta->pt_exchanged_at_odometer_x = value; @@ -1263,6 +1290,7 @@ bool CacheManager::SetCountersPassedForSuccessfulUpdate( int CacheManager::DaysBeforeExchange(uint16_t current) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK(0); + sync_primitives::AutoLock auto_lock(cache_lock_); const rpc::Optional >& days_after_epoch = (pt_->policy_table.module_meta->pt_exchanged_x_days_after_epoch); @@ -1286,6 +1314,7 @@ int CacheManager::DaysBeforeExchange(uint16_t current) { void CacheManager::IncrementIgnitionCycles() { CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); const int ign_val = static_cast( *pt_->policy_table.module_meta->ignition_cycles_since_last_exchange); (*pt_->policy_table.module_meta->ignition_cycles_since_last_exchange) = @@ -1296,18 +1325,21 @@ void CacheManager::IncrementIgnitionCycles() { void CacheManager::ResetIgnitionCycles() { CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); (*pt_->policy_table.module_meta->ignition_cycles_since_last_exchange) = 0; Backup(); } int CacheManager::TimeoutResponse() { CACHE_MANAGER_CHECK(0); + sync_primitives::AutoLock auto_lock(cache_lock_); return pt_->policy_table.module_config.timeout_after_x_seconds * date_time::DateTime::MILLISECONDS_IN_SECOND; } bool CacheManager::SecondsBetweenRetries(std::vector& seconds) { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); rpc::policy_table_interface_base::SecondsBetweenRetries::iterator iter = pt_->policy_table.module_config.seconds_between_retries.begin(); rpc::policy_table_interface_base::SecondsBetweenRetries::iterator iter_end = @@ -1324,6 +1356,7 @@ bool CacheManager::SecondsBetweenRetries(std::vector& seconds) { const policy::VehicleInfo CacheManager::GetVehicleInfo() const { CACHE_MANAGER_CHECK(VehicleInfo()); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ModuleConfig& module_config = pt_->policy_table.module_config; VehicleInfo vehicle_info; vehicle_info.vehicle_make = *module_config.vehicle_make; @@ -1347,6 +1380,7 @@ std::vector CacheManager::GetUserFriendlyMsg( std::vector::const_iterator it = msg_codes.begin(); std::vector::const_iterator it_end = msg_codes.end(); + sync_primitives::AutoLock auto_lock(cache_lock_); for (; it != it_end; ++it) { policy_table::MessageLanguages msg_languages = (*pt_->policy_table.consumer_friendly_messages->messages)[*it]; @@ -1410,6 +1444,7 @@ void CacheManager::GetUpdateUrls(const std::string& service_type, LOG4CXX_DEBUG(logger_, "Search service value is: " << service_type); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ServiceEndpoints::const_iterator iter = pt_->policy_table.module_config.endpoints.find(service_type); @@ -1442,6 +1477,7 @@ CacheManager::GetNotificationsNumber(const std::string& priority) { CACHE_MANAGER_CHECK(0); typedef rpc::policy_table_interface_base::NumberOfNotificationsPerMinute NNPM; + sync_primitives::AutoLock auto_lock(cache_lock_); const NNPM& nnpm = pt_->policy_table.module_config.notifications_per_minute_by_priority; @@ -1455,6 +1491,7 @@ CacheManager::GetNotificationsNumber(const std::string& priority) { bool CacheManager::GetPriority(const std::string& policy_app_id, std::string& priority) const { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); if (kDeviceId == policy_app_id) { priority = EnumToJsonString( pt_->policy_table.app_policies_section.device.priority); @@ -1593,10 +1630,8 @@ void CacheManager::PersistData() { LOG4CXX_AUTO_TRACE(logger_); if (backup_.valid()) { if (pt_.valid()) { - cache_lock_.Acquire(); - policy_table::Table copy_pt(*pt_); - cache_lock_.Release(); - + // Comma expression is used to hold the lock only during the constructor call + policy_table::Table copy_pt((sync_primitives::AutoLock(cache_lock_), *pt_)); backup_->Save(copy_pt); backup_->SaveUpdateRequired(update_required); @@ -1716,8 +1751,8 @@ bool CacheManager::IsPermissionsCalculated(const std::string& device_id, utils::SharedPtr CacheManager::GenerateSnapshot() { CACHE_MANAGER_CHECK(snapshot_); - sync_primitives::AutoLock lock(cache_lock_); snapshot_ = new policy_table::Table(); + sync_primitives::AutoLock auto_lock(cache_lock_); snapshot_->policy_table = pt_->policy_table; snapshot_->SetPolicyTableType(policy_table::PT_SNAPSHOT); @@ -1731,6 +1766,7 @@ bool CacheManager::GetInitialAppData(const std::string& app_id, StringArray& app_hmi_types) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ApplicationPolicies::const_iterator policy_iter = pt_->policy_table.app_policies_section.apps.find(app_id); @@ -1753,6 +1789,7 @@ bool CacheManager::GetFunctionalGroupings( policy_table::FunctionalGroupings& groups) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); const policy_table::FunctionalGroupings& f_groupings = pt_->policy_table.functional_groupings; @@ -1774,6 +1811,7 @@ int CacheManager::CountUnconsentedGroups(const std::string& policy_app_id, return 0; } + sync_primitives::AutoLock lock(cache_lock_); policy_table::FunctionalGroupings::const_iterator groups_iter_end = pt_->policy_table.functional_groupings.end(); @@ -1864,6 +1902,8 @@ bool CacheManager::SetMetaInfo(const std::string& ccpu_version, const std::string& wers_country_code, const std::string& language) { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); + *pt_->policy_table.module_meta->ccpu_version = ccpu_version; *pt_->policy_table.module_meta->wers_country_code = wers_country_code; *pt_->policy_table.module_meta->language = language; @@ -1879,6 +1919,7 @@ bool CacheManager::SetMetaInfo(const std::string& ccpu_version, bool CacheManager::IsMetaInfoPresent() const { CACHE_MANAGER_CHECK(false); bool result = true; + sync_primitives::AutoLock lock(cache_lock_); result = NULL != pt_->policy_table.module_meta->ccpu_version && NULL != pt_->policy_table.module_meta->wers_country_code && NULL != pt_->policy_table.module_meta->language; @@ -1887,6 +1928,7 @@ bool CacheManager::IsMetaInfoPresent() const { bool CacheManager::SetSystemLanguage(const std::string& language) { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock lock(cache_lock_); *pt_->policy_table.module_meta->language = language; Backup(); return true; @@ -1895,6 +1937,7 @@ bool CacheManager::SetSystemLanguage(const std::string& language) { bool CacheManager::GetFunctionalGroupNames(FunctionalGroupNames& names) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); rpc::policy_table_interface_base::FunctionalGroupings::iterator iter = pt_->policy_table.functional_groupings.begin(); rpc::policy_table_interface_base::FunctionalGroupings::iterator iter_end = @@ -2084,6 +2127,7 @@ bool CacheManager::SetDefaultPolicy(const std::string& app_id) { bool CacheManager::IsDefaultPolicy(const std::string& app_id) const { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); const bool result = IsApplicationRepresented(app_id) && policy::kDefaultId == @@ -2094,7 +2138,7 @@ bool CacheManager::IsDefaultPolicy(const std::string& app_id) const { bool CacheManager::SetIsDefault(const std::string& app_id) { CACHE_MANAGER_CHECK(false); - + sync_primitives::AutoLock auto_lock(cache_lock_); if (IsApplicationRepresented(app_id)) { pt_->policy_table.app_policies_section.apps[app_id].set_to_string( kDefaultId); @@ -2137,6 +2181,7 @@ bool CacheManager::SetPredataPolicy(const std::string& app_id) { } bool CacheManager::IsPredataPolicy(const std::string& app_id) const { + sync_primitives::AutoLock auto_lock(cache_lock_); if (!IsApplicationRepresented(app_id)) { return false; } @@ -2146,6 +2191,7 @@ bool CacheManager::IsPredataPolicy(const std::string& app_id) const { bool CacheManager::SetUnpairedDevice(const std::string& device_id, bool unpaired) { + sync_primitives::AutoLock auto_lock(cache_lock_); const bool result = pt_->policy_table.device_data->end() != pt_->policy_table.device_data->find(device_id); if (!result) { @@ -2169,15 +2215,17 @@ bool CacheManager::SetUnpairedDevice(const std::string& device_id, bool CacheManager::SetVINValue(const std::string& value) { CACHE_MANAGER_CHECK(false); - cache_lock_.Acquire(); + { + sync_primitives::AutoLock lock(cache_lock_); *pt_->policy_table.module_meta->vin = value; - cache_lock_.Release(); + } Backup(); return true; } bool CacheManager::IsApplicationRepresented(const std::string& app_id) const { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ApplicationPolicies::const_iterator iter = pt_->policy_table.app_policies_section.apps.find(app_id); return pt_->policy_table.app_policies_section.apps.end() != iter; @@ -2303,6 +2351,7 @@ bool CacheManager::LoadFromFile(const std::string& file_name, LOG4CXX_DEBUG(logger_, "Start verification of policy table loaded from file."); + sync_primitives::AutoLock locker(cache_lock_); table = policy_table::Table(&value); #ifdef ENABLE_LOG @@ -2346,6 +2395,7 @@ void CacheManager::GetAppRequestTypes( std::vector& request_types) const { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); if (kDeviceId == policy_app_id) { LOG4CXX_DEBUG(logger_, "Request types not applicable for app_id " << kDeviceId); @@ -2371,6 +2421,7 @@ void CacheManager::GetAppRequestTypes( const MetaInfo CacheManager::GetMetaInfo() const { LOG4CXX_AUTO_TRACE(logger_); + sync_primitives::AutoLock auto_lock(cache_lock_); MetaInfo meta_info; meta_info.ccpu_version = *pt_->policy_table.module_meta->ccpu_version; meta_info.wers_country_code = @@ -2381,6 +2432,7 @@ const MetaInfo CacheManager::GetMetaInfo() const { std::string CacheManager::GetCertificate() const { CACHE_MANAGER_CHECK(std::string("")); + sync_primitives::AutoLock auto_lock(cache_lock_); if (pt_->policy_table.module_config.certificate.is_initialized()) { return *pt_->policy_table.module_config.certificate; } @@ -2656,9 +2708,10 @@ void CacheManager::BackgroundBackuper::threadMain() { LOG4CXX_AUTO_TRACE(logger_); sync_primitives::AutoLock lock(need_backup_lock_); while (!stop_flag_) { - need_backup_lock_.Release(); - InternalBackup(); - need_backup_lock_.Acquire(); + { + sync_primitives::AutoUnlock need_backup_lock(need_backup_lock_); + InternalBackup(); + } if (new_data_available_ || stop_flag_) { continue; } diff --git a/src/components/policy/policy_regular/include/policy/cache_manager.h b/src/components/policy/policy_regular/include/policy/cache_manager.h index 44827e2602..934d274a28 100644 --- a/src/components/policy/policy_regular/include/policy/cache_manager.h +++ b/src/components/policy/policy_regular/include/policy/cache_manager.h @@ -747,7 +747,7 @@ class CacheManager : public CacheManagerInterface { typedef std::set UnpairedDevices; UnpairedDevices is_unpaired_; - sync_primitives::Lock cache_lock_; + mutable sync_primitives::Lock cache_lock_; sync_primitives::Lock unpaired_lock_; typedef std::map AppCalculatedPermissions; diff --git a/src/components/policy/policy_regular/src/cache_manager.cc b/src/components/policy/policy_regular/src/cache_manager.cc index 98652656c3..c8b6073b0b 100644 --- a/src/components/policy/policy_regular/src/cache_manager.cc +++ b/src/components/policy/policy_regular/src/cache_manager.cc @@ -85,6 +85,7 @@ struct LanguageFinder { CacheManager::CacheManager() : CacheManagerInterface() + , cache_lock_(true) , pt_(new policy_table::Table) , backup_(new SQLPTRepresentation()) , update_required(false) { @@ -103,6 +104,7 @@ CacheManager::~CacheManager() { } const policy_table::Strings& CacheManager::GetGroups(const PTString& app_id) { + sync_primitives::AutoLock auto_lock(cache_lock_); return pt_->policy_table.app_policies_section.apps[app_id].groups; } @@ -114,6 +116,7 @@ bool CacheManager::CanAppKeepContext(const std::string& app_id) const { uint32_t CacheManager::HeartBeatTimeout(const std::string& app_id) const { CACHE_MANAGER_CHECK(0); + sync_primitives::AutoLock auto_lock(cache_lock_); uint32_t result = 0; if (!AppExists(app_id)) { return result; @@ -130,6 +133,7 @@ uint32_t CacheManager::HeartBeatTimeout(const std::string& app_id) const { const policy_table::AppHMITypes* CacheManager::GetHMITypes( const std::string& app_id) { + sync_primitives::AutoLock auto_lock(cache_lock_); const policy_table::ApplicationPolicies& apps = pt_->policy_table.app_policies_section.apps; policy_table::ApplicationPolicies::const_iterator i = apps.find(app_id); @@ -180,6 +184,7 @@ void CacheManager::GetAllAppGroups(const std::string& app_id, return; } + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ApplicationPolicies::const_iterator app_params_iter = pt_->policy_table.app_policies_section.apps.find(app_id); @@ -265,6 +270,7 @@ void CacheManager::GetHMIAppTypeAfterUpdate( std::map& app_hmi_types) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ApplicationPolicies::const_iterator policy_iter_begin = pt_->policy_table.app_policies_section.apps.begin(); policy_table::ApplicationPolicies::const_iterator policy_iter_end = @@ -288,6 +294,7 @@ void CacheManager::GetHMIAppTypeAfterUpdate( bool CacheManager::AppHasHMIType(const std::string& application_id, policy_table::AppHMIType hmi_type) const { + sync_primitives::AutoLock auto_lock(cache_lock_); const policy_table::ApplicationPolicies& policies = pt_->policy_table.app_policies_section.apps; @@ -399,6 +406,7 @@ bool CacheManager::ReactOnUserDevConsentForApp(const std::string& app_id, void CacheManager::GetGroupNameByHashID(const int32_t group_id, std::string& group_name) { CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::FunctionalGroupings::const_iterator fg_iter = pt_->policy_table.functional_groupings.begin(); policy_table::FunctionalGroupings::const_iterator fg_iter_end = @@ -432,6 +440,7 @@ void CacheManager::SaveUpdateRequired(bool status) { bool CacheManager::IsApplicationRevoked(const std::string& app_id) const { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); bool is_revoked = false; if (pt_->policy_table.app_policies_section.apps.end() != pt_->policy_table.app_policies_section.apps.find(app_id)) { @@ -447,6 +456,7 @@ void CacheManager::CheckPermissions(const policy_table::Strings& groups, CheckPermissionResult& result) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::Strings::const_iterator app_groups_iter = groups.begin(); policy_table::Strings::const_iterator app_groups_iter_end = groups.end(); @@ -491,11 +501,13 @@ void CacheManager::CheckPermissions(const policy_table::Strings& groups, bool CacheManager::IsPTPreloaded() { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); return *pt_->policy_table.module_config.preloaded_pt; } int CacheManager::IgnitionCyclesBeforeExchange() { CACHE_MANAGER_CHECK(0); + sync_primitives::AutoLock auto_lock(cache_lock_); const uint8_t limit = std::max( static_cast( pt_->policy_table.module_config.exchange_after_x_ignition_cycles), @@ -515,6 +527,7 @@ int CacheManager::IgnitionCyclesBeforeExchange() { int CacheManager::KilometersBeforeExchange(int current) { CACHE_MANAGER_CHECK(0); + sync_primitives::AutoLock auto_lock(cache_lock_); const int limit = std::max(static_cast( pt_->policy_table.module_config.exchange_after_x_kilometers), @@ -535,6 +548,7 @@ int CacheManager::KilometersBeforeExchange(int current) { bool CacheManager::SetCountersPassedForSuccessfulUpdate( policy::Counters counter, int value) { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); switch (counter) { case KILOMETERS: *pt_->policy_table.module_meta->pt_exchanged_at_odometer_x = value; @@ -560,6 +574,7 @@ bool CacheManager::SetCountersPassedForSuccessfulUpdate( int CacheManager::DaysBeforeExchange(int current) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK(0); + sync_primitives::AutoLock auto_lock(cache_lock_); const rpc::Optional >& days_after_epoch = (pt_->policy_table.module_meta->pt_exchanged_x_days_after_epoch); @@ -583,6 +598,7 @@ int CacheManager::DaysBeforeExchange(int current) { void CacheManager::IncrementIgnitionCycles() { CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); const int ign_val = static_cast( *pt_->policy_table.module_meta->ignition_cycles_since_last_exchange); (*pt_->policy_table.module_meta->ignition_cycles_since_last_exchange) = @@ -593,18 +609,21 @@ void CacheManager::IncrementIgnitionCycles() { void CacheManager::ResetIgnitionCycles() { CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); (*pt_->policy_table.module_meta->ignition_cycles_since_last_exchange) = 0; Backup(); } int CacheManager::TimeoutResponse() { CACHE_MANAGER_CHECK(0); + sync_primitives::AutoLock auto_lock(cache_lock_); return pt_->policy_table.module_config.timeout_after_x_seconds * date_time::DateTime::MILLISECONDS_IN_SECOND; } bool CacheManager::SecondsBetweenRetries(std::vector& seconds) { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); rpc::policy_table_interface_base::SecondsBetweenRetries::iterator iter = pt_->policy_table.module_config.seconds_between_retries.begin(); rpc::policy_table_interface_base::SecondsBetweenRetries::iterator iter_end = @@ -621,6 +640,7 @@ bool CacheManager::SecondsBetweenRetries(std::vector& seconds) { const policy::VehicleInfo CacheManager::GetVehicleInfo() const { CACHE_MANAGER_CHECK(VehicleInfo()); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ModuleConfig& module_config = pt_->policy_table.module_config; VehicleInfo vehicle_info; vehicle_info.vehicle_make = *module_config.vehicle_make; @@ -642,6 +662,7 @@ std::vector CacheManager::GetUserFriendlyMsg( std::vector::const_iterator it = msg_codes.begin(); std::vector::const_iterator it_end = msg_codes.end(); + sync_primitives::AutoLock auto_lock(cache_lock_); for (; it != it_end; ++it) { policy_table::MessageLanguages msg_languages = (*pt_->policy_table.consumer_friendly_messages->messages)[*it]; @@ -701,6 +722,7 @@ void CacheManager::GetUpdateUrls(const std::string& service_type, LOG4CXX_DEBUG(logger_, "Search service value is: " << service_type); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ServiceEndpoints::const_iterator iter = pt_->policy_table.module_config.endpoints.find(service_type); @@ -733,6 +755,7 @@ CacheManager::GetNotificationsNumber(const std::string& priority) { CACHE_MANAGER_CHECK(0); typedef rpc::policy_table_interface_base::NumberOfNotificationsPerMinute NNPM; + sync_primitives::AutoLock auto_lock(cache_lock_); const NNPM& nnpm = pt_->policy_table.module_config.notifications_per_minute_by_priority; @@ -746,6 +769,7 @@ CacheManager::GetNotificationsNumber(const std::string& priority) { bool CacheManager::GetPriority(const std::string& policy_app_id, std::string& priority) const { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); if (kDeviceId == policy_app_id) { priority = EnumToJsonString( pt_->policy_table.app_policies_section.device.priority); @@ -884,9 +908,8 @@ void CacheManager::PersistData() { LOG4CXX_AUTO_TRACE(logger_); if (backup_.valid()) { if (pt_.valid()) { - cache_lock_.Acquire(); - policy_table::Table copy_pt(*pt_); - cache_lock_.Release(); + // Comma expression is used to hold the lock only during the constructor call + policy_table::Table copy_pt((sync_primitives::AutoLock(cache_lock_), *pt_)); backup_->Save(copy_pt); backup_->SaveUpdateRequired(update_required); @@ -978,12 +1001,12 @@ bool CacheManager::IsPermissionsCalculated(const std::string& device_id, utils::SharedPtr CacheManager::GenerateSnapshot() { CACHE_MANAGER_CHECK(snapshot_); - sync_primitives::AutoLock lock(cache_lock_); snapshot_ = new policy_table::Table(); // Copy all members of policy table except messages in consumer friendly // messages + sync_primitives::AutoLock auto_lock(cache_lock_); snapshot_->policy_table.app_policies_section = pt_->policy_table.app_policies_section; snapshot_->policy_table.functional_groupings = @@ -1010,6 +1033,7 @@ bool CacheManager::GetInitialAppData(const std::string& app_id, StringArray& app_hmi_types) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ApplicationPolicies::const_iterator policy_iter = pt_->policy_table.app_policies_section.apps.find(app_id); @@ -1032,6 +1056,7 @@ bool CacheManager::GetFunctionalGroupings( policy_table::FunctionalGroupings& groups) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); const policy_table::FunctionalGroupings& f_groupings = pt_->policy_table.functional_groupings; @@ -1052,6 +1077,7 @@ bool CacheManager::SetMetaInfo(const std::string& ccpu_version, const std::string& wers_country_code, const std::string& language) { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); // We have to set preloaded flag as false in policy table on any response // of GetSystemInfo (SDLAQ-CRS-2365) @@ -1076,6 +1102,7 @@ bool CacheManager::SetSystemLanguage(const std::string& language) { bool CacheManager::GetFunctionalGroupNames(FunctionalGroupNames& names) { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); rpc::policy_table_interface_base::FunctionalGroupings::iterator iter = pt_->policy_table.functional_groupings.begin(); rpc::policy_table_interface_base::FunctionalGroupings::iterator iter_end = @@ -1209,7 +1236,7 @@ long CacheManager::ConvertSecondsToMinute(int seconds) { bool CacheManager::SetDefaultPolicy(const std::string& app_id) { CACHE_MANAGER_CHECK(false); - sync_primitives::AutoLock lock(cache_lock_); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ApplicationPolicies::const_iterator iter = pt_->policy_table.app_policies_section.apps.find(kDefaultId); if (pt_->policy_table.app_policies_section.apps.end() != iter) { @@ -1224,6 +1251,7 @@ bool CacheManager::SetDefaultPolicy(const std::string& app_id) { bool CacheManager::IsDefaultPolicy(const std::string& app_id) const { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); const bool result = pt_->policy_table.app_policies_section.apps.end() != pt_->policy_table.app_policies_section.apps.find(app_id) && @@ -1235,6 +1263,7 @@ bool CacheManager::IsDefaultPolicy(const std::string& app_id) const { bool CacheManager::SetIsDefault(const std::string& app_id) { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ApplicationPolicies::const_iterator iter = pt_->policy_table.app_policies_section.apps.find(app_id); if (pt_->policy_table.app_policies_section.apps.end() != iter) { @@ -1246,7 +1275,7 @@ bool CacheManager::SetIsDefault(const std::string& app_id) { bool CacheManager::SetPredataPolicy(const std::string& app_id) { CACHE_MANAGER_CHECK(false); - sync_primitives::AutoLock lock(cache_lock_); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ApplicationPolicies::const_iterator iter = pt_->policy_table.app_policies_section.apps.find(kPreDataConsentId); @@ -1270,6 +1299,7 @@ bool CacheManager::SetPredataPolicy(const std::string& app_id) { bool CacheManager::IsPredataPolicy(const std::string& app_id) const { // TODO(AOleynik): Maybe change for comparison with pre_DataConsent // permissions or check string value from get_string() + sync_primitives::AutoLock auto_lock(cache_lock_); if (!IsApplicationRepresented(app_id)) { return false; } @@ -1296,6 +1326,7 @@ bool CacheManager::IsPredataPolicy(const std::string& app_id) const { bool CacheManager::SetUnpairedDevice(const std::string& device_id, bool unpaired) { + sync_primitives::AutoLock auto_lock(cache_lock_); const bool result = pt_->policy_table.device_data->end() != pt_->policy_table.device_data->find(device_id); if (!result) { @@ -1328,6 +1359,7 @@ bool CacheManager::IsApplicationRepresented(const std::string& app_id) const { if (kDeviceId == app_id) { return true; } + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ApplicationPolicies::const_iterator iter = pt_->policy_table.app_policies_section.apps.find(app_id); return pt_->policy_table.app_policies_section.apps.end() != iter; @@ -1468,6 +1500,7 @@ bool CacheManager::ResetPT(const std::string& file_name) { bool CacheManager::AppExists(const std::string& app_id) const { CACHE_MANAGER_CHECK(false); + sync_primitives::AutoLock auto_lock(cache_lock_); policy_table::ApplicationPolicies::iterator policy_iter = pt_->policy_table.app_policies_section.apps.find(app_id); return pt_->policy_table.app_policies_section.apps.end() != policy_iter; @@ -1493,6 +1526,7 @@ void CacheManager::GetAppRequestTypes( std::vector& request_types) const { LOG4CXX_AUTO_TRACE(logger_); CACHE_MANAGER_CHECK_VOID(); + sync_primitives::AutoLock auto_lock(cache_lock_); if (kDeviceId == policy_app_id) { LOG4CXX_DEBUG(logger_, "Request types not applicable for app_id " << kDeviceId); @@ -1516,6 +1550,7 @@ void CacheManager::GetAppRequestTypes( std::string CacheManager::GetCertificate() const { CACHE_MANAGER_CHECK(std::string("")); + sync_primitives::AutoLock auto_lock(cache_lock_); if (pt_->policy_table.module_config.certificate.is_initialized()) { return *pt_->policy_table.module_config.certificate; } @@ -1639,9 +1674,10 @@ void CacheManager::BackgroundBackuper::threadMain() { LOG4CXX_AUTO_TRACE(logger_); sync_primitives::AutoLock lock(need_backup_lock_); while (!stop_flag_) { - need_backup_lock_.Release(); - InternalBackup(); - need_backup_lock_.Acquire(); + { + sync_primitives::AutoUnlock unlock(need_backup_lock_); + InternalBackup(); + } if (new_data_available_ || stop_flag_) { continue; } -- cgit v1.2.1