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 : TaskHandlerWrap(queueName),
24 taskQueue_(queueName.c_str(), ffrt::queue_attr().qos(Convert2FfrtQos(queueQos)).timeout(QUEUE_TIME_OUT))
25 {}
SubmitTaskInner(std::function<void ()> && task,const TaskAttribute & taskAttr)26 std::shared_ptr<InnerTaskHandle> QueueTaskHandlerWrap::SubmitTaskInner(std::function<void()> &&task,
27 const TaskAttribute &taskAttr)
28 {
29 ffrt::task_attr ffrtTaskAttr{};
30 BuildFfrtTaskAttr(taskAttr, ffrtTaskAttr);
31 if (taskAttr.insertHead_) {
32 return std::make_shared<InnerTaskHandle>(taskQueue_.submit_head_h(std::move(task),
33 ffrtTaskAttr));
34 }
35 return std::make_shared<InnerTaskHandle>(taskQueue_.submit_h(std::move(task),
36 ffrtTaskAttr));
37 }
38
CancelTaskInner(const std::shared_ptr<InnerTaskHandle> & taskHandle)39 bool QueueTaskHandlerWrap::CancelTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle)
40 {
41 if (!taskHandle) {
42 return false;
43 }
44 return taskQueue_.cancel(taskHandle->GetFfrtHandle()) == 0;
45 }
WaitTaskInner(const std::shared_ptr<InnerTaskHandle> & taskHandle)46 void QueueTaskHandlerWrap::WaitTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle)
47 {
48 if (!taskHandle) {
49 return;
50 }
51 taskQueue_.wait(taskHandle->GetFfrtHandle());
52 }
53
GetTaskCount()54 uint64_t QueueTaskHandlerWrap::GetTaskCount()
55 {
56 return taskQueue_.get_task_cnt();
57 }
58 } // namespace AAFwk
59 } // namespace OHOS