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, uint32_t delayTime, 65 std::function<void()>&& traceIdFunc = nullptr) const; 66 bool PostTaskToTaskRunner(const RefPtr<TaskRunnerAdapter>& taskRunner, TaskExecutor::Task&& task, 67 uint32_t delayTime, const std::string& name, PriorityType priorityType = PriorityType::LOW) const; 68 void SetThreadPriority(int32_t priority) const; 69 bool OnPostTask(Task&& task, TaskType type, uint32_t delayTime, const std::string& name, 70 PriorityType priorityType = PriorityType::LOW) const final; 71 Task WrapTaskWithTraceId(Task&& task, int32_t id) const final; 72 void RemoveTaskFromTaskRunner(const RefPtr<TaskRunnerAdapter>& taskRunner, const std::string& name); 73 bool OnPostTaskWithoutTraceId(Task&& task, TaskType type, uint32_t delayTime, const std::string& name, 74 PriorityType priorityType = PriorityType::LOW) const final; 75 76 #ifdef ACE_DEBUG 77 bool OnPreSyncTask(TaskType type) const final; 78 void OnPostSyncTask() const final; 79 80 void DumpDeadSyncTask(TaskType from, TaskType to) const; 81 mutable std::unordered_map<std::thread::id, std::thread::id> syncTaskTable_; 82 #endif 83 84 void FillTaskTypeTable(TaskType type); 85 static void FillTaskTypeTable(const WeakPtr<TaskExecutorImpl>& weak, TaskType type); 86 87 struct ThreadInfo { 88 std::thread::id threadId; 89 int32_t tid = 0; 90 std::string threadName; 91 }; 92 93 mutable std::mutex tableMutex_; 94 std::unordered_map<TaskType, ThreadInfo> taskTypeTable_; 95 mutable std::array<std::atomic<uint32_t>, TASK_TYPE_SIZE> taskIdTable_ { 0 }; 96 97 static thread_local TaskType localTaskType; 98 99 RefPtr<TaskRunnerAdapter> platformRunner_; 100 RefPtr<TaskRunnerAdapter> uiRunner_; 101 RefPtr<TaskRunnerAdapter> ioRunner_; 102 RefPtr<TaskRunnerAdapter> jsRunner_; 103 RefPtr<TaskRunnerAdapter> gpuRunner_; 104 105 std::shared_ptr<TaskWrapper> taskWrapper_; 106 }; 107 } // namespace OHOS::Ace 108 #endif // FOUNDATION_ACE_FRAMEWORKS_COMMON_TASK_EXECUTOR_IMPL_H 109