1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_ABILITY_RUNTIME_STARTUP_TASK_DISPATCHER_H 17 #define OHOS_ABILITY_RUNTIME_STARTUP_TASK_DISPATCHER_H 18 19 #include <map> 20 #include <atomic> 21 22 #include "startup_sort_result.h" 23 #include "startup_task_result.h" 24 #include "startup_utils.h" 25 #include "startup_task.h" 26 27 namespace OHOS { 28 namespace AbilityRuntime { 29 class StartupTaskDispatcher : public std::enable_shared_from_this<StartupTaskDispatcher> { 30 public: 31 StartupTaskDispatcher(const std::map<std::string, std::shared_ptr<StartupTask>> &tasks, 32 const std::shared_ptr<StartupSortResult> &sortResult); 33 34 ~StartupTaskDispatcher(); 35 36 int32_t Run(const std::shared_ptr<OnCompletedCallback> &completedCallback, 37 const std::shared_ptr<OnCompletedCallback> &mainThreadAwaitCallback); 38 39 void TimeoutStop(); 40 41 private: 42 const std::map<std::string, std::shared_ptr<StartupTask>> &tasks_; 43 std::shared_ptr<StartupSortResult> sortResult_; 44 std::map<std::string, std::uint32_t> inDegreeMap_; 45 uint32_t mainThreadAwaitCount_ = 0; 46 uint32_t tasksCount_ = 0; 47 std::shared_ptr<OnCompletedCallback> completedCallback_; 48 std::shared_ptr<OnCompletedCallback> mainThreadAwaitCallback_; 49 std::atomic<bool> isTimeoutStopped_ = false; 50 51 void Dispatch(const std::string &name, const std::shared_ptr<StartupTaskResult> &result); 52 int32_t NotifyChildren(const std::string &name, const std::shared_ptr<StartupTaskResult> &result); 53 int32_t RunTaskInit(const std::string &name, const std::shared_ptr<StartupTask> &task); 54 void OnError(const std::string &name, const std::shared_ptr<StartupTaskResult> &result); 55 void OnError(int32_t errorCode, const std::string &errorMessage); 56 }; 57 } // namespace AbilityRuntime 58 } // namespace OHOS 59 #endif // OHOS_ABILITY_RUNTIME_STARTUP_TASK_DISPATCHER_H 60