From 5e09ee5bed28739af00224393d7092e5c064cb3e Mon Sep 17 00:00:00 2001 From: huangxiaolinabc123 Date: Thu, 10 Aug 2023 10:01:51 +0800 Subject: [PATCH] fix memory lock Signed-off-by: huangxiaolinabc123 Change-Id: If473a9461ef7263c79ef73def71da8e4a36184e1 --- .../include/collect/common_event_collect.h | 24 +++--- .../source/collect/common_event_collect.cpp | 85 ++++++++----------- .../src/common_event_collect_test.cpp | 28 ------ 3 files changed, 49 insertions(+), 88 deletions(-) diff --git a/services/samgr/native/include/collect/common_event_collect.h b/services/samgr/native/include/collect/common_event_collect.h index 2ab273ab..fd701e05 100644 --- a/services/samgr/native/include/collect/common_event_collect.h +++ b/services/samgr/native/include/collect/common_event_collect.h @@ -22,6 +22,7 @@ #include "event_handler.h" #include "icollect_plugin.h" #include "iremote_object.h" +#include "system_ability_status_change_stub.h" namespace OHOS { class CommonEventCollect : public ICollectPlugin { @@ -35,15 +36,15 @@ public: bool CheckCondition(const OnDemandCondition& condition) override; int32_t AddCollectEvent(const OnDemandEvent& event) override; void Init(const std::list& saProfiles) override; - bool AddCommonListener(); int64_t SaveOnDemandReasonExtraData(const EventFwk::CommonEventData& data); void RemoveOnDemandReasonExtraData(int64_t extraDataId); bool GetOnDemandReasonExtraData(int64_t extraDataId, OnDemandReasonExtraData& extraData) override; + bool CreateCommonEventSubscriber(); private: - bool IsCesReady(); - void CreateCommonEventSubscriber(); int64_t GenerateExtraDataIdLocked(); + void AddSkillsEvent(EventFwk::MatchingSkills& skill); std::mutex commomEventLock_; + std::mutex commonEventSubscriberLock_; sptr commonEventDeath_; std::set commonEventNames_; std::shared_ptr workHandler_; @@ -55,6 +56,14 @@ private: std::map extraDatas_; }; +class CommonEventListener : public SystemAbilityStatusChangeStub { +public: + CommonEventListener(const sptr& commonEventCollect); + void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + void OnRemoveSystemAbility(int32_t systemAblityId, const std::string& deviceId) override; +private: + sptr commonEventCollect_; +}; class CommonHandler : public AppExecFwk::EventHandler { public: CommonHandler(const std::shared_ptr& runner, @@ -76,14 +85,5 @@ public: private: wptr collect_; }; - -class CommonEventDeathRecipient : public IRemoteObject::DeathRecipient { -public: - void OnRemoteDied(const wptr& remote) override; - explicit CommonEventDeathRecipient(const std::shared_ptr& handler) : handler_(handler) {} - ~CommonEventDeathRecipient() override = default; -private: - std::shared_ptr handler_; -}; } // namespace OHOS #endif // SYSTEM_ABILITY_MANAGER_COMMON_EVENT_COLLECT_H \ No newline at end of file diff --git a/services/samgr/native/source/collect/common_event_collect.cpp b/services/samgr/native/source/collect/common_event_collect.cpp index 060277b4..5556ee1e 100644 --- a/services/samgr/native/source/collect/common_event_collect.cpp +++ b/services/samgr/native/source/collect/common_event_collect.cpp @@ -28,10 +28,8 @@ using namespace OHOS::AppExecFwk; namespace OHOS { namespace { constexpr uint32_t INIT_EVENT = 10; -constexpr uint32_t COMMON_DIED_EVENT = 11; constexpr uint32_t REMOVE_EXTRA_DATA_EVENT = 12; constexpr uint32_t REMOVE_EXTRA_DATA_DELAY_TIME = 300000; -constexpr int64_t DELAY_TIME = 1000; constexpr int64_t MAX_EXTRA_DATA_ID = 1000000000; constexpr int32_t COMMON_EVENT_SERVICE_ID = 3299; const std::string UID = "uid"; @@ -56,7 +54,6 @@ int32_t CommonEventCollect::OnStart() return ERR_INVALID_VALUE; } workHandler_ = std::make_shared(handler->GetEventRunner(), this); - commonEventDeath_ = sptr(new CommonEventDeathRecipient(workHandler_)); workHandler_->SendEvent(INIT_EVENT); return ERR_OK; } @@ -98,47 +95,52 @@ void CommonEventCollect::Init(const std::list& onDemandSaProfiles) } } -bool CommonEventCollect::AddCommonListener() +void CommonEventCollect::AddSkillsEvent(EventFwk::MatchingSkills& skill) { - if (!IsCesReady()) { - return false; + std::lock_guard autoLock(commomEventLock_); + for (auto& commonEventName : commonEventNames_) { + HILOGD("CommonEventCollect add event: %{puhlic}s", commonEventName.c_str()); + skill.AddEvent(commonEventName); } - HILOGI("CommonEventCollect AddCommonListener ces is ready"); - CreateCommonEventSubscriber(); - return EventFwk::CommonEventManager::SubscribeCommonEvent(commonEventSubscriber_); } -void CommonEventCollect::CreateCommonEventSubscriber() +bool CommonEventCollect::CreateCommonEventSubscriber() { - EventFwk::MatchingSkills skill = EventFwk::MatchingSkills(); - { - std::lock_guard autoLock(commomEventLock_); - for (auto& commonEventName : commonEventNames_) { - HILOGD("CommonEventCollect add event: %{puhlic}s", commonEventName.c_str()); - skill.AddEvent(commonEventName); + std::lock_guard autoLock(commonEventSubscriberLock_); + EventFwk::MatchingSkills skill; + if (commonEventSubscriber_ != nullptr) { + skill = commonEventSubscriber_->GetSubscribeInfo().GetMatchingSkills(); + AddSkillsEvent(skill); + bool isUnsubscribe = EventFwk::CommonEventManager::UnSubscribeCommonEvent(commonEventSubscriber_); + if (!isUnsubscribe) { + HILOGE("OnAddSystemAbility isUnsubscribe failed!"); } + } else { + skill = EventFwk::MatchingSkills(); + AddSkillsEvent(skill); + EventFwk::CommonEventSubscribeInfo info(skill); + commonEventSubscriber_ = std::make_shared(info, this); } - EventFwk::CommonEventSubscribeInfo info(skill); - commonEventSubscriber_ = std::make_shared(info, this); + return EventFwk::CommonEventManager::SubscribeCommonEvent(commonEventSubscriber_); } -bool CommonEventCollect::IsCesReady() +CommonEventListener::CommonEventListener(const sptr& commonEventCollect) + : commonEventCollect_(commonEventCollect) {} + +void CommonEventListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) { - auto cesProxy = SystemAbilityManager::GetInstance()->CheckSystemAbility( - COMMON_EVENT_SERVICE_ID); - if (cesProxy != nullptr) { - IPCObjectProxy* proxy = reinterpret_cast(cesProxy.GetRefPtr()); - if (commonEventDeath_ != nullptr) { - cesProxy->AddDeathRecipient(commonEventDeath_); - } - // make sure the proxy is not dead - if (proxy != nullptr && !proxy->IsObjectDead()) { - return true; + if (systemAbilityId == COMMON_EVENT_SERVICE_ID) { + HILOGI("CommonEventCollect ces is ready"); + if (!commonEventCollect_->CreateCommonEventSubscriber()) { + HILOGE("OnAddSystemAbility CreateCommonEventSubscriber failed!"); } } - return false; } +void CommonEventListener::OnRemoveSystemAbility(int32_t systemAblityId, const std::string& deviceId) +{ + HILOGI("CommonEventListener OnRemoveSystemAblity systemAblityId:%{public}d", systemAblityId); +} void CommonEventCollect::SaveAction(const std::string& action) { std::lock_guard autoLock(commonEventStateLock_); @@ -219,9 +221,8 @@ int32_t CommonEventCollect::AddCollectEvent(const OnDemandEvent& event) HILOGI("CommonEventCollect add collect events: %{public}s", event.name.c_str()); commonEventNames_.insert(event.name); } - if (!AddCommonListener()) { - HILOGE("CommonEventCollect add listener failed"); - return ERR_INVALID_VALUE; + if (!CreateCommonEventSubscriber()) { + HILOGE("AddCollectEvent CreateCommonEventSubscriber failed!"); } return ERR_OK; } @@ -233,7 +234,7 @@ void CommonHandler::ProcessEvent(const InnerEvent::Pointer& event) return; } auto eventId = event->GetInnerEventId(); - if (eventId != INIT_EVENT && eventId != COMMON_DIED_EVENT && eventId != REMOVE_EXTRA_DATA_EVENT) { + if (eventId != INIT_EVENT && eventId != REMOVE_EXTRA_DATA_EVENT) { HILOGE("CommonEventCollect ProcessEvent error event code!"); return; } @@ -246,12 +247,8 @@ void CommonHandler::ProcessEvent(const InnerEvent::Pointer& event) commonCollect->RemoveOnDemandReasonExtraData(event->GetParam()); return; } - if (!commonCollect->AddCommonListener()) { - HILOGW("CommonEventCollect AddCommonListener retry"); - SendEvent(INIT_EVENT, DELAY_TIME); - return; - } - HILOGI("CommonEventCollect AddCommonListener success"); + sptr listener = new CommonEventListener(commonCollect); + SystemAbilityManager::GetInstance()->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, listener); } CommonEventSubscriber::CommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscribeInfo, @@ -273,12 +270,4 @@ void CommonEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data OnDemandEvent event = {COMMON_EVENT, action, std::to_string(code), extraDataId}; collect->ReportEvent(event); } - -void CommonEventDeathRecipient::OnRemoteDied(const wptr& remote) -{ - HILOGI("CommonEventDeathRecipient called!"); - if (handler_ != nullptr) { - handler_->SendEvent(COMMON_DIED_EVENT, DELAY_TIME); - } -} } // namespace OHOS \ No newline at end of file diff --git a/services/samgr/native/test/unittest/src/common_event_collect_test.cpp b/services/samgr/native/test/unittest/src/common_event_collect_test.cpp index cc050e39..bd2826fe 100644 --- a/services/samgr/native/test/unittest/src/common_event_collect_test.cpp +++ b/services/samgr/native/test/unittest/src/common_event_collect_test.cpp @@ -116,21 +116,6 @@ HWTEST_F(CommonEventCollectTest, OnStop002, TestSize.Level3) * @tc.type: FUNC */ HWTEST_F(CommonEventCollectTest, init001, TestSize.Level3) -{ - DTEST_LOG << " init001 BEGIN" << std::endl; - sptr commonEventCollect = new CommonEventCollect(nullptr); - const std::list onDemandSaProfiles; - commonEventCollect->Init(onDemandSaProfiles); - bool ret = commonEventCollect->AddCommonListener(); - EXPECT_EQ(false, ret); -} - -/** - * @tc.name: init002 - * @tc.desc: test init - * @tc.type: FUNC - */ -HWTEST_F(CommonEventCollectTest, init002, TestSize.Level3) { DTEST_LOG << " init002 BEGIN" << std::endl; shared_ptr commonEventCollect = make_shared(nullptr); @@ -145,19 +130,6 @@ HWTEST_F(CommonEventCollectTest, init002, TestSize.Level3) EXPECT_EQ(ERR_OK, ret); } -/** - * @tc.name: IsCesReady001 - * @tc.desc: test IsCesReady - * @tc.type: FUNC - */ -HWTEST_F(CommonEventCollectTest, IsCesReady001, TestSize.Level3) -{ - DTEST_LOG << " IsCesReady001 BEGIN" << std::endl; - sptr commonEventCollect = new CommonEventCollect(nullptr); - bool ret = commonEventCollect->IsCesReady(); - EXPECT_EQ(false, ret); -} - /** * @tc.name: ProcessEvent001 * @tc.desc: test ProcessEvent -- Gitee