1 /*
2  * Copyright (c) 2024 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 #include "session/host/include/ws_ffrt_helper.h"
17 
18 #include <limits>
19 #include <shared_mutex>
20 #include <unordered_map>
21 
22 #include <unistd.h>
23 
24 #include "c/executor_task.h"
25 #include "ffrt.h"
26 #include "ffrt_inner.h"
27 #include "hitrace_meter.h"
28 #include "window_manager_hilog.h"
29 
30 namespace OHOS::Rosen {
31 namespace {
32 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WSFFRTHelper"};
33 const std::unordered_map<TaskQos, ffrt::qos> FFRT_QOS_MAP = {
34     { TaskQos::INHERIT, ffrt_qos_inherit },
35     { TaskQos::BACKGROUND, ffrt_qos_background },
36     { TaskQos::UTILITY, ffrt_qos_utility },
37     { TaskQos::DEFAULT, ffrt_qos_default },
38     { TaskQos::USER_INITIATED, ffrt_qos_user_initiated },
39     { TaskQos::DEADLINE_REQUEST, ffrt_qos_deadline_request },
40     { TaskQos::USER_INTERACTIVE, ffrt_qos_user_interactive },
41 };
42 }
43 
44 class TaskHandleMap {
45 public:
SaveTask(const std::string& taskName, ffrt::task_handle handle)46     void SaveTask(const std::string& taskName, ffrt::task_handle handle)
47     {
48         std::unique_lock<std::shared_mutex> lock(mutex_);
49         taskMap_[taskName] = std::move(handle);
50     }
51 
RemoveTask(const std::string& taskName)52     void RemoveTask(const std::string& taskName)
53     {
54         std::unique_lock<std::shared_mutex> lock(mutex_);
55         if (auto iter = taskMap_.find(taskName); iter != taskMap_.end()) {
56             if (iter->second != nullptr) {
57                 auto ret = ffrt::skip(iter->second);
58                 if (ret != 0) {
59                     WLOGI("Failed to cancel task, taskName = %{public}s, retcode = %{public}d", taskName.c_str(), ret);
60                 }
61             }
62             taskMap_.erase(iter);
63         } else {
64             WLOGI("Task is not existed, taskName = %{public}s", taskName.c_str());
65         }
66     }
67 
IsTaskExisted(const std::string& taskName)68     bool IsTaskExisted(const std::string& taskName)
69     {
70         std::shared_lock<std::shared_mutex> lock(mutex_);
71         auto iter = taskMap_.find(taskName);
72         return iter != taskMap_.end() && iter->second != nullptr;
73     }
74 
CountTask()75     std::size_t CountTask()
76     {
77         std::shared_lock<std::shared_mutex> lock(mutex_);
78         return taskMap_.size();
79     }
80 
81 private:
82     std::unordered_map<std::string, ffrt::task_handle> taskMap_;
83     std::shared_mutex mutex_;
84 };
85 
WSFFRTHelper()86 WSFFRTHelper::WSFFRTHelper() : taskHandleMap_(std::make_unique<TaskHandleMap>()) {}
87 
88 WSFFRTHelper::~WSFFRTHelper() = default;
89 
SubmitTask(std::function<void()>&& task, const std::string& taskName, uint64_t delayTime, TaskQos qos)90 void WSFFRTHelper::SubmitTask(std::function<void()>&& task, const std::string& taskName, uint64_t delayTime,
91     TaskQos qos)
92 {
93     auto localTask = [task = std::move(task), taskName]() {
94         HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "f:%s", taskName.c_str());
95         task();
96     };
97     if (delayTime == 0 && ffrt_get_cur_task() != nullptr) {
98         localTask();
99         return;
100     }
101     ffrt::task_handle handle = ffrt::submit_h(std::move(localTask), {}, {}, ffrt::task_attr().delay(delayTime).
102         qos(FFRT_QOS_MAP.at(qos)));
103     if (handle == nullptr) {
104         WLOGE("Failed to post task, taskName = %{public}s", taskName.c_str());
105         return;
106     }
107     taskHandleMap_->SaveTask(taskName, std::move(handle));
108 }
109 
CancelTask(const std::string& taskName)110 void WSFFRTHelper::CancelTask(const std::string& taskName)
111 {
112     taskHandleMap_->RemoveTask(taskName);
113 }
114 
IsTaskExisted(const std::string& taskName) const115 bool WSFFRTHelper::IsTaskExisted(const std::string& taskName) const
116 {
117     return taskHandleMap_->IsTaskExisted(taskName);
118 }
119 
CountTask() const120 std::size_t WSFFRTHelper::CountTask() const
121 {
122     return taskHandleMap_->CountTask();
123 }
124 } // namespace OHOS::Rosen
125