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 GetTid(TaskType type)49 int32_t GetTid(TaskType type) final 50 { 51 return taskTypeTable_[type].tid; 52 } 53 GetTotalTaskNum(TaskType type)54 uint32_t GetTotalTaskNum(TaskType type) final 55 { 56 return taskIdTable_[static_cast<uint32_t>(type)]; 57 } 58 59 private: 60 TaskExecutor::Task WrapTaskWithContainer( 61 TaskExecutor::Task&& task, int32_t id, std::function<void()>&& traceIdFunc = nullptr) const; 62 TaskExecutor::Task WrapTaskWithCustomWrapper( 63 TaskExecutor::Task&& task, int32_t id, std::function<void()>&& traceIdFunc = nullptr) const; 64 bool PostTaskToTaskRunner(const RefPtr<TaskRunnerAdapter>& taskRunner, TaskExecutor::Task&& task, 65 uint32_t delayTime, const std::string& callerInfo = {}) const; 66 void SetThreadPriority(int32_t priority) const; 67 bool OnPostTask(Task&& task, TaskType type, uint32_t delayTime, const std::string& callerInfo) const final; 68 Task WrapTaskWithTraceId(Task&& task, int32_t id) const final; 69 70 #ifdef ACE_DEBUG 71 bool OnPreSyncTask(TaskType type) const final; 72 void OnPostSyncTask() const final; 73 74 void DumpDeadSyncTask(TaskType from, TaskType to) const; 75 mutable std::unordered_map<std::thread::id, std::thread::id> syncTaskTable_; 76 #endif 77 78 void FillTaskTypeTable(TaskType type); 79 static void FillTaskTypeTable(const WeakPtr<TaskExecutorImpl>& weak, TaskType type); 80 81 struct ThreadInfo { 82 std::thread::id threadId; 83 int32_t tid = 0; 84 std::string threadName; 85 }; 86 87 mutable std::mutex tableMutex_; 88 std::unordered_map<TaskType, ThreadInfo> taskTypeTable_; 89 mutable std::array<std::atomic<uint32_t>, TASK_TYPE_SIZE> taskIdTable_ { 0 }; 90 91 static thread_local TaskType localTaskType; 92 93 RefPtr<TaskRunnerAdapter> platformRunner_; 94 RefPtr<TaskRunnerAdapter> uiRunner_; 95 RefPtr<TaskRunnerAdapter> ioRunner_; 96 RefPtr<TaskRunnerAdapter> jsRunner_; 97 RefPtr<TaskRunnerAdapter> gpuRunner_; 98 99 std::shared_ptr<TaskWrapper> taskWrapper_; 100 }; 101 } // namespace OHOS::Ace 102 #endif // FOUNDATION_ACE_FRAMEWORKS_COMMON_TASK_EXECUTOR_IMPL_H 103