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 #include "hilog_wrapper.h"
19
20 namespace OHOS {
21 namespace AAFwk {
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)))
24 {}
SubmitTaskInner(std::function<void ()> && task,const TaskAttribute & taskAttr)25 std::shared_ptr<InnerTaskHandle> QueueTaskHandlerWrap::SubmitTaskInner(std::function<void()> &&task,
26 const TaskAttribute &taskAttr)
27 {
28 if (taskAttr.IsDefault()) {
29 return std::make_shared<InnerTaskHandle>(taskQueue_.submit_h(std::move(task)));
30 } else {
31 ffrt::task_attr ffrtTaskAttr;
32 BuildFfrtTaskAttr(taskAttr, ffrtTaskAttr);
33 return std::make_shared<InnerTaskHandle>(taskQueue_.submit_h(std::move(task),
34 ffrtTaskAttr));
35 }
36 }
CancelTaskInner(const std::shared_ptr<InnerTaskHandle> & taskHandle)37 bool QueueTaskHandlerWrap::CancelTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle)
38 {
39 if (!taskHandle) {
40 return false;
41 }
42 return taskQueue_.cancel(taskHandle->GetFfrtHandle()) == 0;
43 }
WaitTaskInner(const std::shared_ptr<InnerTaskHandle> & taskHandle)44 void QueueTaskHandlerWrap::WaitTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle)
45 {
46 if (!taskHandle) {
47 return;
48 }
49 taskQueue_.wait(taskHandle->GetFfrtHandle());
50 }
51 } // namespace AAFwk
52 } // namespace OHOS