diff --git a/services/distributedhardwarefwkservice/include/task/task_board.h b/services/distributedhardwarefwkservice/include/task/task_board.h index a0e54c5e39b79729a3f3c1f03934759b9780ec82..d979994ab15daab41e320ff8e084bbf434894ce2 100644 --- a/services/distributedhardwarefwkservice/include/task/task_board.h +++ b/services/distributedhardwarefwkservice/include/task/task_board.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -40,6 +41,9 @@ public: void DumpAllTasks(std::vector &taskInfos); bool IsEnabledDevice(const std::string &enabledDeviceKey); + void AddDisableTask(std::shared_ptr task); + void RemoveDisableTask(const std::string &taskId); + bool IsAllDisableTaskFinish(); private: void RemoveTaskInner(std::string taskId); @@ -52,6 +56,8 @@ private: /* The key is combination of deviceId and dhId, and the value is taskParam */ std::unordered_map enabledDevices_; std::mutex enabledDevicesMutex_; + std::unordered_map> disableTasks_; + std::shared_mutex disableTasksMtx_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/include/task/task_factory.h b/services/distributedhardwarefwkservice/include/task/task_factory.h index c733bd8d1a825facc2cae2e411775376af2551ec..037c9287931b5996167d765c25976500ba873a8d 100644 --- a/services/distributedhardwarefwkservice/include/task/task_factory.h +++ b/services/distributedhardwarefwkservice/include/task/task_factory.h @@ -30,6 +30,7 @@ public: private: std::shared_ptr CreateEnableTask(const TaskParam &taskParam); std::shared_ptr CreateDisableTask(const TaskParam &taskParam); + void AddTask(TaskType taskType, std::shared_ptr fatherTask, std::shared_ptr task); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/task/disable_task.cpp b/services/distributedhardwarefwkservice/src/task/disable_task.cpp index 2b30632c9195e60275e2a944f3b6999984643770..fa31bada4574450e653718e2f6020e49e9e226fb 100644 --- a/services/distributedhardwarefwkservice/src/task/disable_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/disable_task.cpp @@ -78,6 +78,7 @@ void DisableTask::DoTaskInner() std::string taskId = GetId(); std::shared_ptr father = GetFatherTask().lock(); TaskBoard::GetInstance().RemoveTask(taskId); + TaskBoard::GetInstance().RemoveDisableTask(taskId); /* if finish task, notify father finish */ if (father != nullptr) { auto offLineTask = std::static_pointer_cast(father); diff --git a/services/distributedhardwarefwkservice/src/task/meta_disable_task.cpp b/services/distributedhardwarefwkservice/src/task/meta_disable_task.cpp index 4326e47663e3f378bd267ddb365213482c86fd9c..e2327d4832d9bfecd7204a540ce0cd71a770acc7 100644 --- a/services/distributedhardwarefwkservice/src/task/meta_disable_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/meta_disable_task.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -69,6 +69,7 @@ void MetaDisableTask::DoTaskInner() std::string taskId = GetId(); std::shared_ptr father = GetFatherTask().lock(); TaskBoard::GetInstance().RemoveTask(taskId); + TaskBoard::GetInstance().RemoveDisableTask(taskId); /* if finish task, notify father finish */ if (father != nullptr) { auto offLineTask = std::static_pointer_cast(father); diff --git a/services/distributedhardwarefwkservice/src/task/offline_task.cpp b/services/distributedhardwarefwkservice/src/task/offline_task.cpp index ea99ea93b28521953639e6ed273dfad21747849a..aedc343dafce13c8054184fa6dc8759834a6f452 100644 --- a/services/distributedhardwarefwkservice/src/task/offline_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/offline_task.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -98,7 +98,7 @@ void OffLineTask::DoTaskInner() TaskBoard::GetInstance().RemoveTask(this->GetId()); if (DHContext::GetInstance().GetRealTimeOnlineDeviceCount() == 0 && DHContext::GetInstance().GetIsomerismConnectCount() == 0 && - TaskBoard::GetInstance().IsAllTaskFinish()) { + TaskBoard::GetInstance().IsAllDisableTaskFinish()) { DHLOGI("all devices are offline, start to free the resource"); DistributedHardwareManagerFactory::GetInstance().UnInit(); } diff --git a/services/distributedhardwarefwkservice/src/task/task_board.cpp b/services/distributedhardwarefwkservice/src/task/task_board.cpp index 08c028a9fb07ecbd675f3e08944d27f95aad4268..8c49da9258679ef94c0f466bd92df869b362f844 100644 --- a/services/distributedhardwarefwkservice/src/task/task_board.cpp +++ b/services/distributedhardwarefwkservice/src/task/task_board.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -87,6 +87,39 @@ void TaskBoard::RemoveTaskInner(std::string taskId) tasks_.erase(taskId); } +void TaskBoard::AddDisableTask(std::shared_ptr task) +{ + if (task == nullptr) { + DHLOGE("task is null, error"); + return; + } + + std::unique_lock lock(disableTasksMtx_); + DHLOGI("Add disabletask, id: %{public}s", task->GetId().c_str()); + if (this->disableTasks_.find(task->GetId()) != this->disableTasks_.end()) { + DHLOGE("Task id duplicate, id: %{public}s", task->GetId().c_str()); + return; + } + this->disableTasks_.emplace(task->GetId(), task); +} + +void TaskBoard::RemoveDisableTask(const std::string &taskId) +{ + std::unique_lock lock(disableTasksMtx_); + DHLOGI("Remove disabletask, id: %{public}s", taskId.c_str()); + if (disableTasks_.find(taskId) == disableTasks_.end()) { + DHLOGE("Can not find removed task"); + return; + } + disableTasks_.erase(taskId); +} + +bool TaskBoard::IsAllDisableTaskFinish() +{ + std::shared_lock lock(disableTasksMtx_); + return this->disableTasks_.empty(); +} + void TaskBoard::DumpAllTasks(std::vector &taskInfos) { std::lock_guard lock(tasksMtx_); diff --git a/services/distributedhardwarefwkservice/src/task/task_factory.cpp b/services/distributedhardwarefwkservice/src/task/task_factory.cpp index ecb90ba3a8d78f656973e781210a6171eb2e0aed..8e567aa11ee2bdee4f06b8445450d0a9c2938464 100644 --- a/services/distributedhardwarefwkservice/src/task/task_factory.cpp +++ b/services/distributedhardwarefwkservice/src/task/task_factory.cpp @@ -73,14 +73,20 @@ std::shared_ptr TaskFactory::CreateTask(TaskType taskType, TaskParam taskP return nullptr; } } + AddTask(taskType, fatherTask, task); + return task; +} +void TaskFactory::AddTask(TaskType taskType, std::shared_ptr fatherTask, std::shared_ptr task) +{ if (fatherTask != nullptr) { task->SetFatherTask(fatherTask); fatherTask->AddChildrenTask(task); } TaskBoard::GetInstance().AddTask(task); - - return task; + if (taskType == TaskType::DISABLE || taskType == TaskType::META_DISABLE) { + TaskBoard::GetInstance().AddDisableTask(task); + } } std::shared_ptr TaskFactory::CreateEnableTask(const TaskParam &taskParam)