diff --git a/services/native/include/work_event_handler.h b/services/native/include/work_event_handler.h index 88d1d5c6d95209c4ecdc87f6506c4e6bf4d1dff6..c219207abd725d07e42769dcea83234ba5bcf48b 100644 --- a/services/native/include/work_event_handler.h +++ b/services/native/include/work_event_handler.h @@ -31,7 +31,7 @@ public: IDE_RETRIGGER_MSG }; WorkEventHandler(const std::shared_ptr& runner, - const wptr& service); + const std::shared_ptr& service); ~WorkEventHandler() override = default; /** * @brief Process event. @@ -41,7 +41,7 @@ public: void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; private: - wptr service_; + std::weak_ptr service_; }; } // namespace WorkScheduler } // namespace OHOS diff --git a/services/native/include/work_policy_manager.h b/services/native/include/work_policy_manager.h index 016d756fe85575b45457e72cac8e7d795bfcc5d3..112cc1cec74857506c33866922538bfa32426932 100644 --- a/services/native/include/work_policy_manager.h +++ b/services/native/include/work_policy_manager.h @@ -34,7 +34,7 @@ class AppDataClearListener; class Watchdog; class WorkPolicyManager { public: - explicit WorkPolicyManager(const wptr& wss); + explicit WorkPolicyManager(const std::shared_ptr& wss); ~WorkPolicyManager() = default; /** * @brief Init. @@ -233,13 +233,13 @@ private: uint32_t NewWatchdogId(); void AddWatchdogForWork(std::shared_ptr workStatus); std::shared_ptr GetWorkFromWatchdog(uint32_t id); - void UpdateWatchdogTime(const wptr &wmsptr, + void UpdateWatchdogTime(const std::shared_ptr &wmsptr, std::shared_ptr &topWork); std::list> GetAllIdeWorkStatus(const std::string &bundleName, const std::string &abilityName); void SendIdeWorkRetriggerEvent(int32_t delaytime); - const wptr wss_; + const std::weak_ptr wss_; std::shared_ptr workConnManager_; std::shared_ptr handler_; diff --git a/services/native/include/work_queue_manager.h b/services/native/include/work_queue_manager.h index d5803da57c4f2c917bc0fde6c618212133c9b189..5cf47d9473360080833a7080e35becee10a13ba6 100644 --- a/services/native/include/work_queue_manager.h +++ b/services/native/include/work_queue_manager.h @@ -29,7 +29,7 @@ namespace WorkScheduler { class WorkSchedulerService; class WorkQueueManager : std::enable_shared_from_this { public: - explicit WorkQueueManager(const wptr& wss); + explicit WorkQueueManager(const std::shared_ptr& wss); ~WorkQueueManager() = default; /** * @brief Init. @@ -120,7 +120,7 @@ private: std::vector> GetReayQueue(WorkCondition::Type conditionType, std::shared_ptr conditionVal); std::mutex mutex_; - const wptr wss_; + const std::weak_ptr wss_; std::map> queueMap_; std::map> listenerMap_; diff --git a/services/native/src/work_event_handler.cpp b/services/native/src/work_event_handler.cpp index 68437aea241705aeb8961992e468572da3725e41..0d8f784b382981b5e5188f6bc539b0eece9de7bb 100644 --- a/services/native/src/work_event_handler.cpp +++ b/services/native/src/work_event_handler.cpp @@ -25,7 +25,7 @@ using namespace OHOS::AppExecFwk; namespace OHOS { namespace WorkScheduler { WorkEventHandler::WorkEventHandler(const shared_ptr& runner, - const wptr& service) : EventHandler(runner), service_(service) + const std::shared_ptr& service) : EventHandler(runner), service_(service) { WS_HILOGD("instance created."); } @@ -33,23 +33,22 @@ WorkEventHandler::WorkEventHandler(const shared_ptr& runner, void WorkEventHandler::ProcessEvent([[maybe_unused]] const InnerEvent::Pointer& event) { WS_HILOGD("begin"); - auto wssptr = service_.promote(); - if (wssptr == nullptr) { - WS_HILOGE("service.promote() returns nullptr"); + if (service_.expired()) { + WS_HILOGE("service_ expired"); return; } WS_HILOGD("eventid = %{public}u", event->GetInnerEventId()); switch (event->GetInnerEventId()) { case RETRIGGER_MSG: { - wssptr->GetWorkPolicyManager()->CheckWorkToRun(); + service_.lock()->GetWorkPolicyManager()->CheckWorkToRun(); break; } case SERVICE_INIT_MSG: { - wssptr->Init(GetEventRunner()); + service_.lock()->Init(GetEventRunner()); break; } case IDE_RETRIGGER_MSG: { - wssptr->GetWorkPolicyManager()->TriggerIdeWork(); + service_.lock()->GetWorkPolicyManager()->TriggerIdeWork(); break; } default: diff --git a/services/native/src/work_policy_manager.cpp b/services/native/src/work_policy_manager.cpp index 1eb8fe0f21322a28c6848cf4395490dfe368b95b..35e4254bbb93d882ec17771be150016be70c61f4 100644 --- a/services/native/src/work_policy_manager.cpp +++ b/services/native/src/work_policy_manager.cpp @@ -49,7 +49,7 @@ const int32_t LONG_WATCHDOG_TIME = 20 * 60 * 1000; static int32_t g_lastWatchdogTime = WATCHDOG_TIME; } -WorkPolicyManager::WorkPolicyManager(const wptr& wss) : wss_(wss) +WorkPolicyManager::WorkPolicyManager(const std::shared_ptr& wss) : wss_(wss) { conditionReadyQueue_ = std::make_shared(); watchdogId_ = INIT_WATCHDOG_ID; @@ -60,18 +60,17 @@ WorkPolicyManager::WorkPolicyManager(const wptr& wss) : ws bool WorkPolicyManager::Init(const std::shared_ptr& runner) { WS_HILOGD("Work policy manager init."); - workConnManager_ = make_shared(); - auto wmsptr = wss_.promote(); - if (wmsptr == nullptr) { - WS_HILOGE("failed due to wmsptr is nullptr"); + if (wss_.expired()) { + WS_HILOGE("wss_ expired"); return false; } - handler_ = wmsptr->GetHandler(); + workConnManager_ = make_shared(); + handler_ = wss_.lock()->GetHandler(); if (handler_ == nullptr) { WS_HILOGE("failed due to handler_ is nullptr"); return false; } - watchdog_ = std::make_shared(wmsptr->GetWorkPolicyManager(), runner); + watchdog_ = std::make_shared(wss_.lock()->GetWorkPolicyManager(), runner); return true; } @@ -290,6 +289,10 @@ int32_t WorkPolicyManager::GetRunningCount() void WorkPolicyManager::OnPolicyChanged(PolicyType policyType, shared_ptr detectorVal) { WS_HILOGD("enter"); + if (wss_.expired()) { + WS_HILOGE("wss_ expired"); + return; + } switch (policyType) { case PolicyType::APP_REMOVED: { int32_t uid = detectorVal->intVal; @@ -297,8 +300,7 @@ void WorkPolicyManager::OnPolicyChanged(PolicyType policyType, shared_ptrStopAndClearWorksByUid(detectorVal->intVal); + wss_.lock()->StopAndClearWorksByUid(detectorVal->intVal); break; } default: {} @@ -345,14 +347,13 @@ std::shared_ptr WorkPolicyManager::GetWorkToRun() void WorkPolicyManager::RealStartWork(std::shared_ptr topWork) { WS_HILOGD("RealStartWork topWork ID: %{public}s", topWork->workId_.c_str()); - auto wmsptr = wss_.promote(); - if (wmsptr == nullptr) { - WS_HILOGE("Workscheduler service is null"); - return; + if (wss_.expired()) { + WS_HILOGE("wss_ expired"); + return; } - UpdateWatchdogTime(wmsptr, topWork); + UpdateWatchdogTime(wss_.lock(), topWork); topWork->MarkStatus(WorkStatus::Status::RUNNING); - wmsptr->UpdateWorkBeforeRealStart(topWork); + wss_.lock()->UpdateWorkBeforeRealStart(topWork); RemoveFromReadyQueue(topWork); bool ret = workConnManager_->StartWork(topWork); if (ret) { @@ -368,7 +369,7 @@ void WorkPolicyManager::RealStartWork(std::shared_ptr topWork) } } -void WorkPolicyManager::UpdateWatchdogTime(const wptr &wmsptr, +void WorkPolicyManager::UpdateWatchdogTime(const std::shared_ptr &wmsptr, std::shared_ptr &topWork) { if (!wmsptr->CheckEffiResApplyInfo(topWork->uid_)) { @@ -413,14 +414,13 @@ void WorkPolicyManager::SendRetrigger(int32_t delaytime) void WorkPolicyManager::WatchdogTimeOut(uint32_t watchdogId) { + if (wss_.expired()) { + WS_HILOGE("wss_ expired"); + return; + } WS_HILOGD("WatchdogTimeOut."); std::shared_ptr workStatus = GetWorkFromWatchdog(watchdogId); - auto wmsptr = wss_.promote(); - if (wmsptr == nullptr) { - WS_HILOGE("Workscheduler service is null"); - return; - } - wmsptr->WatchdogTimeOut(workStatus); + wss_.lock()->WatchdogTimeOut(workStatus); } std::shared_ptr WorkPolicyManager::GetWorkFromWatchdog(uint32_t id) diff --git a/services/native/src/work_queue_manager.cpp b/services/native/src/work_queue_manager.cpp index 8fcb81ee944155c12cfce982aef561e2ef1930db..d8ce16451587d9231f1f54c3b3be2a6e7806209d 100644 --- a/services/native/src/work_queue_manager.cpp +++ b/services/native/src/work_queue_manager.cpp @@ -27,7 +27,7 @@ namespace WorkScheduler { const uint32_t TIME_CYCLE = 20 * 60 * 1000; // 20min static int32_t g_timeRetrigger = INT32_MAX; -WorkQueueManager::WorkQueueManager(const wptr& wss) : wss_(wss) +WorkQueueManager::WorkQueueManager(const std::shared_ptr& wss) : wss_(wss) { timeCycle_ = TIME_CYCLE; } @@ -150,8 +150,11 @@ void WorkQueueManager::OnConditionChanged(WorkCondition::Type conditionType, for (auto it : readyWorkVector) { it->MarkStatus(WorkStatus::Status::CONDITION_READY); } - auto ws = wss_.promote(); - ws->OnConditionReady(make_shared>>(readyWorkVector)); + if (wss_.expired()) { + WS_HILOGE("wss_ expired"); + return; + } + wss_.lock()->OnConditionReady(make_shared>>(readyWorkVector)); } bool WorkQueueManager::StopAndClearWorks(list> workList) diff --git a/services/native/src/work_scheduler_service.cpp b/services/native/src/work_scheduler_service.cpp index 005cb0c65a96e5ef18f54ad48f26891a9f0c4782..10f68a3c2c8252827ff98fefcd54b9cc5d138084 100644 --- a/services/native/src/work_scheduler_service.cpp +++ b/services/native/src/work_scheduler_service.cpp @@ -105,7 +105,7 @@ void WorkSchedulerService::OnStart() WS_HILOGE("Init failed due to create EventRunner"); return; } - handler_ = std::make_shared(eventRunner_, wss); + handler_ = std::make_shared(eventRunner_, instance); // Try to init. Init(eventRunner_); @@ -281,7 +281,7 @@ void WorkSchedulerService::WorkQueueManagerInit(const std::shared_ptr(wss); + workQueueManager_ = make_shared(instance); } auto networkListener = make_shared(workQueueManager_); @@ -314,7 +314,7 @@ bool WorkSchedulerService::WorkPolicyManagerInit(const std::shared_ptr(wss); + workPolicyManager_ = make_shared(instance); } if (!workPolicyManager_->Init(runner)) { WS_HILOGE("work policy manager init failed!");