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 #include "form_render_serial_queue.h"
16
17 #include <limits>
18
19 #include "fms_log_wrapper.h"
20
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace FormRender {
24 using namespace ffrt;
25 namespace {
26 constexpr uint32_t CONVERSION_FACTOR = 1000; // ms to us
27 }
28
FormRenderSerialQueue(const std::string & queueName)29 FormRenderSerialQueue::FormRenderSerialQueue(const std::string &queueName): queue_(queueName.c_str())
30 {
31 HILOG_DEBUG("create FormRenderSerialQueue, queueName :%{public}s", queueName.c_str());
32 }
33
~FormRenderSerialQueue()34 FormRenderSerialQueue::~FormRenderSerialQueue()
35 {
36 HILOG_DEBUG("destroy FormRenderSerialQueue");
37 }
38
ScheduleTask(uint64_t ms,std::function<void ()> func)39 bool FormRenderSerialQueue::ScheduleTask(uint64_t ms, std::function<void()> func)
40 {
41 HILOG_DEBUG("begin to ScheduleTask");
42 if (ms > (std::numeric_limits<uint64_t>::max() / CONVERSION_FACTOR)) {
43 HILOG_ERROR("invalid ms,ScheduleTask failed");
44 return false;
45 }
46 std::unique_lock<std::shared_mutex> lock(mutex_);
47 task_handle task_handle = queue_.submit_h(func, task_attr().delay(ms * CONVERSION_FACTOR));
48 if (task_handle == nullptr) {
49 HILOG_ERROR("submit_h return null");
50 return false;
51 }
52 HILOG_DEBUG("ScheduleTask success");
53 return true;
54 }
55
ScheduleDelayTask(const std::string & taskName,uint32_t ms,std::function<void ()> func)56 void FormRenderSerialQueue::ScheduleDelayTask(const std::string &taskName,
57 uint32_t ms, std::function<void()> func)
58 {
59 HILOG_DEBUG("begin to ScheduleDelayTask %{public}s", taskName.c_str());
60 std::unique_lock<std::shared_mutex> lock(mutex_);
61 task_handle task_handle = queue_.submit_h(func, task_attr().delay(ms * CONVERSION_FACTOR));
62 if (task_handle == nullptr) {
63 HILOG_ERROR("submit_h return null");
64 return;
65 }
66 taskMap_[taskName] = std::move(task_handle);
67 HILOG_DEBUG("ScheduleDelayTask success");
68 }
69
CancelDelayTask(const std::string & taskName)70 void FormRenderSerialQueue::CancelDelayTask(const std::string &taskName)
71 {
72 HILOG_DEBUG("begin to CancelDelayTask %{public}s", taskName.c_str());
73 std::unique_lock<std::shared_mutex> lock(mutex_);
74 auto item = taskMap_.find(taskName);
75 if (item == taskMap_.end()) {
76 HILOG_DEBUG("invalid task,CancelDelayTask %{public}s failed", taskName.c_str());
77 return;
78 }
79 if (item->second != nullptr) {
80 int32_t ret = queue_.cancel(item->second);
81 if (ret != 0) {
82 HILOG_ERROR("CancelDelayTask %{public}s failed,errCode:%{public}d", taskName.c_str(), ret);
83 }
84 }
85 taskMap_.erase(taskName);
86 HILOG_DEBUG("CancelDelayTask success");
87 }
88 } // namespace FormRender
89 } // namespace AppExecFwk
90 } // namespace OHOS
91