• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_FLUTTER_FLUTTER_TASK_EXECUTOR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_COMMON_FLUTTER_FLUTTER_TASK_EXECUTOR_H
18 
19 #include <array>
20 #include <atomic>
21 #include <mutex>
22 #include <thread>
23 #include <unordered_map>
24 
25 #include "flutter/common/task_runners.h"
26 #include "flutter/fml/thread.h"
27 
28 #include "base/thread/task_executor.h"
29 #include "base/utils/macros.h"
30 
31 namespace OHOS::Ace {
32 
33 class ACE_EXPORT FlutterTaskExecutor final : public TaskExecutor {
34     DECLARE_ACE_TYPE(FlutterTaskExecutor, TaskExecutor);
35 
36 public:
37     explicit FlutterTaskExecutor(const RefPtr<FlutterTaskExecutor>& taskExecutors);
38     explicit FlutterTaskExecutor(const flutter::TaskRunners& taskRunners);
39     FlutterTaskExecutor() = default;
40     ~FlutterTaskExecutor() final;
41     // Must call this method on platform thread
42     void InitPlatformThread(bool useCurrentEventRunner = false);
43     void InitJsThread(bool newThread = true);
44     void InitOtherThreads(const flutter::TaskRunners& taskRunners);
45 
46     void AddTaskObserver(Task&& callback) override;
47     void RemoveTaskObserver() override;
48     bool WillRunOnCurrentThread(TaskType type) const final;
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     bool OnPostTask(Task&& task, TaskType type, uint32_t delayTime) const final;
62     Task WrapTaskWithTraceId(Task&& task, int32_t id) const final;
63 
64 #ifdef ACE_DEBUG
65     bool OnPreSyncTask(TaskType type) const final;
66     void OnPostSyncTask() const final;
67 
68     void DumpDeadSyncTask(TaskType from, TaskType to) const;
69     mutable std::unordered_map<std::thread::id, std::thread::id> syncTaskTable_;
70 #endif
71 
72     void FillTaskTypeTable(TaskType type);
73     static void FillTaskTypeTable(const WeakPtr<FlutterTaskExecutor>& weak, TaskType type);
74 
75     struct ThreadInfo {
76         std::thread::id threadId;
77         int32_t tid = 0;
78         std::string threadName;
79     };
80 
81     mutable std::mutex tableMutex_;
82     std::unordered_map<TaskType, ThreadInfo> taskTypeTable_;
83     mutable std::array<std::atomic<uint32_t>, TASK_TYPE_SIZE> taskIdTable_ { 0 };
84 
85     static thread_local TaskType localTaskType;
86 
87     std::unique_ptr<fml::Thread> jsThread_;
88 
89     fml::RefPtr<fml::TaskRunner> platformRunner_;
90     fml::RefPtr<fml::TaskRunner> uiRunner_;
91     fml::RefPtr<fml::TaskRunner> ioRunner_;
92     fml::RefPtr<fml::TaskRunner> jsRunner_;
93     fml::RefPtr<fml::TaskRunner> gpuRunner_;
94 };
95 
96 } // namespace OHOS::Ace
97 
98 #endif // FOUNDATION_ACE_FRAMEWORKS_COMMON_FLUTTER_FLUTTER_TASK_EXECUTOR_H
99