• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace OHOS {
19 namespace AAFwk {
20 constexpr int32_t QUEUE_TIME_OUT = 500000; // us
QueueTaskHandlerWrap(const std::string & queueName,TaskQoS queueQos)21 QueueTaskHandlerWrap::QueueTaskHandlerWrap(const std::string &queueName, TaskQoS queueQos)
22     : TaskHandlerWrap(queueName),
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     : TaskHandlerWrap(queueName), 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     ffrt::task_attr ffrtTaskAttr{};
37     BuildFfrtTaskAttr(taskAttr, ffrtTaskAttr);
38     if (taskAttr.insertHead_) {
39         return std::make_shared<InnerTaskHandle>(taskQueue_.submit_head_h(std::move(task), ffrtTaskAttr));
40     }
41     return std::make_shared<InnerTaskHandle>(taskQueue_.submit_h(std::move(task), ffrtTaskAttr));
42 }
43 
CancelTaskInner(const std::shared_ptr<InnerTaskHandle> & taskHandle)44 bool QueueTaskHandlerWrap::CancelTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle)
45 {
46     if (!taskHandle) {
47         return false;
48     }
49     return taskQueue_.cancel(taskHandle->GetFfrtHandle()) == 0;
50 }
WaitTaskInner(const std::shared_ptr<InnerTaskHandle> & taskHandle)51 void QueueTaskHandlerWrap::WaitTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle)
52 {
53     if (!taskHandle) {
54         return;
55     }
56     taskQueue_.wait(taskHandle->GetFfrtHandle());
57 }
58 
GetTaskCount()59 uint64_t QueueTaskHandlerWrap::GetTaskCount()
60 {
61     return taskQueue_.get_task_cnt();
62 }
63 } // namespace AAFwk
64 } // namespace OHOS