• 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 #include "core/common/flutter/flutter_thread_model.h"
31 
32 namespace OHOS::Ace {
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, bool isStageModel = false);
43     void InitJsThread(bool newThread = true);
44     void InitOtherThreads(const flutter::TaskRunners& taskRunners);
45     void InitOtherThreads(FlutterThreadModel* threadModel);
46 
47     void AddTaskObserver(Task&& callback) override;
48     void RemoveTaskObserver() override;
49     void Destory() override;
50     bool WillRunOnCurrentThread(TaskType type) const final;
51 
GetTid(TaskType type)52     int32_t GetTid(TaskType type) final
53     {
54         return taskTypeTable_[type].tid;
55     }
56 
GetTotalTaskNum(TaskType type)57     uint32_t GetTotalTaskNum(TaskType type) final
58     {
59         return taskIdTable_[static_cast<uint32_t>(type)];
60     }
61 
62 private:
63     bool OnPostTask(Task&& task, TaskType type, uint32_t delayTime) const final;
64     Task WrapTaskWithTraceId(Task&& task, int32_t id) const final;
65 
66 #ifdef ACE_DEBUG
67     bool OnPreSyncTask(TaskType type) const final;
68     void OnPostSyncTask() const final;
69 
70     void DumpDeadSyncTask(TaskType from, TaskType to) const;
71     mutable std::unordered_map<std::thread::id, std::thread::id> syncTaskTable_;
72 #endif
73 
74     void FillTaskTypeTable(TaskType type);
75     static void FillTaskTypeTable(const WeakPtr<FlutterTaskExecutor>& weak, TaskType type);
76 
77     struct ThreadInfo {
78         std::thread::id threadId;
79         int32_t tid = 0;
80         std::string threadName;
81     };
82 
83     mutable std::mutex tableMutex_;
84     std::unordered_map<TaskType, ThreadInfo> taskTypeTable_;
85     mutable std::array<std::atomic<uint32_t>, TASK_TYPE_SIZE> taskIdTable_ { 0 };
86 
87     static thread_local TaskType localTaskType;
88 
89     std::unique_ptr<fml::Thread> jsThread_;
90 
91     fml::RefPtr<fml::TaskRunner> platformRunner_;
92     fml::RefPtr<fml::TaskRunner> uiRunner_;
93     fml::RefPtr<fml::TaskRunner> ioRunner_;
94     fml::RefPtr<fml::TaskRunner> jsRunner_;
95     fml::RefPtr<fml::TaskRunner> gpuRunner_;
96 };
97 } // namespace OHOS::Ace
98 #endif // FOUNDATION_ACE_FRAMEWORKS_COMMON_FLUTTER_FLUTTER_TASK_EXECUTOR_H
99