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 
16 #include "queue_task_handler_wrap.h"
17 
18 
19 namespace OHOS {
20 namespace AAFwk {
21 constexpr int32_t QUEUE_TIME_OUT = 500000; // us
QueueTaskHandlerWrap(const std::string &queueName, TaskQoS queueQos)22 QueueTaskHandlerWrap::QueueTaskHandlerWrap(const std::string &queueName, TaskQoS queueQos)
23     : taskQueue_(queueName.c_str(), ffrt::queue_attr().qos(Convert2FfrtQos(queueQos)).timeout(QUEUE_TIME_OUT))
24 {}
25 
QueueTaskHandlerWrap(const std::string &queueName, int32_t concurrentNum, TaskQoS queueQos)26 QueueTaskHandlerWrap::QueueTaskHandlerWrap(const std::string &queueName, int32_t concurrentNum, TaskQoS queueQos)
27     : taskQueue_(
28         ffrt::queue_type::queue_concurrent,
29         queueName.c_str(),
30         ffrt::queue_attr().qos(Convert2FfrtQos(queueQos)).timeout(QUEUE_TIME_OUT).max_concurrency(concurrentNum))
31 {}
32 
SubmitTaskInner(std::function<void()> &&task, const TaskAttribute &taskAttr)33 std::shared_ptr<InnerTaskHandle> QueueTaskHandlerWrap::SubmitTaskInner(std::function<void()> &&task,
34     const TaskAttribute &taskAttr)
35 {
36     if (taskAttr.IsDefault()) {
37         return std::make_shared<InnerTaskHandle>(taskQueue_.submit_h(std::move(task)));
38     } else {
39         ffrt::task_attr ffrtTaskAttr;
40         BuildFfrtTaskAttr(taskAttr, ffrtTaskAttr);
41         return std::make_shared<InnerTaskHandle>(taskQueue_.submit_h(std::move(task),
42             ffrtTaskAttr));
43     }
44 }
CancelTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle)45 bool QueueTaskHandlerWrap::CancelTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle)
46 {
47     if (!taskHandle) {
48         return false;
49     }
50     return taskQueue_.cancel(taskHandle->GetFfrtHandle()) == 0;
51 }
WaitTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle)52 void QueueTaskHandlerWrap::WaitTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle)
53 {
54     if (!taskHandle) {
55         return;
56     }
57     taskQueue_.wait(taskHandle->GetFfrtHandle());
58 }
59 } // namespace AAFwk
60 } // namespace OHOS