1 /* 2 * Copyright (c) 2023 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 FOUNDATION_ACE_FRAMEWORKS_COMMON_TASK_EXECUTOR_IMPL_H 17 #define FOUNDATION_ACE_FRAMEWORKS_COMMON_TASK_EXECUTOR_IMPL_H 18 19 #include <array> 20 #include <atomic> 21 #include <mutex> 22 #include <thread> 23 #include <unordered_map> 24 25 #include "base/thread/task_executor.h" 26 #include "base/utils/macros.h" 27 #include "core/common/task_runner_adapter.h" 28 #include "core/common/thread_model_impl.h" 29 30 namespace OHOS::Ace { 31 class ACE_EXPORT TaskExecutorImpl final : public TaskExecutor { 32 DECLARE_ACE_TYPE(TaskExecutorImpl, TaskExecutor); 33 34 public: 35 explicit TaskExecutorImpl(const RefPtr<TaskExecutorImpl>& taskExecutors); 36 explicit TaskExecutorImpl(const TaskRunners& taskRunners); TaskExecutorImpl(std::shared_ptr<TaskWrapper> taskWrapper)37 explicit TaskExecutorImpl(std::shared_ptr<TaskWrapper> taskWrapper) : taskWrapper_(taskWrapper) {} 38 TaskExecutorImpl() = default; ~TaskExecutorImpl()39 ~TaskExecutorImpl() override {}; 40 void InitPlatformThread(bool useCurrentEventRunner = false, bool isStageModel = false); 41 void InitJsThread(bool newThread = true); 42 void InitOtherThreads(const TaskRunners& taskRunners); 43 void InitOtherThreads(ThreadModelImpl* threadModel); 44 AddTaskObserver(Task && callback)45 void AddTaskObserver(Task&& callback) override {}; RemoveTaskObserver()46 void RemoveTaskObserver() override {}; 47 bool WillRunOnCurrentThread(TaskType type) const final; 48 void RemoveTask(TaskType type, const std::string &name) override; 49 GetTid(TaskType type)50 int32_t GetTid(TaskType type) final 51 { 52 return taskTypeTable_[type].tid; 53 } 54 GetTotalTaskNum(TaskType type)55 uint32_t GetTotalTaskNum(TaskType type) final 56 { 57 return taskIdTable_[static_cast<uint32_t>(type)]; 58 } 59 60 private: 61 TaskExecutor::Task WrapTaskWithContainer( 62 TaskExecutor::Task&& task, int32_t id, std::function<void()>&& traceIdFunc = nullptr) const; 63 TaskExecutor::Task WrapTaskWithCustomWrapper( 64 TaskExecutor::Task&& task, int32_t id, std::function<void()>&& traceIdFunc = nullptr) const; 65 bool PostTaskToTaskRunner(const RefPtr<TaskRunnerAdapter>& taskRunner, TaskExecutor::Task&& task, 66 uint32_t delayTime, const std::string& name, PriorityType priorityType = PriorityType::LOW) const; 67 void SetThreadPriority(int32_t priority) const; 68 bool OnPostTask(Task&& task, TaskType type, uint32_t delayTime, const std::string& name, 69 PriorityType priorityType = PriorityType::LOW) const final; 70 Task WrapTaskWithTraceId(Task&& task, int32_t id) const final; 71 void RemoveTaskFromTaskRunner(const RefPtr<TaskRunnerAdapter>& taskRunner, const std::string& name); 72 bool OnPostTaskWithoutTraceId(Task&& task, TaskType type, uint32_t delayTime, const std::string& name, 73 PriorityType priorityType = PriorityType::LOW) const final; 74 75 #ifdef ACE_DEBUG 76 bool OnPreSyncTask(TaskType type) const final; 77 void OnPostSyncTask() const final; 78 79 void DumpDeadSyncTask(TaskType from, TaskType to) const; 80 mutable std::unordered_map<std::thread::id, std::thread::id> syncTaskTable_; 81 #endif 82 83 void FillTaskTypeTable(TaskType type); 84 static void FillTaskTypeTable(const WeakPtr<TaskExecutorImpl>& weak, TaskType type); 85 86 struct ThreadInfo { 87 std::thread::id threadId; 88 int32_t tid = 0; 89 std::string threadName; 90 }; 91 92 mutable std::mutex tableMutex_; 93 std::unordered_map<TaskType, ThreadInfo> taskTypeTable_; 94 mutable std::array<std::atomic<uint32_t>, TASK_TYPE_SIZE> taskIdTable_ { 0 }; 95 96 static thread_local TaskType localTaskType; 97 98 RefPtr<TaskRunnerAdapter> platformRunner_; 99 RefPtr<TaskRunnerAdapter> uiRunner_; 100 RefPtr<TaskRunnerAdapter> ioRunner_; 101 RefPtr<TaskRunnerAdapter> jsRunner_; 102 RefPtr<TaskRunnerAdapter> gpuRunner_; 103 104 std::shared_ptr<TaskWrapper> taskWrapper_; 105 }; 106 } // namespace OHOS::Ace 107 #endif // FOUNDATION_ACE_FRAMEWORKS_COMMON_TASK_EXECUTOR_IMPL_H 108