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