diff --git a/interfaces/innerkits/include/bundle_active_client.h b/interfaces/innerkits/include/bundle_active_client.h index bad8e3fad86e3e8c537cb3d9e6c76ef1593ad98e..1985b2bfc18bad832ba2445ab8f8626265baedfd 100644 --- a/interfaces/innerkits/include/bundle_active_client.h +++ b/interfaces/innerkits/include/bundle_active_client.h @@ -221,7 +221,7 @@ private: sptr recipient_; std::shared_ptr bundleClientRunner_ {nullptr}; std::shared_ptr bundleClientHandler_ {nullptr}; - std::mutex mutex_; + std::recursive_mutex mutex_; }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/interfaces/innerkits/src/bundle_active_client.cpp b/interfaces/innerkits/src/bundle_active_client.cpp index 483e894825c898743c426e4f3c5d0b631c2513bc..c24711db2241226cd90bf7e0410692c51cc199f4 100644 --- a/interfaces/innerkits/src/bundle_active_client.cpp +++ b/interfaces/innerkits/src/bundle_active_client.cpp @@ -34,7 +34,6 @@ ErrCode BundleActiveClient::GetBundleActiveProxy() if (bundleActiveProxy_ != nullptr) { return ERR_OK; } - std::lock_guard lock(mutex_); sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (!samgr) { BUNDLE_ACTIVE_LOGE("Failed to get SystemAbilityManager."); @@ -75,6 +74,7 @@ ErrCode BundleActiveClient::GetBundleActiveProxy() ErrCode BundleActiveClient::ReportEvent(BundleActiveEvent event, const int32_t userId) { BUNDLE_ACTIVE_LOGI("BundleActiveClient::ReportEvent called"); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -84,6 +84,7 @@ ErrCode BundleActiveClient::ReportEvent(BundleActiveEvent event, const int32_t u ErrCode BundleActiveClient::IsBundleIdle(bool& isBundleIdle, const std::string& bundleName, int32_t userId) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -94,6 +95,7 @@ ErrCode BundleActiveClient::IsBundleIdle(bool& isBundleIdle, const std::string& ErrCode BundleActiveClient::QueryBundleStatsInfoByInterval(std::vector& PackageStats, const int32_t intervalType, const int64_t beginTime, const int64_t endTime, int32_t userId) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -104,6 +106,7 @@ ErrCode BundleActiveClient::QueryBundleStatsInfoByInterval(std::vector& bundleActiveEvents, const int64_t beginTime, const int64_t endTime, int32_t userId) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -115,6 +118,7 @@ ErrCode BundleActiveClient::QueryBundleEvents(std::vector& bu ErrCode BundleActiveClient::SetAppGroup(std::string bundleName, const int32_t newGroup, int32_t userId) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -125,6 +129,7 @@ ErrCode BundleActiveClient::SetAppGroup(std::string bundleName, const int32_t ne ErrCode BundleActiveClient::QueryBundleStatsInfos(std::vector& bundleActivePackageStats, const int32_t intervalType, const int64_t beginTime, const int64_t endTime) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -135,6 +140,7 @@ ErrCode BundleActiveClient::QueryBundleStatsInfos(std::vector& bundleActiveEvents, const int64_t beginTime, const int64_t endTime) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -144,6 +150,7 @@ ErrCode BundleActiveClient::QueryCurrentBundleEvents(std::vector lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -158,6 +165,7 @@ ErrCode BundleActiveClient::QueryModuleUsageRecords(int32_t maxNum, std::vector< BUNDLE_ACTIVE_LOGI("maxNum is illegal, maxNum is %{public}d", maxNum); return ERR_MAX_RECORDS_NUM_BIGER_THEN_ONE_THOUSAND; } + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -167,6 +175,7 @@ ErrCode BundleActiveClient::QueryModuleUsageRecords(int32_t maxNum, std::vector< ErrCode BundleActiveClient::RegisterAppGroupCallBack(const sptr &observer) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -180,6 +189,7 @@ ErrCode BundleActiveClient::RegisterAppGroupCallBack(const sptr &observer) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -194,6 +204,7 @@ ErrCode BundleActiveClient::UnRegisterAppGroupCallBack(const sptr& eventStats, int32_t userId) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -204,6 +215,7 @@ ErrCode BundleActiveClient::QueryDeviceEventStats(int64_t beginTime, int64_t end ErrCode BundleActiveClient::QueryNotificationEventStats(int64_t beginTime, int64_t endTime, std::vector& eventStats, int32_t userId) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -213,6 +225,7 @@ ErrCode BundleActiveClient::QueryNotificationEventStats(int64_t beginTime, int64 void BundleActiveClient::BundleActiveClientDeathRecipient::AddObserver(const sptr &observer) { + std::lock_guard lock(BundleActiveClient::GetInstance().mutex_); if (observer) { observer_ = observer; } @@ -220,6 +233,7 @@ void BundleActiveClient::BundleActiveClientDeathRecipient::AddObserver(const spt void BundleActiveClient::BundleActiveClientDeathRecipient::RemoveObserver() { + std::lock_guard lock(BundleActiveClient::GetInstance().mutex_); if (observer_) { observer_ = nullptr; } @@ -228,6 +242,7 @@ void BundleActiveClient::BundleActiveClientDeathRecipient::RemoveObserver() void BundleActiveClient::BundleActiveClientDeathRecipient::OnRemoteDied(const wptr &object) { (void)object; + std::lock_guard lock(BundleActiveClient::GetInstance().mutex_); BundleActiveClient::GetInstance().bundleActiveProxy_ = nullptr; BundleActiveClient::GetInstance().bundleClientHandler_->PostTask([this]() { this->OnServiceDiedInner(); @@ -236,6 +251,7 @@ void BundleActiveClient::BundleActiveClientDeathRecipient::OnRemoteDied(const wp void BundleActiveClient::BundleActiveClientDeathRecipient::OnServiceDiedInner() { + std::lock_guard lock(BundleActiveClient::GetInstance().mutex_); while (BundleActiveClient::GetInstance().GetBundleActiveProxy() != ERR_OK) { sleep(SLEEP_TIME); }