1
2 /*
3 * Copyright (C) 2023 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "ffrt_handler.h"
18
19 #include <limits>
20
21 #include "sam_log.h"
22
23 namespace OHOS {
24 using namespace ffrt;
25 namespace {
26 constexpr uint64_t CONVERSION_FACTOR = 1000; // ms to us
27 }
28
FFRTHandler(const std::string & name)29 FFRTHandler::FFRTHandler(const std::string& name)
30 {
31 queue_ = std::make_shared<queue>(name.c_str());
32 }
33
CleanFfrt()34 void FFRTHandler::CleanFfrt()
35 {
36 for (auto iter = taskMap_.begin(); iter != taskMap_.end(); ++iter) {
37 HILOGI("CleanFfrt taskMap_ %{public}s", iter->first.c_str());
38 if (queue_ != nullptr && iter->second != nullptr) {
39 auto ret = queue_->cancel(iter->second);
40 if (ret != 0) {
41 HILOGE("cancel task failed, error code %{public}d", ret);
42 }
43 }
44 iter->second = nullptr;
45 }
46 taskMap_.clear();
47 if (queue_ != nullptr) {
48 queue_.reset();
49 }
50 }
51
SetFfrt(const std::string & name)52 void FFRTHandler::SetFfrt(const std::string& name)
53 {
54 queue_ = std::make_shared<queue>(name.c_str());
55 }
56
PostTask(std::function<void ()> func)57 bool FFRTHandler::PostTask(std::function<void()> func)
58 {
59 task_handle handler = queue_->submit_h(func);
60 if (handler == nullptr) {
61 HILOGE("FFRTHandler post task failed");
62 return false;
63 }
64 return true;
65 }
66
PostTask(std::function<void ()> func,uint64_t delayTime)67 bool FFRTHandler::PostTask(std::function<void()> func, uint64_t delayTime)
68 {
69 if (delayTime > std::numeric_limits<uint64_t>::max() / CONVERSION_FACTOR) {
70 HILOGE("invalid delay time");
71 return false;
72 }
73 task_handle handler = queue_->submit_h(func, task_attr().delay(delayTime * CONVERSION_FACTOR));
74 if (handler == nullptr) {
75 HILOGE("FFRTHandler post task failed");
76 return false;
77 }
78 return true;
79 }
80
PostTask(std::function<void ()> func,const std::string & name,uint64_t delayTime)81 bool FFRTHandler::PostTask(std::function<void()> func, const std::string& name, uint64_t delayTime)
82 {
83 if (delayTime > std::numeric_limits<uint64_t>::max() / CONVERSION_FACTOR) {
84 HILOGE("invalid delay time");
85 return false;
86 }
87 std::unique_lock<std::shared_mutex> lock(mutex_);
88 task_handle handler = queue_->submit_h(func, task_attr().delay(delayTime * CONVERSION_FACTOR));
89 if (handler == nullptr) {
90 HILOGE("FFRTHandler post task failed");
91 return false;
92 }
93 taskMap_[name] = std::move(handler);
94 return true;
95 }
96
RemoveTask(const std::string & name)97 void FFRTHandler::RemoveTask(const std::string& name)
98 {
99 std::unique_lock<std::shared_mutex> lock(mutex_);
100 auto item = taskMap_.find(name);
101 if (item == taskMap_.end()) {
102 HILOGW("rm task %{public}s NF", name.c_str());
103 return;
104 }
105 if (item->second != nullptr) {
106 auto ret = queue_->cancel(item->second);
107 if (ret != 0) {
108 HILOGE("cancel task failed, error code %{public}d", ret);
109 }
110 }
111 taskMap_.erase(name);
112 }
113
DelTask(const std::string & name)114 void FFRTHandler::DelTask(const std::string& name)
115 {
116 std::unique_lock<std::shared_mutex> lock(mutex_);
117 auto item = taskMap_.find(name);
118 if (item == taskMap_.end()) {
119 HILOGW("del task %{public}s NF", name.c_str());
120 return;
121 }
122 HILOGD("erase task %{public}s ", name.c_str());
123 taskMap_.erase(name);
124 }
125
HasInnerEvent(const std::string name)126 bool FFRTHandler::HasInnerEvent(const std::string name)
127 {
128 std::unique_lock<std::shared_mutex> lock(mutex_);
129 auto item = taskMap_.find(name);
130 if (item == taskMap_.end()) {
131 return false;
132 }
133 return true;
134 }
135 } // namespace OHOS