diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h index 55c0d60322e45409162eecd152e9fa122347e275..b5fe6afe873364bd4e2a3360081ce47ca06e3f57 100644 --- a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h @@ -59,7 +59,8 @@ public: const DHType dhType); void UpdateBusinessState(const std::string &uuid, const std::string &dhId, BusinessState state); BusinessState QueryBusinessState(const std::string &uuid, const std::string &dhId); - void DumpLoadedComps(std::set &compSourceType, std::set &compSinkType); + void DumpLoadedCompsource(std::set &compSourceType); + void DumpLoadedCompsink(std::set &compSinkType); void Recover(DHType dhType); std::map GetDHSinkInstance(); void TriggerFullCapsSync(const std::string &networkId); @@ -141,7 +142,9 @@ private: private: std::map compSource_; + std::shared_mutex compSourceMutex_; std::map compSink_; + std::shared_mutex compSinkMutex_; std::map compSrcSaId_; std::shared_ptr audioCompPrivacy_ = nullptr; std::shared_ptr cameraCompPrivacy_ = nullptr; diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index 4fe7d962cc46fe76d6f547f714387c23c59680b4..0dc737b8bd1c73bbd92d9377db0fbccbc9a85b02 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -128,6 +128,7 @@ int32_t ComponentManager::InitSAMonitor() DHLOGE("compMonitorPtr_ is null."); return ERR_DH_FWK_COMPONENT_MONITOR_NULL; } + std::unique_lock lock(compSourceMutex_); for (const auto &comp : compSource_) { if (compSrcSaId_.find(comp.first) == compSrcSaId_.end()) { continue; @@ -156,6 +157,7 @@ void ComponentManager::StartComponent() void ComponentManager::RegisterDHStateListener() { + std::unique_lock lock(compSourceMutex_); for (const auto &item : compSource_) { DHLOGI("Register DH State listener, dhType: %{public}" PRIu32, (uint32_t)item.first); if (item.second == nullptr) { @@ -170,7 +172,7 @@ void ComponentManager::RegisterDataSyncTriggerListener() { std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); eventHandler_ = std::make_shared(runner); - + std::unique_lock lock(compSourceMutex_); for (const auto &item : compSource_) { DHLOGI("Register Data Sync Trigger listener, dhType: %{public}" PRIu32, (uint32_t)item.first); if (item.second == nullptr) { @@ -222,6 +224,7 @@ void ComponentManager::UnInitSAMonitor() DHLOGE("compMonitorPtr_ is null."); return; } + std::unique_lock lock(compSourceMutex_); for (const auto &comp : compSource_) { if (compSrcSaId_.find(comp.first) == compSrcSaId_.end()) { continue; @@ -232,6 +235,7 @@ void ComponentManager::UnInitSAMonitor() void ComponentManager::UnregisterDHStateListener() { + std::unique_lock lock(compSourceMutex_); for (const auto &item : compSource_) { DHLOGI("Unregister DH State listener, dhType: %{public}" PRIu32, (uint32_t)item.first); if (item.second == nullptr) { @@ -244,6 +248,7 @@ void ComponentManager::UnregisterDHStateListener() void ComponentManager::UnregisterDataSyncTriggerListener() { + std::unique_lock lock(compSourceMutex_); for (const auto &item : compSource_) { DHLOGI("Unregister Data Sync Trigger listener, dhType: %{public}" PRIu32, (uint32_t)item.first); if (item.second == nullptr) { @@ -274,11 +279,8 @@ void ComponentManager::StopComponent() DHLOGE("StopSource failed, but want to continue"); } if (!WaitForResult(Action::STOP_SINK, sinkResult)) { - DHLOGE("StopSource failed, but want to continue"); + DHLOGE("StopSink failed, but want to continue"); } - - compSource_.clear(); - compSink_.clear(); } void ComponentManager::StopPrivacy() @@ -298,6 +300,7 @@ void ComponentManager::StopPrivacy() ActionResult ComponentManager::StartSource() { DHLOGI("start."); + std::unique_lock lock(compSourceMutex_); std::unordered_map> futures; std::string uuid = DHContext::GetInstance().GetDeviceInfo().uuid; for (const auto &item : compSource_) { @@ -321,6 +324,7 @@ ActionResult ComponentManager::StartSource() ActionResult ComponentManager::StartSource(DHType dhType) { DHLOGI("Start Source, dhType: %{public}" PRIu32, (uint32_t)dhType); + std::unique_lock lock(compSourceMutex_); std::unordered_map> futures; if (compSource_.find(dhType) == compSource_.end()) { DHLOGE("Component for DHType: %{public}" PRIu32 " not init source handler", (uint32_t)dhType); @@ -347,6 +351,7 @@ ActionResult ComponentManager::StartSource(DHType dhType) ActionResult ComponentManager::StartSink() { DHLOGI("start."); + std::unique_lock lock(compSinkMutex_); std::unordered_map> futures; std::string uuid = DHContext::GetInstance().GetDeviceInfo().uuid; for (const auto &item : compSink_) { @@ -378,6 +383,7 @@ ActionResult ComponentManager::StartSink() ActionResult ComponentManager::StartSink(DHType dhType) { DHLOGI("Start Sink, dhType: %{public}" PRIu32, (uint32_t)dhType); + std::unique_lock lock(compSinkMutex_); std::unordered_map> futures; if (compSink_.find(dhType) == compSink_.end()) { DHLOGE("Component for DHType: %{public}" PRIu32 " not init sink handler", (uint32_t)dhType); @@ -412,6 +418,7 @@ ActionResult ComponentManager::StartSink(DHType dhType) ActionResult ComponentManager::StopSource() { DHLOGI("start."); + std::unique_lock lock(compSourceMutex_); std::unordered_map> futures; for (const auto &item : compSource_) { if (item.second == nullptr) { @@ -425,12 +432,14 @@ ActionResult ComponentManager::StopSource() }).detach(); futures.emplace(item.first, f.share()); } + compSource_.clear(); return futures; } ActionResult ComponentManager::StopSink() { DHLOGI("start."); + std::unique_lock lock(compSinkMutex_); std::unordered_map> futures; for (const auto &item : compSink_) { if (item.second == nullptr) { @@ -452,6 +461,7 @@ ActionResult ComponentManager::StopSink() }).detach(); futures.emplace(item.first, f.share()); } + compSink_.clear(); return futures; } @@ -489,6 +499,7 @@ bool ComponentManager::WaitForResult(const Action &action, ActionResult actionsR bool ComponentManager::InitCompSource() { auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes(); + std::unique_lock lock(compSourceMutex_); for (const auto &type : compTypes) { IDistributedHardwareSource *sourcePtr = nullptr; auto ret = ComponentLoader::GetInstance().GetSource(type, sourcePtr); @@ -513,6 +524,7 @@ bool ComponentManager::InitCompSource() bool ComponentManager::InitCompSink() { auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes(); + std::unique_lock lock(compSinkMutex_); for (const auto &type : compTypes) { IDistributedHardwareSink *sinkPtr = nullptr; auto ret = ComponentLoader::GetInstance().GetSink(type, sinkPtr); @@ -536,6 +548,7 @@ int32_t ComponentManager::Enable(const std::string &networkId, const std::string return ERR_DH_FWK_PARA_INVALID; } DHLOGI("start."); + std::unique_lock lock(compSourceMutex_); if (compSource_.find(dhType) == compSource_.end()) { DHLOGE("can not find handler for dhId = %{public}s.", GetAnonyString(dhId).c_str()); return ERR_DH_FWK_PARA_INVALID; @@ -623,6 +636,8 @@ int32_t ComponentManager::Disable(const std::string &networkId, const std::strin if (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(dhId)) { return ERR_DH_FWK_PARA_INVALID; } + DHLOGI("start."); + std::unique_lock lock(compSourceMutex_); auto find = compSource_.find(dhType); if (find == compSource_.end()) { DHLOGE("can not find handler for dhId = %{public}s.", GetAnonyString(dhId).c_str()); @@ -895,11 +910,17 @@ void ComponentManager::UpdateVersionCache(const std::string &uuid, const Version VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); } -void ComponentManager::DumpLoadedComps(std::set &compSourceType, std::set &compSinkType) +void ComponentManager::DumpLoadedCompsource(std::set &compSourceType) { + std::unique_lock lock(compSourceMutex_); for (auto compSource : compSource_) { compSourceType.emplace(compSource.first); } +} + +void ComponentManager::DumpLoadedCompsink(std::set &compSinkType) +{ + std::unique_lock lock(compSinkMutex_); for (auto compSink : compSink_) { compSinkType.emplace(compSink.first); } @@ -969,6 +990,7 @@ void ComponentManager::RecoverDistributedHardware(DHType dhType) std::map ComponentManager::GetDHSinkInstance() { + std::unique_lock lock(compSinkMutex_); return compSink_; } @@ -1022,6 +1044,7 @@ void ComponentManager::UpdateBusinessState(const std::string &networkId, const s IDistributedHardwareSource* ComponentManager::GetDHSourceInstance(DHType dhType) { + std::unique_lock lock(compSourceMutex_); if (compSource_.find(dhType) == compSource_.end()) { DHLOGE("can not find handler for dhType = %{public}d.", dhType); return nullptr; diff --git a/services/distributedhardwarefwkservice/src/hidumphelper/hidump_helper.cpp b/services/distributedhardwarefwkservice/src/hidumphelper/hidump_helper.cpp index a121ac83b9e71ac46125a23a1314b6d23c1abdf7..1b249cdf3970af337c2bdb58ca5177c0a03319da 100644 --- a/services/distributedhardwarefwkservice/src/hidumphelper/hidump_helper.cpp +++ b/services/distributedhardwarefwkservice/src/hidumphelper/hidump_helper.cpp @@ -134,7 +134,8 @@ int32_t HidumpHelper::ShowAllLoadedComps(std::string &result) DHLOGI("Dump all loaded compTypes."); std::set loadedCompSource {}; std::set loadedCompSink {}; - ComponentManager::GetInstance().DumpLoadedComps(loadedCompSource, loadedCompSink); + ComponentManager::GetInstance().DumpLoadedCompsource(loadedCompSource); + ComponentManager::GetInstance().DumpLoadedCompsink(loadedCompSink); DHVersion dhVersion; ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion); @@ -143,9 +144,8 @@ int32_t HidumpHelper::ShowAllLoadedComps(std::string &result) if (!loadedCompSource.empty()) { for (auto compSource : loadedCompSource) { std::string dhTypeStr = "UNKNOWN"; - auto it = DHTypeStrMap.find(compSource); - if (it != DHTypeStrMap.end()) { - dhTypeStr = it->second; + if (DHTypeStrMap.find(compSource) != DHTypeStrMap.end()) { + dhTypeStr = DHTypeStrMap.find(compSource)->second; } std::string sourceVersion = ""; auto iter = dhVersion.compVersions.find(compSource); @@ -164,9 +164,8 @@ int32_t HidumpHelper::ShowAllLoadedComps(std::string &result) if (!loadedCompSink.empty()) { for (auto compSink : loadedCompSink) { std::string dhTypeStr = "UNKNOWN"; - auto it = DHTypeStrMap.find(compSink); - if (it != DHTypeStrMap.end()) { - dhTypeStr = it->second; + if (DHTypeStrMap.find(compSink) != DHTypeStrMap.end()) { + dhTypeStr = DHTypeStrMap.find(compSink)->second; } std::string sinkVersion = ""; auto iter = dhVersion.compVersions.find(compSink); diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp index 0c0affba7e91912917d6f8139e04fa34e1b1a57a..d5b996c106ffd30b4b230a3fbf1b23ef8fde8d06 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp @@ -850,16 +850,22 @@ HWTEST_F(ComponentManagerTest, UpdateVersionCache_001, TestSize.Level0) } /** - * @tc.name: DumpLoadedComps_001 - * @tc.desc: Verify the DumpLoadedComps function + * @tc.name: DumpLoadedCompsource_001 + * @tc.desc: Verify the DumpLoadedCompsource function * @tc.type: FUNC * @tc.require: AR000GHSJM */ -HWTEST_F(ComponentManagerTest, DumpLoadedComps_001, TestSize.Level0) +HWTEST_F(ComponentManagerTest, DumpLoadedCompsource_001, TestSize.Level0) { std::set compSourceType; + ComponentManager::GetInstance().DumpLoadedCompsource(compSourceType); + EXPECT_EQ(true, ComponentManager::GetInstance().compSource_.empty()); +} + +HWTEST_F(ComponentManagerTest, DumpLoadedCompsink_001, TestSize.Level0) +{ std::set compSinkType; - ComponentManager::GetInstance().DumpLoadedComps(compSourceType, compSinkType); + ComponentManager::GetInstance().DumpLoadedCompsink(compSinkType); EXPECT_EQ(true, ComponentManager::GetInstance().compSource_.empty()); }